Annotation of q_a/samples/walk/walk.c, revision 1.1.1.1

1.1       root        1: /****************************************************************************
                      2: *
                      3: *    PROGRAM: WALK.C
                      4: *
                      5: *    PURPOSE: Uses recursion to walk through all subdirectories in the
                      6: *             current working directory
                      7: *
                      8: *    FUNCTIONS:
                      9: *
                     10: *        FindFirstDirectory() - finds the first directory in the current
                     11: *                 working directory
                     12: *        Walk() - finds the subdirectories in the current working directory,
                     13: *                 changes the current working directory to this subdirectory,
                     14: *                 recusively calls itself until there are no more
                     15: *                 subdirectories
                     16: *
                     17: *    COMMENTS:
                     18: *
                     19: ****************************************************************************/
                     20: 
                     21: #include <windows.h>
                     22: #include <stdio.h>
                     23: #include <string.h>
                     24: #include <process.h>
                     25: #include "walk.h"
                     26: 
                     27: /****************************************************************************
                     28: *
                     29: *    FUNCTION: FindFirstDirectory(LPTSTR, LPWIN32_FIND_DATA)
                     30: *
                     31: *    PURPOSE: finds the first directory in the current working directory
                     32: *
                     33: *    COMMENTS:
                     34: *
                     35: *       This function is called by Walk() each time a new subdirectory is
                     36: *       entered.  Since only directory entries are of interest, this call
                     37: *       provides a simple means to bypass non-directory filenames.
                     38: *
                     39: *    INPUT: lpszSearchFile -> "*"
                     40: *           lpffd - pointer to the file find data structure of type
                     41: *                   WIN32_FIND_DATA
                     42: *
                     43: *    OUTPUT: Returns a file handle if a directory is found
                     44: *            Returns a -1 if no directory is found        
                     45: *
                     46: ****************************************************************************/
                     47: 
                     48: HANDLE FindFirstDirectory(LPTSTR lpszSearchFile, LPWIN32_FIND_DATA lpffd)
                     49: {
                     50:   BOOL     bRC;
                     51:   DWORD    dwRC;
                     52:   HANDLE   hSearch;
                     53:   int      iRC;
                     54: 
                     55:   hSearch=FindFirstFile(lpszSearchFile,lpffd);
                     56:   if (hSearch == (HANDLE) -1)
                     57:      return (hSearch);
                     58:   for(;;)
                     59:      {
                     60:      dwRC=GetFileAttributes(lpffd->cFileName);
                     61:      if (dwRC & FILE_ATTRIBUTE_DIRECTORY)
                     62:         {
                     63:         iRC=strcmp(lpffd->cFileName,".");
                     64:         if (iRC)
                     65:            {
                     66:            iRC=strcmp(lpffd->cFileName,"..");
                     67:            if (iRC)
                     68:               return (hSearch);
                     69:            }
                     70:         }
                     71:      bRC=FindNextFile(hSearch,lpffd);
                     72:      if (bRC == FALSE)
                     73:         {
                     74:         FindClose(hSearch);
                     75:         return ((HANDLE) -1);
                     76:         }
                     77:      }
                     78: }
                     79: 
                     80: /****************************************************************************
                     81: *
                     82: *    FUNCTION: FindNextDirectory(LPTSTR, LPWIN32_FIND_DATA)
                     83: *
                     84: *    PURPOSE: finds the next directory in the current working directory
                     85: *
                     86: *    COMMENTS:
                     87: *
                     88: *       This function is called by Walk() each time a new subdirectory is
                     89: *       entered.  Since only directory entries are of interest, this call
                     90: *       provides a simple means to bypass non-directory filenames.
                     91: *
                     92: *    INPUT: lpszSearchFile -> "*"
                     93: *           lpffd - pointer to the file find data structure of type
                     94: *                   WIN32_FIND_DATA
                     95: *
                     96: *    OUTPUT: Returns a file handle if a directory is found
                     97: *            Returns a -1 if no directory is found        
                     98: *
                     99: ****************************************************************************/
                    100: 
                    101: BOOL FindNextDirectory(HANDLE hSearch, LPWIN32_FIND_DATA lpffd)
                    102: {
                    103:   BOOL     bRC;
                    104:   DWORD    dwRC;
                    105: 
                    106:   for(;;)
                    107:      {
                    108:      bRC=FindNextFile(hSearch,lpffd);
                    109:      if (bRC == FALSE)
                    110:         return (FALSE);
                    111:      dwRC=GetFileAttributes(lpffd->cFileName);
                    112:      if (dwRC & FILE_ATTRIBUTE_DIRECTORY)
                    113:         return (TRUE);
                    114:      }
                    115: }
                    116: 
                    117: /****************************************************************************
                    118: *
                    119: *    FUNCTION: Walk(WORD)
                    120: *
                    121: *    PURPOSE: finds a subdirectory in the current working directory,
                    122: *             changes the current working directory to this subdirectory,
                    123: *             and recusively calls itself until there are no more
                    124: *             subdirectories
                    125: *
                    126: *    COMMENTS:When a new directory is entered from above, a handle for
                    127: *             the new directory is obtained using FindFirstDirectory.  Once
                    128: *             the first directory is found, the current working directory
                    129: *             is changed to this first directory and Walk() is recursively
                    130: *             called again.  At this point, the next available directory
                    131: *             is searched for using FindNextFile, entered and a recursive
                    132: *             call is made to Walk().  When each directory has been searched,
                    133: *             until no more directories exist, the current working directory
                    134: *             is changed to the parent directory (..).  This continues until
                    135: *             the current working directory is equal to the original
                    136: *             directory.
                    137: *
                    138: *    INPUT: wLevel - bookmark, when wLevel is greater than 0, then the
                    139: *             current working directory is a subdirectory of the original
                    140: *             directory.  If wLevel is equal to 0, then the directory is the
                    141: *             original directory and the recursive calls stop
                    142: *
                    143: *    OUTPUT: 
                    144: *
                    145: ****************************************************************************/
                    146: 
                    147: VOID Walk(WORD wLevel)
                    148: {
                    149:   BOOL              bRC=TRUE;
                    150:   DWORD             dwRC;
                    151:   HANDLE            hSearch;
                    152:   WIN32_FIND_DATA   w32FindBuf;
                    153: 
                    154:   dwRC=GetCurrentDirectory(BUFSIZE,chPathName);
                    155:   if (dwRC >= (BUFSIZE-1))
                    156:      {
                    157:      printf("Buffer too small for path name.  Exiting...\n");
                    158:      exit(1);
                    159:      }
                    160:   printf("%s\n",chPathName);
                    161:   hSearch=FindFirstDirectory("*",&w32FindBuf);
                    162:   if (hSearch == (HANDLE) -1)
                    163:      {
                    164:      if (wLevel)
                    165:         SetCurrentDirectory("..");
                    166:      return;
                    167:      }
                    168: 
                    169:   for (;;)
                    170:      {
                    171:      SetCurrentDirectory(w32FindBuf.cFileName);
                    172:      Walk(++wLevel);
                    173:      bRC=FindNextDirectory(hSearch,&w32FindBuf);
                    174:      if (bRC == FALSE)
                    175:         {
                    176:         SetCurrentDirectory("..");
                    177:         FindClose(hSearch);
                    178:         return;
                    179:         }
                    180:      }
                    181: }
                    182: 
                    183: VOID main(VOID)
                    184: {
                    185:   Walk(0);
                    186: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.