home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / WHERE.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  129 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  WHERE.C:  will search all DIRs on the given drive for specified file.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <dos.h>
  10. #include <conio.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13.  
  14. #if defined(__ZTC__)
  15.  #include <direct.h>
  16.  #define GetDrive(d) dos_getdrive(&d)
  17.  #define SetDrive(d) {unsigned x;dos_setdrive(d,&x);}
  18. #elif defined(__TURBOC__)
  19.  #include <dir.h>
  20.  #define GetDrive(d) ((d) = getdisk() + 1)
  21.  #define SetDrive(d) (setdisk(d - 1))
  22. #else /* assume MSC */
  23.  #include <direct.h>
  24.  #define GetDrive(d) _dos_getdrive(&d)
  25.  #define SetDrive(d) {unsigned x;_dos_setdrive(d,&x);}
  26. #endif
  27.  
  28. #include "dirport.h"
  29.  
  30. int count=0;
  31.  
  32. main(int argc, char *argv[])
  33. {
  34.       char *curdir,
  35.             sought[80],
  36.            *temp;
  37.       int   newdrive, p;
  38.       void  searchdir(char *dir, char *ptrn);
  39.       unsigned curdrive;
  40.  
  41.       /*  Find out where we are */
  42.  
  43.       curdir=getcwd(NULL,80);
  44.       GetDrive(curdrive);
  45.  
  46.       /*  Find out what we're looking for  */
  47.  
  48.       if(argc>1)
  49.             strcpy(sought,argv[1]);
  50.       else
  51.       {
  52.             printf("\n\nPattern to search for: ");
  53.             gets(sought);
  54.       }
  55.  
  56.       /*  Get designator for another drive if specified  */
  57.  
  58.       if(sought[1]==':')
  59.       {
  60.             newdrive=(toupper(sought[0]))-64;       /* convert  */
  61.             SetDrive(newdrive);
  62.             p = (sought[2]=='\\') ? 3:2;
  63.             strcpy(sought, &(sought[p]));
  64.       }
  65.  
  66.       /*  Add wildcard prefix/suffix if necessary  */
  67.  
  68.       if(sought[0]=='.')
  69.       {
  70.             temp=strcat("*",sought);        /*  set prefix  */
  71.             strcpy(sought,temp);
  72.       }
  73.       if(!strchr(sought,'.'))
  74.             strcpy(sought,"*.*");           /*  set suffix  */
  75.  
  76.       /*  Perform search for pattern starting in root  */
  77.  
  78.       searchdir("\\",sought);
  79.       printf("\nNumber of matches: %d",count);
  80.  
  81.       /*  Restore Original Drive and Directory  */
  82.  
  83.       SetDrive(curdrive);
  84.       chdir(curdir);
  85.       return EXIT_SUCCESS;
  86. }
  87.  
  88. /*------------------------------------------------------------------------- */
  89.  
  90. void searchdir(char *path, char *ptrn)
  91. {
  92.       DOSFileData   *f;
  93.       char          *wholepath;
  94.       unsigned       rtn;
  95.  
  96.       chdir(path);                        /*  change to new path  */
  97.       wholepath = getcwd(NULL, 80);       /*  get full path name  */
  98.       f = malloc(sizeof(*f));
  99.  
  100.       /*  Search for filename matches in this directory  */
  101.  
  102.       rtn = FIND_FIRST(ptrn, _A_ANY, f);
  103.       while (rtn == 0)
  104.       {
  105.             if (!(ff_attr(f) & _A_SUBDIR ))
  106.                   printf("%s\\%s\n",wholepath,ff_name(f));
  107.             else  printf("%s\\%s <DIR>\n", wholepath, ff_name(f));
  108.             ++count;
  109.  
  110.             rtn = FIND_NEXT(f);           /* find next match      */
  111.       }  /* end while loop  */
  112.  
  113.       /*  Now search any subdirectories under this directory  */
  114.  
  115.       rtn = FIND_FIRST("*.*", _A_SUBDIR, f);
  116.       while (rtn == 0)
  117.       {
  118.             if ((ff_attr(f) & _A_SUBDIR) && (ff_name(f)[0] != '.'))
  119.             {
  120.                   searchdir(ff_name(f), ptrn);  /* recursive call */
  121.                   chdir(wholepath);
  122.             }
  123.             rtn = FIND_NEXT(f);           /* search next dir      */
  124.       }
  125.  
  126.       free(wholepath);
  127.       free(f);
  128. }
  129.