home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / WHEREOS2.ZIP / WHEREOS2.C next >
C/C++ Source or Header  |  1988-09-30  |  4KB  |  149 lines

  1. /*
  2.  
  3.     Whereos2  File finder for OS/2
  4.  
  5. Written by:   Bob Whitten, P.O. Box DD, Tujunga, CA 91042
  6.     bix name:  bwhit;       Compuserve ID: 72220,2137
  7.  
  8. The code is hereby released to the public domain.  It is offered AS-IS,
  9. without any assurance that it will do anything useful or that it will
  10. not do something harmful.
  11. */
  12.  
  13. #define INCL_BASE
  14. #include <os2.h>
  15. #include <stdio.h>
  16.  
  17.  
  18. void dirlook(char *s);
  19.  
  20. #ifdef INCL_BASE
  21. unsigned int disknum;
  22. unsigned long far logdrv;
  23. unsigned int far pathlen =256;
  24. unsigned char far path[257];
  25. #endif
  26.  
  27. char dirtxt[133];
  28.  
  29. void main(int argc, char *argv[])
  30.     {
  31.     int ErrorCode;
  32.     int i, dotflag;
  33.     char ch, look4[15];
  34.  
  35.     if (argc != 2)
  36.     {
  37.     printf("whereos2 Usage: whereos2 <filename pattern>\n"
  38.         "   <filename pattern> may contain wildcards.\n"
  39.         "   If no '.' is given, one will be assigned, and will search\n"
  40.         "    for all extensions.\n\n"
  41.         "Brought to you by:  Bob Whitten, P.O. Box DD, Tujunga, CA 91042\n");
  42.     exit(0);
  43.     }
  44.     dotflag = 0;
  45.     for (i=0; i < 14; i++)
  46.     {
  47.     ch = argv[1][i];
  48.     look4[i] = ch;
  49.     if (ch == '.')
  50.         dotflag = 1;
  51.     }
  52.     if ( ! dotflag)
  53.     strcat(look4, ".*");
  54.     printf("Searching for pattern: for %s \n\n", look4);
  55.  
  56. #ifdef INCL_BASE
  57.     ErrorCode = DosQCurDisk(&disknum, &logdrv);
  58.     if (ErrorCode) DosExit(1, ErrorCode);
  59.     ErrorCode = DosQCurDir(0, path, &pathlen);      /* save this for later */
  60.     if (ErrorCode) DosExit(1, ErrorCode);
  61. #endif
  62.     dirtxt[0] = '\\';        /* simply, the root */
  63.     dirtxt[1] = '\0';
  64.     ErrorCode = DosChdir( dirtxt, 0L);        /* change to the root */
  65.     if (ErrorCode) DosExit(1, ErrorCode);
  66.  
  67.     dirlook(look4);
  68.  
  69.     ErrorCode = DosChdir(path,0L);
  70.     if (ErrorCode) DosExit(1, ErrorCode);
  71.     }
  72.  
  73. char mons[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  74.          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  75.  
  76. void dirlook(char *fname)
  77.     {
  78.     int i;
  79.     unsigned int ErrorCode;
  80.     FILEFINDBUF ffbuf;
  81.     unsigned short searchndl = 0xffff;
  82.     unsigned int attr = 0x0037;     /* normal and dir files */
  83.     unsigned int searchcnt = 1; /* just get 1, */
  84.     int savdirtxtl;
  85.  
  86.  /*   FIRST LOOK FOR "fname" MATCHES */
  87.  
  88.     ErrorCode = DosFindFirst( fname, &searchndl, attr,
  89.         &ffbuf, sizeof(ffbuf),
  90.         &searchcnt, 0L);
  91.     if (ErrorCode == ERROR_NO_MORE_FILES)
  92.     goto SEARCHDIR;     /* a "goto" is sometimes considered
  93.                    bad form, but it seemed appropriate here */
  94.     else if (ErrorCode != 0)
  95.      DosExit(1, ErrorCode);
  96.     while (searchcnt)
  97.     {
  98.     if (ffbuf.attrFile & 0x10)
  99.         printf("%s%s  <DIR>\n", dirtxt, ffbuf.achName);
  100.     else
  101.         printf("%s%s   %ld  %d-%3s-%02d \n",
  102.            dirtxt, ffbuf.achName, ffbuf.cbFile,
  103.            ffbuf.fdateLastWrite.day, mons[ffbuf.fdateLastWrite.month],
  104.            (ffbuf.fdateLastWrite.year+80) % 100);
  105.     ErrorCode = DosFindNext(searchndl, &ffbuf, sizeof(ffbuf), &searchcnt);
  106.     if ((ErrorCode != ERROR_NO_MORE_FILES) && (ErrorCode != 0))
  107.          DosExit(1, ErrorCode);
  108.     }
  109.     ErrorCode = DosFindClose(searchndl);
  110.     if (ErrorCode != 0)
  111.     DosExit(1, ErrorCode);
  112.  
  113.  
  114. /*  NOW LOOK FOR DIRECTORIES, (except "." and "..")  */
  115.  
  116. SEARCHDIR:
  117.     searchcnt = 1;
  118.     searchndl = -1;
  119.     ErrorCode = DosFindFirst( "*.*", &searchndl, attr,
  120.         &ffbuf, sizeof(ffbuf),
  121.         &searchcnt, 0L);
  122.     if (ErrorCode == ERROR_NO_MORE_FILES)
  123.     return;
  124.     else if (ErrorCode != 0)
  125.      DosExit(1, ErrorCode);
  126.     while (searchcnt)
  127.     {
  128.     if ((ffbuf.attrFile & 0x10) && (ffbuf.achName[0] != '.'))      /* check for directory */
  129.         {
  130.         ErrorCode = ChDir(ffbuf.achName, 0L);
  131.         if (ErrorCode)
  132.         DosExit(1, ErrorCode);
  133.         savdirtxtl = strlen(dirtxt);
  134.         strcat(dirtxt, ffbuf.achName);
  135.         strcat(dirtxt, "\\");
  136.         dirlook(fname);        /* go for it, recursively */
  137.         dirtxt[savdirtxtl] = '\0';
  138.         ErrorCode = ChDir("..", 0L);
  139.         }
  140.     ErrorCode = DosFindNext(searchndl, &ffbuf, sizeof(ffbuf), &searchcnt);
  141.     if ((ErrorCode != ERROR_NO_MORE_FILES) && (ErrorCode != 0))
  142.          DosExit(1, ErrorCode);
  143.     }
  144.     ErrorCode = DosFindClose(searchndl);
  145.     if (ErrorCode != 0)
  146.     DosExit(1, ErrorCode);
  147.  
  148.     }
  149.