home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / zoo_src / z201src1 / makelist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-25  |  6.1 KB  |  235 lines

  1. #ifndef LINT
  2. /* @(#) makelist.c 2.3 88/01/24 12:46:44 */
  3. static char sccsid[]="@(#) makelist.c 2.3 88/01/24 12:46:44";
  4. #endif /* LINT */
  5.  
  6. /*
  7. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  8. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  9. */
  10.  
  11. #include "options.h"
  12. #include "portable.h"
  13. #include "errors.i"
  14. #include "zoo.h"
  15. #include "zooio.h"
  16. #include "various.h"
  17.  
  18. #include "zoofns.h"
  19. #include "assert.h"
  20. #include "debug.h"
  21.  
  22. #ifdef LINT_ARGS
  23. char *nameptr (char *);
  24. void modpath (char *);
  25. #else
  26. char *nameptr();
  27. void modpath ();
  28. #endif
  29.  
  30. /*******************/
  31. /*
  32. makelist() gets all pathnames corresponding to a set of filespecs and
  33. adds them to a list.  Not more than "flistsize" pathnames are added.
  34. Into `longest' it returns the length of the longest name added, or
  35. zero if none added.
  36.  
  37. Files ignore1, ignore2, and ignore3 are not added to the list.
  38. A file that is a device/directory is also not added to the list.
  39.  
  40. However, if ignore1 is NULL, both these tests are skipped and all files
  41. will be added to the list.
  42. */
  43.  
  44. void makelist (argc, argv, flist, flistsize, ignore1, ignore2, ignore3, longest)
  45. int argc;               /* number of filespec supplied */
  46. char *argv[];           /* array of pointers to supplied filespecs */
  47. register char *flist[]; /* array of pointers to filenames we will add */
  48. int flistsize;          /* home many names we can use */
  49. char *ignore1, *ignore2, *ignore3; /* files to exclude from list */
  50. int *longest;        /* length of longest name in list */
  51. {
  52.    char *this_path;        /* current pathname */
  53.    int fptr;               /* pointer to within flist */
  54.    register int i, j;      /* loop counters */
  55.  
  56. #ifdef WILDCARD
  57.    char *pat_name;         /* filename part of pattern */
  58. #endif
  59.  
  60.    int gap;                /* for Shell sort */
  61.    
  62.    flistsize--;            /* allow for one terminating NULL entry */
  63.    fptr = *longest = 0;
  64.  
  65.    assert(argc > 0);
  66.  
  67. #define WCLEN    4    /* length needed for wildcard, and a little extra */
  68.  
  69.    while (argc > 0) {
  70. #ifdef WILDCARD
  71.         int argok = 0;                                            /* arg not matched yet */
  72. #endif
  73.       char *this_arg;
  74.         this_arg = emalloc (strlen (*argv) + WCLEN);
  75.         strcpy (this_arg, *argv);
  76.  
  77.       /* Initialize fileset 0.  Select all files -- we will later
  78.              filter out the ones wanted */
  79. #ifdef FOLD
  80.       strlwr (this_arg);
  81. #endif
  82.  
  83. #ifdef WILDCARD
  84.       pat_name = strdup (nameptr (this_arg));        /* pattern without path */
  85. #ifdef VER_CH /* trailing version field */
  86. {
  87.    static char version_field[] = " *";
  88.    char *p;
  89.    p = strrchr (pat_name, VER_CH);
  90.    if (p == NULL) {
  91.       *version_field = VER_CH;
  92.       pat_name = newcat (pat_name, version_field); /* adds trailing ";*" */
  93.    }
  94. }
  95. #endif
  96.         /*
  97.         replace filename by wildcard;  however, if argument ends in slash, 
  98.         then simply append wildcard so we get all files in that directory
  99.         */
  100. #ifdef FORCESLASH
  101.             fixslash (this_arg);                /* convert backslashes to slashes */
  102. #endif
  103.  
  104.         if (*lastptr(this_arg) == *PATH_CH) {
  105.             strcat (this_arg, WILDCARD);
  106.             pat_name = "*";                    /* and select all files */
  107.         } else
  108. #ifdef SPEC_WILD
  109.             spec_wild (this_arg);
  110. #else
  111.           strcpy (nameptr (this_arg), WILDCARD);
  112. #endif /* SPEC_WILD */
  113. #endif /* WILDCARD */
  114.  
  115.       nextfile (0, this_arg, 0);
  116.       while (fptr < flistsize && 
  117.             (this_path = nextfile(1, (char *) NULL, 0)) != NULL) {
  118.          char *this_name = nameptr (this_path);
  119.             modpath (this_path);                    /* do any needed changes to path */
  120.  
  121. #ifdef IGNORECASE
  122. #define    COMPARE    strcmpi
  123. #else
  124. #define    COMPARE    strcmp
  125. #endif
  126.             if (ignore1 != NULL) {
  127.                 if (samefile (this_name,ignore1)  ||    /* exclude ignored files */
  128.                      samefile (this_name,ignore2)  ||
  129.                      samefile (this_name,ignore3))
  130.                     continue;
  131.  
  132. #ifdef CHEKUDIR
  133.                 if (isuadir(this_path))
  134.                     continue;
  135. #else /* CHEKUDIR */
  136. # ifdef CHEKDIR
  137.                 if (isfdir(this_path))
  138.                     continue;
  139. # endif /* CHEKDIR */
  140. #endif /* CHEKUDIR */
  141.             } /* end if ignore1 ! = NULL */
  142.  
  143. /* 
  144. If WILDCARD is defined (e.g. AmigaDOS, MS-DOS, VAX/VMS), then nextfile()
  145. returns all filenames and we must now select the ones we need by pattern
  146. matching.  If WILDCARD is not defined (e.g. **IX), filenames have already been
  147. selected by the shell and need not be tested again. 
  148. */
  149. #ifdef WILDCARD
  150.             if (match_half (this_name,pat_name) ||
  151.                 match_half (pat_name, "?-?") &&     /* character range */
  152.                     *this_name >= *pat_name && *this_name <= pat_name[2])
  153. #endif
  154.             {
  155. #ifdef WILDCARD
  156.                 argok = 1;                                    /* remember arg matched */
  157. #endif
  158.                 flist[fptr++] = strdup (this_path);
  159.                 if (*longest < strlen(this_path))
  160.                     *longest = strlen(this_path);
  161.             }
  162.  
  163.         } /* end while */
  164. #ifdef WILDCARD
  165.         if (argok == 0) {                                    /* no match for argument */
  166.             prterror ('e', "Could not open %s\n", *argv);
  167.         }
  168. #endif
  169.       argc--;
  170.       argv++;
  171.    }
  172.    /* fptr is now 1 + index of last item in array */
  173.  
  174.    if (this_path != NULL && fptr >= flistsize)
  175.       prterror ('w', too_many_files, flistsize);
  176. #ifndef  DONT_SORT
  177.    /* Shell sort -- K&R p. 58 */
  178.    for (gap = fptr/2; gap > 0; gap /= 2)
  179.       for (i = gap; i < fptr; i++)
  180.          for (j = i - gap; j >= 0 && 
  181.             strcmp(flist[j],flist[j+gap]) > 0; j -= gap) {
  182.             char *t = flist[j]; flist[j] = flist[j+gap]; flist[j+gap] = t;
  183.          }
  184. #endif /* DONT_SORT */
  185.  
  186.    fptr--;     /* fptr is now index of last item in array */
  187.  
  188.    /* Remove duplicates */
  189.    for (i = 0; i < fptr; i++) {
  190.       while (i<fptr && COMPARE(flist[i],flist[i+1]) == 0) {
  191.          for (j = i; j < fptr; j++)
  192.             flist[j] = flist[j+1];
  193.          fptr--;
  194.       }
  195.    }
  196.  
  197.    flist[++fptr] = NULL;      /* NULL entry terminates list */
  198. }
  199.  
  200. /*******
  201. modpath() makes any changes needed before pathname is stored;
  202. currently these could involve folding it to lower case and
  203. converting backslashes to forward slashes
  204. */
  205.  
  206. /*ARGUSED*/
  207. void modpath (path)
  208. char *path;
  209. {
  210. #ifdef FOLD
  211.     strlwr (path);
  212. #endif
  213.  
  214. #ifdef FORCESLASH
  215.     fixslash (path);                /* convert backslashes to slashes */
  216. #endif
  217. }
  218.  
  219. #ifdef CHEKDIR
  220. /* Function isfdir returns 1 if pathname is a directory else 0 */
  221. int isfdir (this_path)
  222. char *this_path;
  223. {
  224.     int dir;
  225.     ZOOFILE f;  
  226.     f = zooopen (this_path, Z_READ);
  227.     if (f == NOFILE)
  228.         return 0;
  229.     else {
  230.         dir = isadir(f);  zooclose(f);
  231.     }
  232.     return (dir);
  233. }
  234. #endif
  235.