home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 376_01 / os2tool.000 / DIRSCAN.C < prev    next >
C/C++ Source or Header  |  1992-07-04  |  7KB  |  293 lines

  1. /*
  2. * DIRSCAN.C -        General directory scanner function.
  3. *
  4. *
  5. * PROGRAMMER:        Martti Ylikoski
  6. * CREATED:        30.10.1991
  7. * VERSION:        1.0
  8. *
  9. */
  10. //#undef MSDOS
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <direct.h>
  15. #include <errno.h>
  16. #ifdef MSDOS
  17. #include <dos.h>
  18. #include <errno.h>
  19. #define TRUE 1
  20. #define FALSE 0
  21. #endif
  22. #ifndef  MSDOS
  23. #include <os2.h>
  24. #endif
  25. #include "dirscan.h"
  26.  
  27. /*==================================================
  28. *
  29. *  Search for a file.
  30. *
  31. *=================================================*/
  32. #ifdef MSDOS
  33. int DirScan( int fcount, char *fname[], unsigned findattr,    int no_default, int (*DirInitFn) (char *),
  34.     int (*DirExitFn)(char *),int (* FileHandlerFn)(char *), int DownCnt, int sc_flags )
  35. {
  36.    int           status, j, k, ret ;
  37.    char        curdir[80], buf[100], sysbuf[80], lastdir[80] ;
  38.  
  39.    int           curdrive ;
  40.    int           i ;
  41.    struct find_t    findbuf ;
  42.  
  43. //   curdrive = _getdrive() ;
  44.    getcwd(curdir, (int) sizeof(curdir)) ;
  45.  
  46. //   buf[0] = 'A' + curdrive -1 ;
  47. //   strcpy(&buf[1], ":\\") ;
  48. //   strcat(buf,curdir) ;
  49.  
  50.      strcpy(buf,curdir) ;
  51.  
  52. //   if (curdirlen != 0) /* not in the main directory */
  53.     if (curdir[strlen(curdir) -1] != '\\')
  54.     strcat(buf,"\\") ;
  55.  
  56.    if (DirInitFn != NULL)
  57.       DirInitFn(buf) ;
  58.  
  59.    for(j = 0 ; j < fcount ; j++) /* scan through all file specifiers */
  60.    {
  61.  
  62.       if (( status = _dos_findfirst(fname[j], findattr, &findbuf)) == 0)
  63.       {
  64.         if ( no_default == FALSE || (no_default == TRUE && (findattr & findbuf.attrib) != 0))
  65.         {
  66.            strcpy(sysbuf, buf) ;
  67.            strcat(sysbuf,findbuf.name) ;
  68.            if ((ret = FileHandlerFn(sysbuf)) != 0)
  69.           return( ret )    ;
  70.         }
  71.  
  72.      do
  73.      {
  74.         if (( status = _dos_findnext(&findbuf)) == 0)
  75.         {
  76.            if ( no_default == FALSE || (no_default == TRUE && (findattr & findbuf.attrib) != 0))
  77.            {
  78.           strcpy(sysbuf, buf) ;
  79.           strcat(sysbuf,findbuf.name) ;
  80.           if ((ret = FileHandlerFn(sysbuf)) != 0)
  81.              return( ret ) ;
  82.          }
  83.         }
  84.      } while (status == 0) ;
  85.       }
  86.    } /* all file specifiers */
  87.    /* span to subdirectories */
  88.    if (DownCnt == 0)
  89.    {
  90.       if (DirExitFn != NULL)
  91.     DirExitFn(buf) ;
  92.       return( 0 ) ;
  93.    }
  94.  
  95.    if (sc_flags & SC_BEFORESUBDIRS && DirExitFn != NULL)
  96.       DirExitFn(buf) ;
  97.  
  98.    DownCnt -- ;
  99.    if (( status = _dos_findfirst("*.*", _A_SUBDIR, &findbuf)) != 0)
  100.    {
  101.       return( 0 ) ; /* no subdirectory */
  102.    }
  103.  
  104.    if ( findbuf.name[0] != '.'    &&
  105.     (findbuf.attrib & _A_SUBDIR)) /* only for root */
  106.    {
  107.       chdir (findbuf.name) ;
  108.       if ((ret = DirScan(fcount, &fname[0], findattr, no_default, DirInitFn, DirExitFn, FileHandlerFn, DownCnt, sc_flags)) != 0)
  109.      return( 1 ) ;
  110.       chdir("..") ;
  111.     }
  112.  
  113.  
  114.    do
  115.    {
  116.       if (( status = _dos_findnext(&findbuf)) == 0)
  117.       {
  118.         if ( findbuf.name[0] != '.' &&
  119.            (findbuf.attrib & _A_SUBDIR))
  120.         {
  121.            chdir (findbuf.name) ;
  122.            if ((ret = DirScan(fcount, &fname[0], findattr, no_default, DirInitFn, DirExitFn, FileHandlerFn, DownCnt, sc_flags)) != 0)
  123.           return( 1 ) ;
  124.            chdir("..") ;
  125.         }
  126.       }
  127.    } while (status == 0) ;
  128.  
  129.    if (sc_flags & SC_AFTERSUBDIRS && DirExitFn != NULL)
  130.       DirExitFn(buf) ;
  131.  
  132.    return( 0 ) ;
  133. }
  134. #endif
  135. #ifndef MSDOS
  136. int DirScan( int fcount, char *fname[], USHORT findattr,  int no_default, int (*DirInitFn) (char *),
  137.     int (*DirExitFn)(char *),int (* FileHandlerFn)(char *), int DownCnt, int sc_flags )
  138. {
  139.    int           status, j, k, ret ;
  140.    char        curdir[80], buf[100], sysbuf[80] ;
  141.    USHORT      curdirlen ;
  142.    USHORT      curdrive, i ;
  143.    ULONG       tmp ;
  144.  
  145.    HDIR    hdir;
  146.    USHORT      SearchCount;
  147.    FILEFINDBUF findbuf[1];
  148.  
  149.    DosQCurDisk (&curdrive, &tmp) ;
  150.    curdirlen = sizeof(curdir) ;
  151.    DosQCurDir(curdrive, curdir, &curdirlen ) ;
  152.  
  153.    buf[0] = 'A' + curdrive -1 ;
  154.    strcpy(&buf[1], ":\\") ;
  155.    strcat(buf,curdir) ;
  156.  
  157. //   if (curdirlen != 0) /* not in the main directory */
  158.     if (curdir[0] != '\0')
  159.       strcat(buf,"\\") ;
  160.  
  161.    if (DirInitFn != NULL)
  162.       DirInitFn(buf) ;
  163.  
  164.    for(j = 0 ; j < fcount ; j++) /* scan through all file specifiers */
  165.    {
  166.       SearchCount = 1 ; //(int) sizeof(findbuf)/sizeof(findbuf[0]) ;
  167.       hdir = HDIR_CREATE;
  168.      
  169.       /* search for files */
  170.       if ( (status = DosFindFirst2(fname[j],
  171.               &hdir,
  172.               findattr,
  173.               &findbuf[0],
  174.               sizeof(findbuf),
  175.               &SearchCount,
  176.               FIL_STANDARD,
  177.               0L)) == 0)
  178.       {
  179.      for (i = 0 ; i < SearchCount; i++)
  180.      {
  181.         if ( no_default == FALSE || (no_default == TRUE && (findattr & findbuf[i].attrFile) != 0))
  182.         {
  183.            strcpy(sysbuf, buf) ;
  184.            strcat(sysbuf,findbuf[i].achName) ;
  185.            if ((ret = FileHandlerFn(sysbuf)) != 0)
  186.            {
  187.                   DosFindClose(hdir) ;
  188.           return( ret )    ;
  189.            }
  190.         }
  191.  
  192.      }
  193.  
  194.      do
  195.      {
  196.         if ((status = DosFindNext(hdir,
  197.                   &findbuf[0],
  198.                               sizeof(findbuf),
  199.                   &SearchCount)) == 0)
  200.         {
  201.           for (i = 0 ; i < SearchCount; i++)
  202.           {
  203.          if ( no_default == FALSE || (no_default == TRUE && (findattr & findbuf[i].attrFile) != 0))
  204.          {
  205.             strcpy(sysbuf, buf) ;
  206.             strcat(sysbuf,findbuf[i].achName) ;
  207.             if ((ret = FileHandlerFn(sysbuf)) != 0)
  208.             {
  209.                        DosFindClose(hdir) ;
  210.                return( ret ) ;
  211.             }
  212.          }
  213.            }
  214.         }
  215.      } while (status == 0) ;
  216.       }
  217.       DosFindClose(hdir) ;
  218.    } /* all file specifiers */
  219.    /* span to subdirectories */
  220.    if (DownCnt == 0)
  221.    {
  222.       if (DirExitFn != NULL)
  223.     DirExitFn(buf) ;
  224.       return( 0 ) ;
  225.    }
  226.  
  227.    if (sc_flags & SC_BEFORESUBDIRS && DirExitFn != NULL)
  228.       DirExitFn(buf) ;
  229.  
  230.    DownCnt -- ;
  231.  
  232.    hdir = HDIR_CREATE;
  233.    SearchCount = 1 ; // (int) sizeof(findbuf)/sizeof(findbuf[0]) ;
  234.    if ( (status = DosFindFirst2("*.*",
  235.               &hdir,
  236.               FILE_DIRECTORY,
  237.               &findbuf[0],
  238.               sizeof(findbuf),
  239.               &SearchCount,
  240.               FIL_STANDARD,
  241.               0L)) != 0)
  242.    {
  243.       DosFindClose(hdir) ;
  244.       return( 0 ) ; /* no subdirectory */
  245.    }
  246.  
  247.    for( i = 0; i < SearchCount; i++)
  248.    {
  249.       if ( findbuf[i].achName[0] != '.'    &&
  250.     (findbuf[i].attrFile & FILE_DIRECTORY) ) /* only for root */
  251.       {
  252.      chdir (findbuf[i].achName) ;
  253.      if ((ret = DirScan(fcount, &fname[0], findattr, no_default, DirInitFn, DirExitFn, FileHandlerFn, DownCnt, sc_flags)) != 0)
  254.      {
  255.         DosFindClose(hdir) ;
  256.         return( 1 ) ;
  257.      }
  258.      chdir("..") ;
  259.        }
  260.    }
  261.  
  262.    do
  263.    {
  264.       if ((status = DosFindNext(hdir,
  265.                   &findbuf[0],
  266.                   sizeof(findbuf),
  267.                   &SearchCount)) == 0)
  268.       {
  269.      for (i=0 ; i < SearchCount; i++)
  270.      {
  271.         if ( findbuf[i].achName[0] != '.' &&
  272.            (findbuf[i].attrFile & FILE_DIRECTORY))
  273.         {
  274.            chdir (findbuf[i].achName) ;
  275.            if(( ret = DirScan(fcount, &fname[0], findattr, no_default, DirInitFn, DirExitFn, FileHandlerFn, DownCnt, sc_flags)) != 0)
  276.            {
  277.           DosFindClose(hdir) ;
  278.           return( 1 ) ;
  279.            }
  280.            chdir("..") ;
  281.         }
  282.      }
  283.       }
  284.    } while (status == 0) ;
  285.    DosFindClose(hdir) ;
  286.  
  287.    if (sc_flags & SC_AFTERSUBDIRS && DirExitFn != NULL)
  288.       DirExitFn(buf) ;
  289.  
  290.    return( 0 ) ;
  291. }
  292. #endif
  293.