home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / zoo141_c.lzh / MAKELIST.C < prev    next >
C/C++ Source or Header  |  1987-02-07  |  6KB  |  191 lines

  1. /* makelist.c */
  2. /*
  3. Copyright (C) 1986 Rahul Dhesi -- All rights reserved
  4. */
  5.  
  6. #include "options.h"
  7. #include "portable.h"
  8.  
  9. /* for low-level I/O */
  10. #ifdef NOFCNTL
  11. #include <file.h>
  12. #else
  13. #include <fcntl.h>
  14. #endif
  15.  
  16. #ifdef FLAT
  17. #include <types.h>
  18. #include <stat.h>
  19. #else
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #endif
  23.  
  24. #include "zoo.h"
  25. #include <stdio.h>
  26. #include "various.h"
  27.  
  28. #include "zoofns.h"
  29. #include "assert.h"
  30. #include "debug.h"
  31.  
  32. #ifdef LINT_ARGS
  33. char *nameptr (char *);
  34. #else
  35. char *nameptr();
  36. #endif
  37.  
  38. /*******************/
  39. /*
  40. makelist() gets all pathnames corresponding to a set of filespecs and
  41. adds them to a list.  Not more than "flistsize" pathnames are added.
  42. Into `longest' it returns the length of the longest name added, or
  43. zero if none added.
  44. */
  45.  
  46. void makelist (argc, argv, flist, flistsize, ignore1, ignore2, ignore3, longest)
  47. int argc;               /* number of filespec supplied */
  48. char *argv[];           /* array of pointers to supplied filespecs */
  49. register char *flist[]; /* array of pointers to filenames we will add */
  50. int flistsize;          /* home many names we can use */
  51. char *ignore1, *ignore2, *ignore3; /* files to exclude from list */
  52. int *longest;        /* length of longest name in list */
  53. {
  54.    char *this_path;        /* current pathname */
  55.    int fptr;               /* pointer to within flist */
  56.    register int i, j;      /* loop counters */
  57.    char *pat_name;         /* filename part of pattern */
  58.    int range;              /* testing character range? */
  59.    int gap;                /* for Shell sort */
  60.    
  61.    flistsize--;            /* allow for one terminating NULL entry */
  62.    fptr = *longest = 0;
  63.  
  64.    assert(argc > 0);
  65.  
  66.    while (argc > 0) {
  67.       char *this_arg = *argv;
  68. #ifndef PORTABLE
  69.       char *temp = lastptr (this_arg);
  70.       char lastch = *temp;
  71. #endif
  72.       assert(*lastptr (this_arg) != NULL);
  73. #ifdef PORTABLE
  74.       /* nothing */
  75. #else
  76.       if (strchr(PATH_SEP, lastch) != NULL)
  77.          this_arg = newcat (this_arg, "*.*");
  78. #endif
  79.  
  80.       debug((printf ("makelist:  this_arg = [%s].\n", this_arg)))
  81.  
  82.       /* initialize fileset 0.  If char range specified, select all
  83.       files -- match_half will filter out the ones wanted */
  84. #ifdef FOLD
  85.       strlwr (this_arg);
  86. #endif
  87.       pat_name = strdup (nameptr (this_arg));
  88.  
  89. #ifdef PORTABLE
  90.       /* nothing */
  91. #else
  92.       if (range = match_half (pat_name, "?-?")) {  /* "=" intentional */
  93.          strcpy (nameptr (this_arg), "*.*"); /* change "?-?" to "*.*" */
  94.       }
  95.       /* note: when this_arg goes from "?-?" to "*.*", pat_name remains
  96.       unchanged. */
  97. #endif
  98.  
  99.       nextfile (0, this_arg, 0);
  100.       while (fptr < flistsize && 
  101.             (this_path = nextfile(1, NULL, 0)) != NULL) {
  102.          char *this_name = nameptr (this_path);
  103. #ifdef FOLD
  104.          strlwr (this_path);
  105. #endif
  106.  
  107. #ifdef FORCESLASH
  108.    fixslash (this_path);               /* convert backslashes to slashes */
  109. #endif
  110.  
  111.          debug((printf ("makelist:  this_path=[%s] this_name=[%s] pat_name=[%s].\n", 
  112.                                     this_path, this_name, pat_name)))
  113.  
  114. #ifdef IGNORECASE
  115.          if (!strcmpi(this_name,ignore1)  ||    /* exclude ignored files */
  116.              !strcmpi(this_name,ignore2)  ||
  117.              !strcmpi(this_name,ignore3))
  118.             continue;
  119. #else
  120.          if (!strcmp(this_name,ignore1)  ||    /* exclude ignored files */
  121.              !strcmp(this_name,ignore2)  ||
  122.              !strcmp(this_name,ignore3))
  123.             continue;
  124. #endif
  125.  
  126. #ifdef CHEKDIR
  127.       {
  128.          int skip, han;  
  129.          han=OPEN(this_name, F_READ);
  130.          skip = isadir(han);  close(han);
  131.          if (skip)
  132.             continue;
  133.       }
  134. #endif
  135.       
  136. #ifdef CHEKUDIR
  137.          if (isuadir(this_name))
  138.             continue;
  139. #endif
  140.  
  141.          if (match (this_name,pat_name) || range && 
  142.                *this_name >= *pat_name && *this_name <= pat_name[2]) {
  143.             flist[fptr++] = strdup (this_path);
  144.          if (*longest < strlen(this_path))
  145.             *longest = strlen(this_path);
  146.             debug((printf ("makelist:  [%s] added to list.\n", this_path)))
  147.          }
  148.  
  149.       }
  150.       argc--;
  151.       argv++;
  152.    }
  153.    /* fptr is now 1 + index of last item in array */
  154.  
  155.    if (this_path != NULL && fptr >= flistsize)
  156.       prterror ('w', "Some filenames ignored -- can only handle %d.\n",
  157.                                                    flistsize);
  158. #ifndef  DONT_SORT
  159.    /* Shell sort -- K&R p. 58 */
  160.    for (gap = fptr/2; gap > 0; gap /= 2)
  161.       for (i = gap; i < fptr; i++)
  162.          for (j = i - gap; j >= 0 && 
  163.             strcmp(flist[j],flist[j+gap]) > 0; j -= gap) {
  164. /* strcmp(nameptr(flist[j]),nameptr(flist[j+gap])) > 0; j -= gap) */
  165.             char *t = flist[j]; flist[j] = flist[j+gap]; flist[j+gap] = t;
  166.          }
  167. #endif /* DONT_SORT */
  168.  
  169.    fptr--;     /* fptr is now index of last item in array */
  170.  
  171.    /* Remove duplicates */
  172.    for (i = 0; i < fptr; i++) {
  173.       debug((printf ("duplicate test:  [%s] and [%s].\n",
  174.                         nameptr(flist[i]), nameptr(flist[i+1]))))
  175. #ifdef IGNORECASE
  176. /* while (i<fptr && strcmpi (nameptr(flist[i]),nameptr(flist[i+1])) == 0) */
  177.       while (i<fptr && strcmpi (flist[i],flist[i+1]) == 0) {
  178. #else
  179. /* while (i<fptr && strcmp  (nameptr(flist[i]),nameptr(flist[i+1])) == 0) */
  180.       while (i<fptr && strcmp (flist[i],flist[i+1]) == 0) {
  181. #endif
  182.          for (j = i; j < fptr; j++)
  183.             flist[j] = flist[j+1];
  184.          fptr--;
  185.       }
  186.    }
  187.  
  188.    flist[++fptr] = NULL;      /* NULL entry terminates list */
  189. }
  190.  
  191.