home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch09 / whereis.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-12  |  5.2 KB  |  178 lines

  1. /*
  2.     WHEREIS.C
  3.  
  4.     A file finder utility that searches the current drive, starting
  5.     with the root directory (\), for the specified pathname.
  6.     Wildcard characters can be included.
  7.  
  8.     Compile with:  C> cl whereis.c
  9.  
  10.     Usage is:  C> whereis pathname
  11.  
  12.     Copyright (C) 1988 Ray Duncan
  13. */
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18.  
  19. #define API unsigned extern far pascal
  20.  
  21. API DosChDir(char far *, unsigned long);
  22. API DosFindClose(unsigned);
  23. API DosFindFirst(char far *, unsigned far *, unsigned, void far *,
  24.                  int, int far *, unsigned long);
  25. API DosFindNext(unsigned, void far *, int, int far *);
  26. API DosQCurDisk(int far *, unsigned long far *);
  27. API DosQCurDir(int, void far *, int far *);
  28. API DosSelectDisk(int);
  29. API DosWrite(unsigned, void far *, unsigned, unsigned far *);
  30.  
  31. #define NORM      0x00                  /* file attribute bits */
  32. #define RD_ONLY   0x01
  33. #define HIDDEN    0x02
  34. #define SYSTEM    0x04
  35. #define DIR       0x10
  36. #define ARCHIVE   0x20
  37.  
  38. struct _srec {                          /* used by DosFindFirst */
  39.                     unsigned cdate;     /* and DosFindNext */
  40.                     unsigned ctime;
  41.                     unsigned adate;
  42.                     unsigned atime;
  43.                     unsigned wdate;
  44.                     unsigned wtime; 
  45.                     long fsize;
  46.                     long falloc;
  47.                     unsigned fattr;
  48.                     char fcount;
  49.                     char fname[13];
  50.              } 
  51.                 sbuf;                   /* receives search results */
  52.  
  53. int drvno;                              /* current drive */
  54. int count = 0;                          /* total files matched */
  55. char sname[80];                         /* target pathname */
  56.  
  57.  
  58. main(int argc, char *argv[])
  59. {
  60.     unsigned long drvmap;               /* logical drive map */
  61.  
  62.     if(argc < 2)                        /* exit if no parameters */
  63.     {
  64.         printf("\nwhereis: missing filename\n");
  65.         exit(1);
  66.     }
  67.  
  68.     DosQCurDisk(&drvno, &drvmap);       /* get current drive */
  69.  
  70.                                         /* any drive specified? */
  71.     if(((strlen(argv[1])) >= 2) && ((argv[1])[1] == ':'))
  72.     {
  73.                                         /* get binary drive code */
  74.         drvno = ((argv[1]) [0] | 0x20) - ('a'-1);
  75.  
  76.         if(DosSelectDisk(drvno))        /* select drive or exit */
  77.         {
  78.             printf("\nwhereis: bad drive\n");
  79.             exit(1);
  80.         }
  81.  
  82.         argv[1] += 2;                   /* advance past drive */
  83.     }
  84.  
  85.     strcpy(sname,argv[1]);              /* save search target */
  86.  
  87.     schdir("\\");                       /* start search with root */
  88.  
  89.                                         /* advise if no matches */
  90.     if(count == 0) printf("\nwhereis: no files\n");
  91. }
  92.  
  93.  
  94. /*
  95.     SCHDIR: search directory for matching files and
  96.             any other directories
  97. */
  98.  
  99. schdir(char *dirname)
  100. {
  101.     unsigned shan = -1;                 /* search handle */
  102.     int scnt = 1;                       /* max search matches */
  103.  
  104.     DosChDir(dirname, 0L);              /* select new directory */
  105.  
  106.     schfile();                          /* find and list files */
  107.  
  108.                                         /* search for directories */
  109.     if(!DosFindFirst("*.*", &shan, NORM|DIR, &sbuf, sizeof(sbuf), &scnt, 0L))
  110.     {
  111.         do                              /* if found directory other */
  112.         {                               /* than . and .. aliases */
  113.             if((sbuf.fattr & DIR) && (sbuf.fname[0] != '.'))
  114.             {
  115.                 schdir(sbuf.fname);     /* search the directory */
  116.                 DosChDir("..", 0L);     /* restore old directory */                         
  117.             }
  118.                                         /* look for more directories */
  119.         } while(DosFindNext(shan, &sbuf, sizeof(sbuf), &scnt) == 0);
  120.     }
  121.  
  122.     DosFindClose(shan);                 /* close search handle */
  123. }
  124.  
  125.  
  126. /*
  127.     SCHFILE: search current directory for files
  128.              matching string in 'sname'
  129. */
  130.  
  131. schfile()
  132. {
  133.     unsigned shan = -1;                 /* search handle */
  134.     int scnt = 1;                       /* max search matches */
  135.  
  136.     if(!DosFindFirst(sname, &shan, NORM, &sbuf, sizeof(sbuf), &scnt, 0L))
  137.     {
  138.         do pfile();                     /* list matching files */
  139.         while(DosFindNext(shan, &sbuf, sizeof(sbuf), &scnt) == 0);
  140.     }
  141.  
  142.     DosFindClose(shan);                 /* close search handle */
  143. }
  144.  
  145.  
  146. /*
  147.     PFILE: display current drive and directory, 
  148.            followed by filename from 'sbuf.fname'
  149. */
  150.  
  151. pfile()
  152. {
  153.     count++;                            /* count matched files */
  154.     pdir();                /* list drive and path */
  155.     printf("%s\n", strlwr(sbuf.fname)); /* list filename */ 
  156. }
  157.  
  158.  
  159. /*
  160.     PDIR: display current drive and directory
  161. */
  162.  
  163. pdir()
  164. {
  165.     char dbuf[80];            /* receives current dir */
  166.     int dlen = sizeof(dbuf);            /* length of buffer */
  167.  
  168.     DosQCurDir(0, dbuf, &dlen);         /* get current directory */
  169.  
  170.     if(strlen(dbuf) != 0)               /* add backslash to */
  171.         strcat(dbuf,"\\");              /* directory if not root */
  172.     
  173.                     /* display drive and path */
  174.     printf("%c:\\%s", drvno+'a'-1, strlwr(dbuf));
  175. }
  176.  
  177. 
  178.