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"
28 #include "file_name.h"
31 /* Fix the info in the MCWD file to be a proper directory name.
32 * Always has a leading separator. Never has a trailing separator
33 * (unless it is the path itself). */
35 static const char *fix_mcwd(char *ans)
42 if(!fp || !fgets(buf, MAX_PATH, fp)) {
45 ans[0] = get_default_drive();
50 buf[strlen(buf) -1] = '\0';
52 /* drive letter present? */
54 if (buf[0] && buf[1] == ':') {
59 ans[0] = get_default_drive();
62 /* add a leading separator */
63 if (*s != '/' && *s != '\\') {
70 /* translate to upper case */
71 for (s = ans; *s; ++s) {
77 /* if only drive, colon, & separator */
80 /* zap the trailing separator */
86 int unix_dir_loop(Stream_t *Stream, MainParam_t *mp);
87 int unix_loop(Stream_t *Stream UNUSEDP, MainParam_t *mp, char *arg,
90 static int _unix_loop(Stream_t *Dir, MainParam_t *mp,
91 const char *filename UNUSEDP)
93 return unix_dir_loop(Dir, mp);
96 int unix_loop(Stream_t *Stream UNUSEDP, MainParam_t *mp,
97 char *arg, int follow_dir_link)
101 size_t unixNameLength;
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(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,"Ambiguous\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_zt(&entry, filename,
260 mp->shortname.data, mp->shortname.len,
261 mp->longname.data, mp->longname.len)) == 0 ){
263 if(!checkForDot(mp->lookupflags,entry.name)) {
265 if((mp->lookupflags & DO_OPEN) ||
267 (mp->lookupflags & DO_OPEN_DIRS))) {
268 MyFile = mp->File = OpenFileByDirentry(&entry);
272 mp->direntry = &entry;
274 ret |= mp->dirCallback(&entry,mp);
276 ret |= mp->callback(&entry, mp);
281 if(mp->fast_quit && (ret & ERROR_ONE))
291 static int recurs_dos_loop(MainParam_t *mp, const char *filename0,
292 const char *filename1,
293 lookupState_t *lookupState)
295 /* Dir is de-allocated by the same entity which allocated it */
306 /* strip dots and / */
307 if(!strncmp(filename0,"./", 2)) {
311 if(!strcmp(filename0,".") && filename1) {
315 if(filename0[0] == '/') {
322 filename0 = filename1;
329 if(!strncmp(filename0,"../", 3) ||
330 (!strcmp(filename0, "..") && filename1)) {
332 mp->File = getDirentry(mp->File)->Dir;
333 return recurs_dos_loop(mp, filename0+2, filename1, lookupState);
336 doing_mcwd = !!filename1;
338 ptr = strchr(filename0, '/');
340 length = strlen(filename0);
344 length = ptrdiff(ptr, filename0);
348 if(mp->lookupflags & OPEN_PARENT) {
349 mp->targetName = filename0;
350 ret = handle_leaf(getDirentry(mp->File), mp,
356 if(!strcmp(filename0, ".") || !filename0[0]) {
357 return handle_leaf(getDirentry(mp->File),
361 if(!strcmp(filename0, "..")) {
362 return handle_leaf(getParent(getDirentry(mp->File)), mp,
366 lookupflags = mp->lookupflags;
369 lookupState->filename = filename0;
370 if(lookupState->nbContainers + lookupState->nbDirs > 0){
371 /* we have already one target, don't bother
373 FREE(&lookupState->container);
375 /* no match yet. Remember this container for
377 lookupState->container = COPY(mp->File);
379 lookupState->nbContainers++;
382 lookupflags = ACCEPT_DIR | DO_OPEN | NO_DOTS;
387 initializeDirentry(&entry, mp->File);
388 while(!(ret & STOP_NOW) &&
390 (r=vfat_lookup(&entry, filename0, length,
391 lookupflags | NO_MSG,
392 mp->shortname.data, mp->shortname.len,
393 mp->longname.data, mp->longname.len)) == 0 ){
394 if(checkForDot(lookupflags, entry.name))
395 /* while following the path, ignore the
396 * special entries if they were not
397 * explicitly given */
402 SubDir = mp->File = OpenFileByDirentry(&entry);
403 ret |= recurs_dos_loop(mp, ptr, filename1, lookupState);
406 ret |= handle_leaf(&entry, mp, lookupState);
407 if(isUniqueTarget(mp->targetName))
408 return ret | STOP_NOW;
416 return ret | ERROR_ONE;
417 if(doing_mcwd && !have_one)
422 static int common_dos_loop(MainParam_t *mp, const char *pathname,
423 lookupState_t *lookupState, int open_mode)
431 mp->loop = _dos_loop;
435 if(*pathname && pathname[1] == ':') {
436 drive = ch_toupper(*pathname);
438 if(mp->mcwd[0] == drive)
440 } else if(mp->mcwd[0]) {
444 drive = get_default_drive();
447 if(*pathname=='/') /* absolute path name */
450 RootDir = mp->File = open_root_dir(drive, open_mode, NULL);
454 ret = recurs_dos_loop(mp, cwd, pathname, lookupState);
459 ret = recurs_dos_loop(mp, "", pathname, lookupState);
465 static int dos_loop(MainParam_t *mp, const char *arg)
467 return common_dos_loop(mp, arg, 0, mp->openflags);
471 static int dos_target_lookup(MainParam_t *mp, const char *arg)
473 lookupState_t lookupState;
477 lookupState.nbDirs = 0;
479 lookupState.nbContainers = 0;
480 lookupState.container = 0;
482 lookupflags = mp->lookupflags;
483 mp->lookupflags = DO_OPEN | ACCEPT_DIR;
484 ret = common_dos_loop(mp, arg, &lookupState, O_RDWR);
485 mp->lookupflags = lookupflags;
489 if(lookupState.nbDirs) {
491 mp->targetDir = lookupState.Dir;
492 FREE(&lookupState.container); /* container no longer needed */
496 switch(lookupState.nbContainers) {
499 fprintf(stderr,"%s: no match for target\n", arg);
502 mp->targetName = strdup(lookupState.filename);
503 mp->targetDir = lookupState.container;
507 fprintf(stderr, "Ambiguous %s\n", arg);
513 * Is target a Unix directory
518 static int unix_is_dir(const char *name)
521 if(stat(name, &buf) < 0)
524 return 1 && S_ISDIR(buf.st_mode);
527 static int unix_target_lookup(MainParam_t *mp, const char *arg)
530 mp->unixTarget = strdup(arg);
531 /* try complete filename */
532 if(access(mp->unixTarget, F_OK) == 0) {
533 switch(unix_is_dir(mp->unixTarget)) {
542 ptr = strrchr(mp->unixTarget, '/');
544 mp->targetName = mp->unixTarget;
545 mp->unixTarget = strdup(".");
549 mp->targetName = ptr+1;
554 int target_lookup(MainParam_t *mp, const char *arg)
556 if((mp->lookupflags & NO_UNIX) || (arg[0] && arg[1] == ':' ))
557 return dos_target_lookup(mp, arg);
559 return unix_target_lookup(mp, arg);
562 int main_loop(MainParam_t *mp, char **argv, int argc)
569 if(argc != 1 && mp->targetName) {
571 "Several file names given, but last argument (%s) not a directory\n", mp->targetName);
572 FREE(&mp->targetDir);
576 for (i = 0; i < argc; i++) {
579 mp->originalArg = argv[i];
580 mp->basenameHasWildcard = strpbrk(_basename(mp->originalArg),
582 if (mp->unixcallback && (!argv[i][0]
583 #ifdef OS_mingw32msvc
584 /* On Windows, support only the command-line image drive. */
587 || argv[i][1] != ':' ))
588 ret = unix_loop(0, mp, argv[i], 1);
590 ret = dos_loop(mp, argv[i]);
592 if (! (ret & (GOT_ONE | ERROR_ONE)) ) {
593 /* one argument was unmatched */
594 fprintf(stderr, "%s: File \"%s\" not found\n",
599 if(mp->fast_quit && (Bret & (MISSED_ONE | ERROR_ONE)))
602 FREE(&mp->targetDir);
605 if ((Bret & GOT_ONE) && ( Bret & MISSED_ONE))
607 if (Bret & MISSED_ONE)
612 static int dispatchToFile(direntry_t *entry, MainParam_t *mp)
615 return mp->callback(entry, mp);
617 return mp->unixcallback(mp);
621 void init_mp(MainParam_t *mp)
624 mp->openflags = O_RDONLY;
628 mp->dirCallback = dispatchToFile;
629 mp->unixcallback = NULL;
630 mp->shortname.data = mp->longname.data = 0;
631 mp->shortname.len = mp->longname.len = 0;
636 const char *mpGetBasename(MainParam_t *mp)
639 wchar_to_native(mp->direntry->name, mp->targetBuffer,
640 MAX_VNAMELEN+1, sizeof(mp->targetBuffer));
641 return mp->targetBuffer;
643 return _basename(mp->unixSourceName);
646 void mpPrintFilename(FILE *fp, MainParam_t *mp)
649 fprintPwd(fp, mp->direntry, 0);
651 fprintf(fp,"%s",mp->originalArg);
654 const char *mpPickTargetName(MainParam_t *mp)
656 /* picks the target name: either the one explicitly given by the
657 * user, or the same as the source */
659 return mp->targetName;
661 return mpGetBasename(mp);
664 char *mpBuildUnixFilename(MainParam_t *mp)
670 target = mpPickTargetName(mp);
671 ret = malloc(strlen(mp->unixTarget) + 2 + strlen(target));
674 strcpy(ret, mp->unixTarget);
676 /* fix for 'mcopy -n x:file existingfile' -- H. Lermen 980816 */
677 if(!mp->targetName && !mp->targetDir && !unix_is_dir(ret))
680 if(!strcmp(target, ".")) {
682 } else if(!strcmp(target, "..")) {
685 while( (tmp=strchr(target, '/')) ) {
686 strncat(ret, target, ptrdiff(tmp,target));