home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / ff2b.zip / FF.C next >
C/C++ Source or Header  |  1991-06-27  |  7KB  |  210 lines

  1. /*$TITLE 'File Find Tool FF'*/
  2. /*──────────────────────────────────────────────────────────────────*/
  3. /*  FF.C              (c) B. Kunrath 1989, 1990, 1991               */
  4. /*                    Modified for dual mode by D. Plum 1991        */
  5. /*  Function:                                                       */
  6. /*  - searches for matching files on an entire disk.                */
  7. /*                                                                  */
  8. /*  Tools used:                                                     */
  9. /*  - IBM OS/2  1.3 Extended Edition                                */
  10. /*  - IBM OS/2  Programming Tools and Information 1.3               */
  11. /*  - Microsoft C 6.0                                               */
  12. /*                                                                  */
  13. /*  Used API's:                                                     */
  14. /*      DosQCurDisk                                                 */
  15. /*      DosSelectDisk                                               */
  16. /*      DosFindFirst                                                */
  17. /*      DosFindNext                                                 */
  18. /*      DosFindCLose                                                */
  19. /*                                                                  */
  20. /*──────────────────────────────────────────────────────────────────*/
  21.  
  22. #define INCL_BASE
  23. #define INCL_DOSDEVICES
  24. #include <os2.h>
  25. #include <conio.h>
  26. #include <ctype.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <process.h>
  31. #include <direct.h>
  32. #include <malloc.h>
  33.  
  34. #define WILDCARD_LEN        15
  35. #define ATTR_READ_ONLY      0x01
  36. #define ATTR_HIDDEN_FILES   0x02
  37. #define ATTR_SYSTEM_FILES   0x04
  38. #define ATTR_INCL_SUBDIRS   0x10
  39. #define ATTR_INCL_ARCHIVE   0x20
  40.  
  41. UCHAR cwd[256],oldCwd[255];
  42. USHORT searchHandle = 1;
  43. USHORT searchCnt = 1;
  44. FILEFINDBUF ffb;
  45.  
  46. UCHAR  wildCard[WILDCARD_LEN+1];
  47. USHORT driveNr;
  48. ULONG  driveMap;
  49. BOOL   printed = FALSE;
  50.  
  51. void ExecHead(void);
  52. void ExecLine(void);
  53. void FileFind(void);
  54.  
  55. int main (int argc, char *argv[])
  56. {
  57.     DosQCurDisk(&driveNr, &driveMap);
  58.     if (oldCwd[strlen(getcwd(oldCwd, sizeof(oldCwd) - 1)) - 1] == '\\')
  59.       oldCwd[strlen(oldCwd) - 1] = 0;
  60.  
  61.     if ((argc < 2) || (strlen(argv[1]) > WILDCARD_LEN))
  62.     {
  63.         printf("FileFind 1.01  Copyright ╕ B. Kunrath, Fa. OBIS GmbH 1989, 1990, 1991\n");
  64.         printf("Usage: ff [Drive]<Filename>\n");
  65.         printf("       Drive    - The disk drive to be searched\n");
  66.         printf("       Filename - The filename to be searched for, including\n");
  67.         printf("                  wildcards if desried, but excluding pathname.\n");
  68.         printf("Examples:\n");
  69.         printf("       ff a:\\*.c       - find all files matching *.c on drive a:\n");
  70.         printf("       ff hennes.idx   - find hennes.idx on the current drive\n");
  71.         printf("Exit(%d)...\n", ERROR_INVALID_PARAMETER);
  72.         exit(ERROR_INVALID_PARAMETER);
  73.     }
  74.     else
  75.     {
  76.         strcpy(wildCard, argv[1]);
  77.         printf("Searching for '%s'\n", wildCard );
  78.     };
  79.  
  80.     if (argv[1][1] == ':' && isalpha(argv[1][0]))
  81.     {
  82.         DosSelectDisk (toupper(argv[1][0]) - 0x40);
  83.         if (wildCard[2]=='\\')
  84.             strcpy(wildCard, &wildCard[3]);
  85.         else
  86.             strcpy(wildCard, &wildCard[2]);
  87.     }
  88.     else
  89.         if (wildCard[0]=='\\')
  90.             strcpy(wildCard, &wildCard[1]);
  91.  
  92.     chdir("\\");
  93.  
  94.     FileFind();
  95.     if (!printed)
  96.         printf("No matching files found.\n");
  97.  
  98.     DosSelectDisk (driveNr);
  99.     chdir(oldCwd);
  100.     return 0;
  101. }
  102.  
  103. void ExecHead(void)
  104. {
  105.     printf("    Size   Date    Time  Pathname\n");
  106.     printed = TRUE;
  107.     return;
  108. }
  109.  
  110. void ExecLine(void)
  111. {
  112.     if (!printed)
  113.         ExecHead();
  114.     printf("%8ld %2d/%02d/%02d %2d:%02d  %s\\%s\n",
  115.            ffb.cbFile,
  116.            ffb.fdateLastWrite.month,
  117.            ffb.fdateLastWrite.day,
  118.            (ffb.fdateLastWrite.year + 80) % 100,
  119.            ffb.ftimeLastWrite.hours,
  120.            ffb.ftimeLastWrite.minutes,
  121.            cwd, ffb.achName);
  122.     return;
  123. }
  124.  
  125. void FileFind(void)
  126. {
  127.     struct DIRNAMESAVE
  128.         {
  129.         struct DIRNAMESAVE *next;
  130.         UCHAR name[sizeof ffb.achName];
  131.         } *first = NULL, *last = NULL, *new = NULL;
  132.  
  133.     if (cwd[strlen(getcwd(cwd, sizeof(cwd) - 1)) - 1] == '\\')
  134.       cwd[strlen(cwd) - 1] = 0;
  135.  
  136.     searchHandle = 1;
  137.     searchCnt = 1;
  138.     DosFindFirst( wildCard,                 /*  The file specification  */
  139.                   &searchHandle,            /*  handle -> return        */
  140.                   ATTR_READ_ONLY |          /*  Attributes of files     */
  141.                   ATTR_HIDDEN_FILES |
  142.                   ATTR_SYSTEM_FILES ,
  143.                   &ffb,                     /*  Info. Buffer            */
  144.                   sizeof( FILEFINDBUF ),    /*  size of Info. Buffer    */
  145.                   &searchCnt,               /*  How many to search ?    */
  146.                   0L );
  147.  
  148.     while (searchCnt)
  149.     {
  150.         ExecLine();
  151.         DosFindNext( searchHandle,
  152.                      &ffb,
  153.                      sizeof( FILEFINDBUF ),
  154.                      &searchCnt);
  155.     }
  156.  
  157.     DosFindClose( searchHandle );
  158.  
  159.     searchHandle = 1;
  160.     searchCnt = 1;
  161.     DosFindFirst( "*.*",                    /*  The file specification  */
  162.                  &searchHandle,             /*  handle -> return        */
  163.                  ATTR_READ_ONLY |           /*  Attributes of files     */
  164.                  ATTR_HIDDEN_FILES |
  165.                  ATTR_SYSTEM_FILES |
  166.                  ATTR_INCL_SUBDIRS,
  167.                  &ffb,                      /*  Info. Buffer            */
  168.                  sizeof( FILEFINDBUF ),     /*  size of Info. Buffer    */
  169.                  &searchCnt,                /*  How many to search ?    */
  170.                  0L );                      /*  reserved                */
  171.  
  172.     while (searchCnt)
  173.     {
  174.       if (ffb.attrFile & 0x10 && ffb.achName[0] != '.')
  175.         if((new = malloc(sizeof(struct DIRNAMESAVE))) != NULL)
  176.         {
  177.           new->next = NULL;
  178.           strcpy(new->name, ffb.achName);
  179.           if(first == NULL)
  180.             first = new;
  181.           else
  182.             last->next = new;
  183.           last = new;
  184.          }
  185.         else
  186.         {
  187.           printf("Insufficient memory.\n");
  188.           exit(1);
  189.         }
  190.       DosFindNext( searchHandle,
  191.                    &ffb,
  192.                    sizeof( FILEFINDBUF ),
  193.                    &searchCnt);
  194.     }
  195.  
  196.     DosFindClose( searchHandle );
  197.  
  198.     while(first != NULL)
  199.       {
  200.         chdir(new->name);
  201.         FileFind();
  202.         chdir("..");
  203.         new = first;
  204.         first = first->next;
  205.         free(new);
  206.       }
  207.  
  208. };
  209.  
  210.