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