home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_02 / 1002092a < prev    next >
Text File  |  1991-01-14  |  5KB  |  112 lines

  1.  
  2.  
  3.  
  4. /*
  5.         A recursive descent main to search directories and subs
  6.         with wildcard and path capabilities -> then pass the
  7.         path name to a user supplied function called "subfunc".
  8.         2 switches are presently supported: /t for totals and
  9.         /s for subdirectories.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <malloc.h>
  15. #include <string.h>
  16. #include <dos.h>
  17.  
  18. #define MAX_SUB 128             /* max subdirectory width for stack */
  19.                                                 /* subfunc is user module to link to */
  20. extern  void subfunc(char *path);
  21.  
  22. int main(int argc, char **argv)
  23. {
  24. struct find_t *fib;
  25. char *path, *drive, *dir, *fname, *ext;
  26. char *tmppath, *tmpdir, *tmpargv, *subfifo;
  27. char (*subfifoptr)[_MAX_FNAME];
  28. int total = 0;
  29.                                 /* malloc so we don't blow up the stack */
  30. if (!((fib = malloc(sizeof(struct find_t))) &&
  31.                         (path = malloc(_MAX_PATH)) &&
  32.                         (drive = malloc(_MAX_DRIVE)) &&
  33.                         (dir = malloc(_MAX_DIR)) &&
  34.                         (fname = malloc(_MAX_FNAME)) &&
  35.                         (ext = malloc(_MAX_EXT)) &&
  36.                         (tmpdir = malloc(_MAX_DIR)) &&
  37.                         (tmppath = malloc(_MAX_PATH))))
  38.         {                       /* return resources to DOS */
  39.         free(fib), free(path), free(drive), free(dir);
  40.         free(fname), free(ext), free(tmpdir), free(tmppath);
  41.         printf("NOT ABLE TO MALLOC SPACE!\N");
  42.         exit(-1);
  43.         }
  44.  
  45. if (argc < 2)
  46.         {                       /* there was no command line argument */
  47.         _splitpath(argv[0], drive, dir, fname, ext);
  48.         printf("\n\t%s <filespec> [/s/t]\n", fname);
  49.         printf("\tFilespec can have DOS wildcards!\n");
  50.         printf("\t/S switch includes subdirectories.\n");
  51.         printf("\t/T switch gives a total.\n");
  52.         }
  53. else
  54.         {
  55.         if (argc > 2 && (strstr(strupr(argv[2]), "/S")))
  56.                 {               /* if S switch - do subdirectories first */
  57.                 tmpargv = argv[1];      /* save argv[1] for future use */
  58.                 if (!(subfifo = *subfifoptr =
  59.                                 (char *)malloc(_MAX_FNAME * MAX_SUB)))
  60.                         {               /* return resources to DOS */
  61.                         free(fib), free(path), free(drive), free(dir);
  62.                         free(fname), free(ext), free(tmpdir), free(tmppath);
  63.                         printf("UNABLE TO MALLOC FOR INTERNAL FIFO!\N");
  64.                         exit(-1);
  65.                         }
  66.                 _splitpath(argv[1], drive, dir, fname, ext);
  67.                 _makepath(path, drive, dir, "*", "*");
  68.                 if (!_dos_findfirst(path, _A_SUBDIR, fib))
  69.                         do
  70.                                 {
  71.                                 if (fib->name[0] != '.' &&
  72.                                                  fib->attrib & _A_SUBDIR)
  73.                                                                 /* push on FIFO */
  74.                                         strcpy(*subfifoptr++, fib->name);
  75.                                 } while (!_dos_findnext(fib) &&
  76.                                                 *subfifoptr <
  77.                                                 &subfifo[(_MAX_FNAME - 1) * MAX_SUB]);
  78.                 **subfifoptr = NULL;            /* terminate FIFO */
  79.                 *subfifoptr = subfifo;  /* reset FIFO pointer */
  80.                 while(**subfifoptr)             /* while not at the end */
  81.                         {
  82.                         strcpy(tmpdir, dir);
  83.                         _makepath(path, drive, 
  84.                                         strcat(tmpdir, *subfifoptr++), fname, ext);
  85.                         argv[1] = path;
  86.                                                                 /* recursive part of program */
  87.                         total += main(argc, argv);      /* check next level */
  88.                         }
  89.                 free(subfifo);
  90.                 argv[1] = tmpargv;                              /* restore argv[1] */
  91.                 }
  92.                                                                                 /* look for files */
  93.         if (!_dos_findfirst(argv[1], _A_NORMAL, fib))
  94.                 do
  95.                         {
  96.                         _splitpath(argv[1], drive, dir, fname, ext);
  97.                         strcpy(tmppath, drive);
  98.                         strcat(tmppath, dir);
  99.                                                         /* now call the work function */
  100.                         subfunc(strcat(tmppath, fib->name));
  101.                         total++;
  102.                         } while (!_dos_findnext(fib));
  103.         if (argc > 2 && strstr(strupr(argv[2]), "/T"))
  104.                 printf("\ntotal = %d\t%s files\n", total, argv[1]);
  105.         }
  106.                 /* return resources to DOS */
  107. free(fib), free(path), free(drive), free(dir);
  108. free(fname), free(ext), free(tmpdir), free(tmppath);
  109.  
  110. return total;
  111. }
  112.