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

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

unix.superglobalmegacorp.com

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