home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / WHERE.C < prev    next >
C/C++ Source or Header  |  1991-07-19  |  3KB  |  110 lines

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