1 /* Copyright 1997-2002,2005-2009 Alain Knaff.
2 * This file is part of mtools.
4 * Mtools is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Mtools is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
18 * Iterating over all the command line parameters, and matching patterns
22 #include "sysincludes.h"
30 #include "file_name.h"
33 /* Fix the info in the MCWD file to be a proper directory name.
34 * Always has a leading separator. Never has a trailing separator
35 * (unless it is the path itself). */
37 static const char *fix_mcwd(char *ans)
44 if(!fp || !fgets(buf, MAX_PATH, fp)) {
47 ans[0] = get_default_drive();
52 buf[strlen(buf) -1] = '\0';
54 /* drive letter present? */
56 if (buf[0] && buf[1] == ':') {
61 ans[0] = get_default_drive();
64 /* add a leading separator */
65 if (*s != '/' && *s != '\\') {
72 /* translate to upper case */
73 for (s = ans; *s; ++s) {
79 /* if only drive, colon, & separator */
82 /* zap the trailing separator */
88 int unix_dir_loop(Stream_t *Stream, MainParam_t *mp);
89 int unix_loop(Stream_t *Stream, MainParam_t *mp, char *arg,
92 static int _unix_loop(Stream_t *Dir, MainParam_t *mp, const char *filename)
94 return unix_dir_loop(Dir, mp);
97 int unix_loop(Stream_t *Stream, MainParam_t *mp, char *arg, int follow_dir_link)
105 unixNameLength = strlen(arg);
106 if(unixNameLength > 1 && arg[unixNameLength-1] == '/') {
107 /* names ending in slash, and having at least two characters */
108 char *name = strdup(arg);
109 name[unixNameLength-1]='\0';
110 mp->unixSourceName = name;
112 mp->unixSourceName = arg;
114 /* mp->dir.attr = ATTR_ARCHIVE;*/
115 mp->loop = _unix_loop;
116 if((mp->lookupflags & DO_OPEN)){
117 mp->File = SimpleFileOpen(0, 0, arg, O_RDONLY, 0, 0, 0, 0);
121 tmp = _basename(arg);
122 strncpy(mp->filename, tmp, VBUFSIZE);
123 mp->filename[VBUFSIZE-1] = '\0';
127 GET_DATA(mp->File, 0, 0, &isdir, 0);
129 #if !defined(__EMX__) && !defined(OS_mingw32msvc)
134 #if !defined(__EMX__) && !defined(OS_mingw32msvc)
135 if(!follow_dir_link &&
136 MT_LSTAT(arg, &buf) == 0 &&
137 S_ISLNK(buf.st_mode)) {
138 /* skip links to directories in order to avoid
141 "skipping directory symlink %s\n",
146 if(! (mp->lookupflags & ACCEPT_DIR))
148 mp->File = OpenDir(Stream, arg);
153 ret = mp->dirCallback(0, mp);
155 ret = mp->unixcallback(mp);
161 int isSpecial(const char *name)
165 if(!strcmp(name,"."))
167 if(!strcmp(name,".."))
173 int isSpecialW(const wchar_t *name)
177 if(!wcscmp(name,L"."))
179 if(!wcscmp(name,L".."))
185 static int checkForDot(int lookupflags, const wchar_t *name)
187 return (lookupflags & NO_DOTS) && isSpecialW(name);
191 typedef struct lookupState_t {
196 const char *filename;
199 static int isUniqueTarget(const char *name)
201 return name && strcmp(name, "-");
204 static int handle_leaf(direntry_t *direntry, MainParam_t *mp,
205 lookupState_t *lookupState)
213 /* we are looking for a "target" file */
214 switch(lookupState->nbDirs) {
215 case 0: /* no directory yet, open it */
216 lookupState->Dir = OpenFileByDirentry(direntry);
217 lookupState->nbDirs++;
218 /* dump the container, we have
220 FREE(&lookupState->container);
222 case 1: /* we have already a directory */
223 FREE(&lookupState->Dir);
224 fprintf(stderr,"Ambigous\n");
225 return STOP_NOW | ERROR_ONE;
227 return STOP_NOW | ERROR_ONE;
231 mp->direntry = direntry;
232 if(IS_DIR(direntry)) {
233 if(mp->lookupflags & (DO_OPEN | DO_OPEN_DIRS))
234 MyFile = mp->File = OpenFileByDirentry(direntry);
235 ret = mp->dirCallback(direntry, mp);
237 if(mp->lookupflags & DO_OPEN)
238 MyFile = mp->File = OpenFileByDirentry(direntry);
239 ret = mp->callback(direntry, mp);
242 if(isUniqueTarget(mp->targetName))
247 static int _dos_loop(Stream_t *Dir, MainParam_t *mp, const char *filename)
256 initializeDirentry(&entry, Dir);
258 (r=vfat_lookup(&entry, filename, -1,
259 mp->lookupflags, mp->shortname,
260 mp->longname)) == 0 ){
262 if(!checkForDot(mp->lookupflags,entry.name)) {
264 if((mp->lookupflags & DO_OPEN) ||
266 (mp->lookupflags & DO_OPEN_DIRS))) {
267 MyFile = mp->File = OpenFileByDirentry(&entry);
271 mp->direntry = &entry;
273 ret |= mp->dirCallback(&entry,mp);
275 ret |= mp->callback(&entry, mp);
280 if(mp->fast_quit && (ret & ERROR_ONE))
290 static int recurs_dos_loop(MainParam_t *mp, const char *filename0,
291 const char *filename1,
292 lookupState_t *lookupState)
294 /* Dir is de-allocated by the same entity which allocated it */
305 /* strip dots and / */
306 if(!strncmp(filename0,"./", 2)) {
310 if(!strcmp(filename0,".") && filename1) {
314 if(filename0[0] == '/') {
321 filename0 = filename1;
328 if(!strncmp(filename0,"../", 3) ||
329 (!strcmp(filename0, "..") && filename1)) {
331 mp->File = getDirentry(mp->File)->Dir;
332 return recurs_dos_loop(mp, filename0+2, filename1, lookupState);
335 doing_mcwd = !!filename1;
337 ptr = strchr(filename0, '/');
339 length = strlen(filename0);
343 length = ptr - filename0;
347 if(mp->lookupflags & OPEN_PARENT) {
348 mp->targetName = filename0;
349 ret = handle_leaf(getDirentry(mp->File), mp,
355 if(!strcmp(filename0, ".") || !filename0[0]) {
356 return handle_leaf(getDirentry(mp->File),
360 if(!strcmp(filename0, "..")) {
361 return handle_leaf(getParent(getDirentry(mp->File)), mp,
365 lookupflags = mp->lookupflags;
368 lookupState->filename = filename0;
369 if(lookupState->nbContainers + lookupState->nbDirs > 0){
370 /* we have already one target, don't bother
372 FREE(&lookupState->container);
374 /* no match yet. Remember this container for
376 lookupState->container = COPY(mp->File);
378 lookupState->nbContainers++;
381 lookupflags = ACCEPT_DIR | DO_OPEN | NO_DOTS;
386 initializeDirentry(&entry, mp->File);
387 while(!(ret & STOP_NOW) &&
389 (r=vfat_lookup(&entry, filename0, length,
390 lookupflags | NO_MSG,
391 mp->shortname, mp->longname)) == 0 ){
392 if(checkForDot(lookupflags, entry.name))
393 /* while following the path, ignore the
394 * special entries if they were not
395 * explicitly given */
400 SubDir = mp->File = OpenFileByDirentry(&entry);
401 ret |= recurs_dos_loop(mp, ptr, filename1, lookupState);
404 ret |= handle_leaf(&entry, mp, lookupState);
405 if(isUniqueTarget(mp->targetName))
406 return ret | STOP_NOW;
414 return ret | ERROR_ONE;
415 if(doing_mcwd && !have_one)
420 static int common_dos_loop(MainParam_t *mp, const char *pathname,
421 lookupState_t *lookupState, int open_mode)
429 mp->loop = _dos_loop;
433 if(*pathname && pathname[1] == ':') {
434 drive = toupper(*pathname);
436 if(mp->mcwd[0] == drive)
438 } else if(mp->mcwd[0]) {
442 drive = get_default_drive();
445 if(*pathname=='/') /* absolute path name */
448 RootDir = mp->File = open_root_dir(drive, open_mode, NULL);
452 ret = recurs_dos_loop(mp, cwd, pathname, lookupState);
457 ret = recurs_dos_loop(mp, "", pathname, lookupState);
463 static int dos_loop(MainParam_t *mp, const char *arg)
465 return common_dos_loop(mp, arg, 0, mp->openflags);
469 static int dos_target_lookup(MainParam_t *mp, const char *arg)
471 lookupState_t lookupState;
475 lookupState.nbDirs = 0;
477 lookupState.nbContainers = 0;
478 lookupState.container = 0;
480 lookupflags = mp->lookupflags;
481 mp->lookupflags = DO_OPEN | ACCEPT_DIR;
482 ret = common_dos_loop(mp, arg, &lookupState, O_RDWR);
483 mp->lookupflags = lookupflags;
487 if(lookupState.nbDirs) {
489 mp->targetDir = lookupState.Dir;
490 FREE(&lookupState.container); /* container no longer needed */
494 switch(lookupState.nbContainers) {
497 fprintf(stderr,"%s: no match for target\n", arg);
500 mp->targetName = strdup(lookupState.filename);
501 mp->targetDir = lookupState.container;
505 fprintf(stderr, "Ambigous %s\n", arg);
510 static int unix_target_lookup(MainParam_t *mp, const char *arg)
513 mp->unixTarget = strdup(arg);
514 /* try complete filename */
515 if(access(mp->unixTarget, F_OK) == 0)
517 ptr = strrchr(mp->unixTarget, '/');
519 mp->targetName = mp->unixTarget;
520 mp->unixTarget = strdup(".");
524 mp->targetName = ptr+1;
529 int target_lookup(MainParam_t *mp, const char *arg)
531 if((mp->lookupflags & NO_UNIX) || (arg[0] && arg[1] == ':' ))
532 return dos_target_lookup(mp, arg);
534 return unix_target_lookup(mp, arg);
537 int main_loop(MainParam_t *mp, char **argv, int argc)
544 if(argc != 1 && mp->targetName) {
546 "Several file names given, but last argument (%s) not a directory\n", mp->targetName);
549 for (i = 0; i < argc; i++) {
552 mp->originalArg = argv[i];
553 mp->basenameHasWildcard = strpbrk(_basename(mp->originalArg),
555 if (mp->unixcallback && (!argv[i][0]
556 #ifdef OS_mingw32msvc
557 /* On Windows, support only the command-line image drive. */
560 || argv[i][1] != ':' ))
561 ret = unix_loop(0, mp, argv[i], 1);
563 ret = dos_loop(mp, argv[i]);
565 if (! (ret & (GOT_ONE | ERROR_ONE)) ) {
566 /* one argument was unmatched */
567 fprintf(stderr, "%s: File \"%s\" not found\n",
572 if(mp->fast_quit && (Bret & (MISSED_ONE | ERROR_ONE)))
575 FREE(&mp->targetDir);
578 if ((Bret & GOT_ONE) && ( Bret & MISSED_ONE))
580 if (Bret & MISSED_ONE)
585 static int dispatchToFile(direntry_t *entry, MainParam_t *mp)
588 return mp->callback(entry, mp);
590 return mp->unixcallback(mp);
594 void init_mp(MainParam_t *mp)
597 mp->openflags = O_RDONLY;
601 mp->dirCallback = dispatchToFile;
602 mp->unixcallback = NULL;
603 mp->shortname = mp->longname = 0;
608 const char *mpGetBasename(MainParam_t *mp)
611 wchar_to_native(mp->direntry->name, mp->targetBuffer,
613 return mp->targetBuffer;
615 return _basename(mp->unixSourceName);
618 void mpPrintFilename(FILE *fp, MainParam_t *mp)
621 fprintPwd(fp, mp->direntry, 0);
623 fprintf(fp,"%s",mp->originalArg);
626 const char *mpPickTargetName(MainParam_t *mp)
628 /* picks the target name: either the one explicitly given by the
629 * user, or the same as the source */
631 return mp->targetName;
633 return mpGetBasename(mp);
636 char *mpBuildUnixFilename(MainParam_t *mp)
642 target = mpPickTargetName(mp);
643 ret = malloc(strlen(mp->unixTarget) + 2 + strlen(target));
646 strcpy(ret, mp->unixTarget);
648 #if 1 /* fix for 'mcopy -n x:file existingfile' -- H. Lermen 980816 */
649 if(!mp->targetName && !mp->targetDir) {
651 if (!MT_STAT(ret, &buf) && !S_ISDIR(buf.st_mode))
656 if(!strcmp(target, ".")) {
658 } else if(!strcmp(target, "..")) {
661 while( (tmp=strchr(target, '/')) ) {
662 strncat(ret, target, tmp-target);