Annotation of mstools/samples/filer/walk.c, revision 1.1.1.2

1.1       root        1: /****************************************************************************
                      2: *
1.1.1.2 ! root        3: *
        !             4: *    Microsoft Developer Support
        !             5: *    Copyright (c) 1992 Microsoft Corporation
        !             6: *
        !             7: *
1.1       root        8: *    PROGRAM: WALK.C
                      9: *
                     10: *    PURPOSE: Uses recursion to walk through all subdirectories in the
                     11: *             current working directory
                     12: *
                     13: *    FUNCTIONS:
                     14: *
                     15: *        FindFirstDirectory() - finds the first directory in the current
                     16: *                 working directory
                     17: *        Walk() - finds the subdirectories in the current working directory,
                     18: *                 changes the current working directory to this subdirectory,
                     19: *                 recusively calls itself until there are no more
                     20: *                 subdirectories
                     21: *
                     22: *    COMMENTS:
                     23: *
                     24: ****************************************************************************/
1.1.1.2 ! root       25: #define  STRICT
1.1       root       26: #include <windows.h>
                     27: #include <stdio.h>
                     28: #include <string.h>
                     29: #include <process.h>
                     30: #include "globals.h"
                     31: #include "filer.h"
                     32: #include "walk.h"
                     33: 
                     34: extern CRITICAL_SECTION     gSetDirCS;
                     35: 
                     36: /****************************************************************************
                     37: *
                     38: *    FUNCTION: FindFirstDirectory(LPTSTR, LPWIN32_FIND_DATA)
                     39: *
                     40: *    PURPOSE: finds the first directory in the current working directory
                     41: *
                     42: *    COMMENTS:
                     43: *
                     44: *       This function is called by Walk() each time a new subdirectory is
                     45: *       entered.  Since only directory entries are of interest, this call
                     46: *       provides a simple means to bypass non-directory filenames.
                     47: *
                     48: *    INPUT: lpszSearchFile -> "*"
                     49: *           lpffd - pointer to the file find data structure of type
                     50: *                   WIN32_FIND_DATA
                     51: *
                     52: *    OUTPUT: Returns a file handle if a directory is found
                     53: *            Returns a -1 if no directory is found        
                     54: *
                     55: ****************************************************************************/
                     56: 
                     57: HANDLE FindFirstDirectory(LPTSTR lpszSearchFile, LPWIN32_FIND_DATA lpffd)
                     58: {
                     59:   BOOL     fRC;
                     60:   DWORD    dwRC;
                     61:   HANDLE   hFile;
                     62:   int      iRC;
                     63: 
                     64:   hFile=FindFirstFile(lpszSearchFile,lpffd);
                     65:   if (hFile == (HANDLE) -1)
                     66:      return (hFile);
                     67:   for(;;){
                     68:      dwRC=GetFileAttributes(lpffd->cFileName);
                     69:      if (dwRC & FILE_ATTRIBUTE_DIRECTORY){
                     70:         iRC=strcmp(lpffd->cFileName,".");
                     71:         if (iRC){
                     72:            iRC=strcmp(lpffd->cFileName,"..");
                     73:            if (iRC)
                     74:               return (hFile);
                     75:         }
                     76:      }
                     77:      fRC=FindNextFile(hFile,lpffd);
                     78:      if (fRC == FALSE){
                     79:         FindClose(hFile);
                     80:         return ((HANDLE) -1);
                     81:      }
                     82:   }
                     83: }
                     84: 
                     85: /****************************************************************************
                     86: *
                     87: *    FUNCTION: FindNextDirectory(LPTSTR, LPWIN32_FIND_DATA)
                     88: *
                     89: *    PURPOSE: finds the next directory in the current working directory
                     90: *
                     91: *    COMMENTS:
                     92: *
                     93: *       This function is called by Walk() each time a new subdirectory is
                     94: *       entered.  Since only directory entries are of interest, this call
                     95: *       provides a simple means to bypass non-directory filenames.
                     96: *
                     97: *    INPUT: lpszSearchFile -> "*"
                     98: *           lpffd - pointer to the file find data structure of type
                     99: *                   WIN32_FIND_DATA
                    100: *
                    101: *    OUTPUT: Returns a file handle if a directory is found
                    102: *            Returns a -1 if no directory is found        
                    103: *
                    104: ****************************************************************************/
                    105: 
                    106: BOOL FindNextDirectory(HANDLE hDir, LPWIN32_FIND_DATA lpffd)
                    107: {
                    108:   BOOL     fRC;
                    109:   DWORD    dwRC;
                    110: 
                    111:   for(;;){
                    112:      fRC=FindNextFile(hDir,lpffd);
                    113:      if (fRC == FALSE)
                    114:         return (FALSE);
                    115:      dwRC=GetFileAttributes(lpffd->cFileName);
                    116:      if (dwRC & FILE_ATTRIBUTE_DIRECTORY)
                    117:         return (TRUE);
                    118:   }
                    119: }
                    120: 
                    121: /****************************************************************************
                    122: *
                    123: *    FUNCTION: Walk(WORD)
                    124: *
                    125: *    PURPOSE: finds a subdirectory in the current working directory,
                    126: *             changes the current working directory to this subdirectory,
                    127: *             and recusively calls itself until there are no more
                    128: *             subdirectories
                    129: *
                    130: *    COMMENTS:When a new directory is entered from above, a handle for
                    131: *             the new directory is obtained using FindFirstDirectory.  Once
                    132: *             the first directory is found, the current working directory
                    133: *             is changed to this first directory and Walk() is recursively
                    134: *             called again.  At this point, the next available directory
                    135: *             is searched for using FindNextFile, entered and a recursive
                    136: *             call is made to Walk().  When each directory has been searched,
                    137: *             until no more directories exist, the current working directory
                    138: *             is changed to the parent directory (..).  This continues until
                    139: *             the current working directory is equal to the original
                    140: *             directory.
                    141: *
                    142: *    INPUT: wLevel - bookmark, when wLevel is greater than 0, then the
                    143: *             current working directory is a subdirectory of the original
                    144: *             directory.  If wLevel is equal to 0, then the directory is the
                    145: *             original directory and the recursive calls stop
                    146: *
                    147: *    OUTPUT: 
                    148: *
                    149: ****************************************************************************/
                    150: 
                    151: VOID Walk(HWND hwnd, LPSTR lpszPathName, WORD wLevel, LPBOOL lpfAlive)
                    152: {
                    153:     BOOL              fRC=TRUE;
                    154:     DWORD             dwCurrent;
                    155:     HANDLE            hDir;
                    156:     WIN32_FIND_DATA   w32FindBuf;
                    157: 
                    158:     if( *lpfAlive == FALSE )
                    159:         return;
                    160: 
                    161:     dwCurrent=GetCurrentDirectory(DIRECTORY_STRING_SIZE, lpszPathName);
                    162:     if( dwCurrent >= (DIRECTORY_STRING_SIZE-1) ){
                    163:         ErrorMsg("Walk: Buffer Size error.");
                    164:         return;
                    165:     }
                    166: 
                    167:     SendMessage(hwnd, LB_ADDSTRING, NULL, (LPARAM)lpszPathName);
                    168: 
                    169:     hDir=FindFirstDirectory("*",&w32FindBuf);
                    170:     if (hDir == (HANDLE) -1){
                    171: 
                    172:         if (wLevel)
                    173:             SetCurrentDirectory("..");
                    174: 
                    175:         return;
                    176:     }
                    177: 
                    178:     for (;;){
                    179:         SetCurrentDirectory(w32FindBuf.cFileName);
                    180:         Walk(hwnd, lpszPathName, ++wLevel, lpfAlive);
                    181:         fRC=FindNextDirectory(hDir,&w32FindBuf);
                    182: 
                    183:         if (fRC == FALSE){
                    184:             SetCurrentDirectory("..");
                    185:             FindClose(hDir);
                    186:             return;
                    187:         }
                    188:     }
                    189: }
                    190: 
                    191: BOOL EnumDir(HANDLE hwnd, LPCINFO lpCInfo)
                    192: {
                    193:     char                szPathName[DIRECTORY_STRING_SIZE];
                    194:     LPCRITICAL_SECTION  lpCS;
                    195:     BOOL                fExitCode = TRUE;
                    196: 
                    197:     if( hwnd == lpCInfo->hwndLL)
                    198:         lpCS = &(lpCInfo->CritSecL);
                    199:     else
                    200:         lpCS = &(lpCInfo->CritSecR);
                    201: 
                    202:     EnterCriticalSection(lpCS);
                    203:     EnterCriticalSection(&gSetDirCS);
                    204: 
                    205:     lpCInfo->fAlive = TRUE;
                    206: 
                    207:     if( SetCurrentDirectory( lpCInfo->lpDriveInfo->DriveName ) ){
                    208: 
                    209:         //
                    210:         // Empty the ListBox of its contents,
                    211:         // Clear the redraw flag, and build the directory tree.
                    212:         //
                    213:         SendMessage(hwnd, LB_RESETCONTENT, (WPARAM)NULL, (LPARAM)NULL);
                    214: //        SendMessage(hwnd, WM_SETREDRAW, (WPARAM)0, (LPARAM)NULL);
                    215: 
                    216:         Walk(hwnd, (LPSTR)szPathName, 0, &(lpCInfo->fAlive));
                    217: 
                    218:         if( lpCInfo->fAlive == FALSE )
                    219:             fExitCode = TRUE;
                    220:         else{
                    221:             //
                    222:             // set the redraw flag, then add and remove a blank string,
                    223:             // in order to update the window.
                    224:             //
                    225:             SendMessage(hwnd, WM_SETREDRAW, (WPARAM)1, (LPARAM)NULL);
                    226:             SendMessage(hwnd, LB_DELETESTRING,
                    227:                 SendMessage(hwnd, LB_ADDSTRING, (WPARAM)NULL, (LPARAM)" "),
                    228:                 (LPARAM)NULL);
                    229:         }
                    230:     }
                    231:     else
                    232:         fExitCode = FALSE;
                    233: 
                    234:     lpCInfo->fAlive = FALSE;
                    235: 
                    236:     LeaveCriticalSection(lpCS);
                    237:     LeaveCriticalSection(&gSetDirCS);
                    238: 
                    239:     return(fExitCode);
                    240: }

unix.superglobalmegacorp.com

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