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 UNUSEDP, MainParam_t *mp, char *arg,
92 static int _unix_loop(Stream_t *Dir, MainParam_t *mp,
93 const char *filename UNUSEDP)
95 return unix_dir_loop(Dir, mp);
98 int unix_loop(Stream_t *Stream UNUSEDP, MainParam_t *mp,
99 char *arg, int follow_dir_link)
103 size_t unixNameLength;
107 unixNameLength = strlen(arg);
108 if(unixNameLength > 1 && arg[unixNameLength-1] == '/') {
109 /* names ending in slash, and having at least two characters */
110 char *name = strdup(arg);
111 name[unixNameLength-1]='\0';
112 mp->unixSourceName = name;
114 mp->unixSourceName = arg;
116 /* mp->dir.attr = ATTR_ARCHIVE;*/
117 mp->loop = _unix_loop;
118 if((mp->lookupflags & DO_OPEN)){
119 mp->File = SimpleFileOpen(0, 0, arg, O_RDONLY, 0, 0, 0, 0);
123 tmp = _basename(arg);
124 strncpy(mp->filename, tmp, VBUFSIZE);
125 mp->filename[VBUFSIZE-1] = '\0';
129 GET_DATA(mp->File, 0, 0, &isdir, 0);
131 #if !defined(__EMX__) && !defined(OS_mingw32msvc)
136 #if !defined(__EMX__) && !defined(OS_mingw32msvc)
137 if(!follow_dir_link &&
138 MT_LSTAT(arg, &buf) == 0 &&
139 S_ISLNK(buf.st_mode)) {
140 /* skip links to directories in order to avoid
143 "skipping directory symlink %s\n",
148 if(! (mp->lookupflags & ACCEPT_DIR))
150 mp->File = OpenDir(arg);
155 ret = mp->dirCallback(0, mp);
157 ret = mp->unixcallback(mp);
163 int isSpecial(const char *name)
167 if(!strcmp(name,"."))
169 if(!strcmp(name,".."))
175 int isSpecialW(const wchar_t *name)
179 if(!wcscmp(name,L"."))
181 if(!wcscmp(name,L".."))
187 static int checkForDot(int lookupflags, const wchar_t *name)
189 return (lookupflags & NO_DOTS) && isSpecialW(name);
193 typedef struct lookupState_t {
198 const char *filename;
201 static int isUniqueTarget(const char *name)
203 return name && strcmp(name, "-");
206 static int handle_leaf(direntry_t *direntry, MainParam_t *mp,
207 lookupState_t *lookupState)
215 /* we are looking for a "target" file */
216 switch(lookupState->nbDirs) {
217 case 0: /* no directory yet, open it */
218 lookupState->Dir = OpenFileByDirentry(direntry);
219 lookupState->nbDirs++;
220 /* dump the container, we have
222 FREE(&lookupState->container);
224 case 1: /* we have already a directory */
225 FREE(&lookupState->Dir);
226 fprintf(stderr,"Ambiguous\n");
227 return STOP_NOW | ERROR_ONE;
229 return STOP_NOW | ERROR_ONE;
233 mp->direntry = direntry;
234 if(IS_DIR(direntry)) {
235 if(mp->lookupflags & (DO_OPEN | DO_OPEN_DIRS))
236 MyFile = mp->File = OpenFileByDirentry(direntry);
237 ret = mp->dirCallback(direntry, mp);
239 if(mp->lookupflags & DO_OPEN)
240 MyFile = mp->File = OpenFileByDirentry(direntry);
241 ret = mp->callback(direntry, mp);
244 if(isUniqueTarget(mp->targetName))
249 static int _dos_loop(Stream_t *Dir, MainParam_t *mp, const char *filename)
258 initializeDirentry(&entry, Dir);
260 (r=vfat_lookup_zt(&entry, filename,
262 mp->shortname.data, mp->shortname.len,
263 mp->longname.data, mp->longname.len)) == 0 ){
265 if(!checkForDot(mp->lookupflags,entry.name)) {
267 if((mp->lookupflags & DO_OPEN) ||
269 (mp->lookupflags & DO_OPEN_DIRS))) {
270 MyFile = mp->File = OpenFileByDirentry(&entry);
274 mp->direntry = &entry;
276 ret |= mp->dirCallback(&entry,mp);
278 ret |= mp->callback(&entry, mp);
283 if(mp->fast_quit && (ret & ERROR_ONE))
293 static int recurs_dos_loop(MainParam_t *mp, const char *filename0,
294 const char *filename1,
295 lookupState_t *lookupState)
297 /* Dir is de-allocated by the same entity which allocated it */
308 /* strip dots and / */
309 if(!strncmp(filename0,"./", 2)) {
313 if(!strcmp(filename0,".") && filename1) {
317 if(filename0[0] == '/') {
324 filename0 = filename1;
331 if(!strncmp(filename0,"../", 3) ||
332 (!strcmp(filename0, "..") && filename1)) {
334 mp->File = getDirentry(mp->File)->Dir;
335 return recurs_dos_loop(mp, filename0+2, filename1, lookupState);
338 doing_mcwd = !!filename1;
340 ptr = strchr(filename0, '/');
342 length = strlen(filename0);
346 length = ptrdiff(ptr, filename0);
350 if(mp->lookupflags & OPEN_PARENT) {
351 mp->targetName = filename0;
352 ret = handle_leaf(getDirentry(mp->File), mp,
358 if(!strcmp(filename0, ".") || !filename0[0]) {
359 return handle_leaf(getDirentry(mp->File),
363 if(!strcmp(filename0, "..")) {
364 return handle_leaf(getParent(getDirentry(mp->File)), mp,
368 lookupflags = mp->lookupflags;
371 lookupState->filename = filename0;
372 if(lookupState->nbContainers + lookupState->nbDirs > 0){
373 /* we have already one target, don't bother
375 FREE(&lookupState->container);
377 /* no match yet. Remember this container for
379 lookupState->container = COPY(mp->File);
381 lookupState->nbContainers++;
384 lookupflags = ACCEPT_DIR | DO_OPEN | NO_DOTS;
389 initializeDirentry(&entry, mp->File);
390 while(!(ret & STOP_NOW) &&
392 (r=vfat_lookup(&entry, filename0, length,
393 lookupflags | NO_MSG,
394 mp->shortname.data, mp->shortname.len,
395 mp->longname.data, mp->longname.len)) == 0 ){
396 if(checkForDot(lookupflags, entry.name))
397 /* while following the path, ignore the
398 * special entries if they were not
399 * explicitly given */
404 SubDir = mp->File = OpenFileByDirentry(&entry);
405 ret |= recurs_dos_loop(mp, ptr, filename1, lookupState);
408 ret |= handle_leaf(&entry, mp, lookupState);
409 if(isUniqueTarget(mp->targetName))
410 return ret | STOP_NOW;
418 return ret | ERROR_ONE;
419 if(doing_mcwd && !have_one)
424 static int common_dos_loop(MainParam_t *mp, const char *pathname,
425 lookupState_t *lookupState, int open_mode)
433 mp->loop = _dos_loop;
437 if(*pathname && pathname[1] == ':') {
438 drive = ch_toupper(*pathname);
440 if(mp->mcwd[0] == drive)
442 } else if(mp->mcwd[0]) {
446 drive = get_default_drive();
449 if(*pathname=='/') /* absolute path name */
452 RootDir = mp->File = open_root_dir(drive, open_mode, NULL);
456 ret = recurs_dos_loop(mp, cwd, pathname, lookupState);
461 ret = recurs_dos_loop(mp, "", pathname, lookupState);
467 static int dos_loop(MainParam_t *mp, const char *arg)
469 return common_dos_loop(mp, arg, 0, mp->openflags);
473 static int dos_target_lookup(MainParam_t *mp, const char *arg)
475 lookupState_t lookupState;
479 lookupState.nbDirs = 0;
481 lookupState.nbContainers = 0;
482 lookupState.container = 0;
484 lookupflags = mp->lookupflags;
485 mp->lookupflags = DO_OPEN | ACCEPT_DIR;
486 ret = common_dos_loop(mp, arg, &lookupState, O_RDWR);
487 mp->lookupflags = lookupflags;
491 if(lookupState.nbDirs) {
493 mp->targetDir = lookupState.Dir;
494 FREE(&lookupState.container); /* container no longer needed */
498 switch(lookupState.nbContainers) {
501 fprintf(stderr,"%s: no match for target\n", arg);
504 mp->targetName = strdup(lookupState.filename);
505 mp->targetDir = lookupState.container;
509 fprintf(stderr, "Ambiguous %s\n", arg);
514 static int unix_target_lookup(MainParam_t *mp, const char *arg)
517 mp->unixTarget = strdup(arg);
518 /* try complete filename */
519 if(access(mp->unixTarget, F_OK) == 0)
521 ptr = strrchr(mp->unixTarget, '/');
523 mp->targetName = mp->unixTarget;
524 mp->unixTarget = strdup(".");
528 mp->targetName = ptr+1;
533 int target_lookup(MainParam_t *mp, const char *arg)
535 if((mp->lookupflags & NO_UNIX) || (arg[0] && arg[1] == ':' ))
536 return dos_target_lookup(mp, arg);
538 return unix_target_lookup(mp, arg);
541 int main_loop(MainParam_t *mp, char **argv, int argc)
548 if(argc != 1 && mp->targetName) {
550 "Several file names given, but last argument (%s) not a directory\n", mp->targetName);
553 for (i = 0; i < argc; i++) {
556 mp->originalArg = argv[i];
557 mp->basenameHasWildcard = strpbrk(_basename(mp->originalArg),
559 if (mp->unixcallback && (!argv[i][0]
560 #ifdef OS_mingw32msvc
561 /* On Windows, support only the command-line image drive. */
564 || argv[i][1] != ':' ))
565 ret = unix_loop(0, mp, argv[i], 1);
567 ret = dos_loop(mp, argv[i]);
569 if (! (ret & (GOT_ONE | ERROR_ONE)) ) {
570 /* one argument was unmatched */
571 fprintf(stderr, "%s: File \"%s\" not found\n",
576 if(mp->fast_quit && (Bret & (MISSED_ONE | ERROR_ONE)))
579 FREE(&mp->targetDir);
582 if ((Bret & GOT_ONE) && ( Bret & MISSED_ONE))
584 if (Bret & MISSED_ONE)
589 static int dispatchToFile(direntry_t *entry, MainParam_t *mp)
592 return mp->callback(entry, mp);
594 return mp->unixcallback(mp);
598 void init_mp(MainParam_t *mp)
601 mp->openflags = O_RDONLY;
605 mp->dirCallback = dispatchToFile;
606 mp->unixcallback = NULL;
607 mp->shortname.data = mp->longname.data = 0;
608 mp->shortname.len = mp->longname.len = 0;
613 const char *mpGetBasename(MainParam_t *mp)
616 wchar_to_native(mp->direntry->name, mp->targetBuffer,
617 MAX_VNAMELEN+1, sizeof(mp->targetBuffer));
618 return mp->targetBuffer;
620 return _basename(mp->unixSourceName);
623 void mpPrintFilename(FILE *fp, MainParam_t *mp)
626 fprintPwd(fp, mp->direntry, 0);
628 fprintf(fp,"%s",mp->originalArg);
631 const char *mpPickTargetName(MainParam_t *mp)
633 /* picks the target name: either the one explicitly given by the
634 * user, or the same as the source */
636 return mp->targetName;
638 return mpGetBasename(mp);
641 char *mpBuildUnixFilename(MainParam_t *mp)
647 target = mpPickTargetName(mp);
648 ret = malloc(strlen(mp->unixTarget) + 2 + strlen(target));
651 strcpy(ret, mp->unixTarget);
653 #if 1 /* fix for 'mcopy -n x:file existingfile' -- H. Lermen 980816 */
654 if(!mp->targetName && !mp->targetDir) {
656 if (!MT_STAT(ret, &buf) && !S_ISDIR(buf.st_mode))
661 if(!strcmp(target, ".")) {
663 } else if(!strcmp(target, "..")) {
666 while( (tmp=strchr(target, '/')) ) {
667 strncat(ret, target, ptrdiff(tmp,target));