Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.9 KiB

  1. //
  2. // WALKPATH.C
  3. //
  4. #include "sigverif.h"
  5. LPDIRNODE g_lpDirList = NULL;
  6. LPDIRNODE g_lpDirEnd = NULL;
  7. BOOL g_bRecurse = TRUE;
  8. //
  9. // This function takes a directory name and a search pattern and looks for all files mathching the pattern.
  10. // If bRecurse is set, then it will add subdirectories to the end of the g_lpDirList for subsequent traversal.
  11. //
  12. // In this routine we allocate and fill in some of the lpFileNode values that we know about.
  13. //
  14. void FindFile(DIRNODE *lpDir, TCHAR *lpDirName, TCHAR *lpFileName)
  15. {
  16. DWORD dwRet;
  17. BOOL bOk = TRUE;
  18. HANDLE hFind;
  19. LPFILENODE lpFileNode;
  20. LPDIRNODE lpDirNode;
  21. WIN32_FIND_DATA FindFileData;
  22. // If the user clicked STOP, then bail immediately!
  23. // If the directory is bogus, then skip to the next one.
  24. if (!g_App.bStopScan && SetCurrentDirectory(lpDirName)) {
  25. // If the user wants to look in subdirectories, then we need to find everything in the current directory
  26. // and if it's a directory we want to add it to the end of the g_lpDirList.
  27. if (g_bRecurse) {
  28. hFind = FindFirstFile(TEXT("*.*"), &FindFileData);
  29. if (hFind != INVALID_HANDLE_VALUE) {
  30. bOk = TRUE;
  31. while (bOk && !g_App.bStopScan) {
  32. if (lstrcmp(FindFileData.cFileName, TEXT(".")) &&
  33. lstrcmp(FindFileData.cFileName, TEXT("..")) &&
  34. (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  35. lpDirNode = (LPDIRNODE) MALLOC(sizeof(DIRNODE));
  36. if (lpDirNode) {
  37. dwRet = GetFullPathName(FindFileData.cFileName, MAX_PATH, lpDirNode->DirName, 0);
  38. if (!dwRet || dwRet >= MAX_PATH) {
  39. GetShortPathName(lpDirName, lpDirNode->DirName, MAX_PATH);
  40. GetFullPathName(FindFileData.cFileName, MAX_PATH, lpDirNode->DirName, 0);
  41. SetCurrentDirectory(lpDirName);
  42. }
  43. }
  44. g_lpDirEnd->next = lpDirNode;
  45. g_lpDirEnd = lpDirNode;
  46. }
  47. bOk = FindNextFile(hFind, &FindFileData);
  48. }
  49. FindClose(hFind);
  50. }
  51. }
  52. // We have added any subdirectories to the dir list, so now we can actually look for
  53. // the lpFileName search pattern and start adding to the g_App.lpFileList.
  54. hFind = FindFirstFile(lpFileName, &FindFileData);
  55. if (hFind != INVALID_HANDLE_VALUE) {
  56. bOk = TRUE;
  57. // While there are more files to be found, keep looking in the directory...
  58. while (bOk && !g_App.bStopScan) {
  59. if (lstrcmp(FindFileData.cFileName, TEXT(".")) &&
  60. lstrcmp(FindFileData.cFileName, TEXT("..")) &&
  61. !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  62. //
  63. // Allocate an lpFileNode, fill it in, and add it to the end of g_App.lpFileList
  64. //
  65. // We need to call CharLowerBuff on the file and dir names because
  66. // the catalog files all contain lower-case names for the files.
  67. //
  68. lpFileNode = CreateFileNode(lpDirName, FindFileData.cFileName);
  69. if (lpFileNode) {
  70. if (!g_App.lpFileList)
  71. g_App.lpFileList = lpFileNode;
  72. else g_App.lpFileLast->next = lpFileNode;
  73. g_App.lpFileLast = lpFileNode;
  74. // Increment the total number of files we've found that meet the search criteria.
  75. g_App.dwFiles++;
  76. }
  77. }
  78. // Get the next file meeting the search pattern.
  79. bOk = FindNextFile(hFind, &FindFileData);
  80. }
  81. FindClose(hFind);
  82. }
  83. }
  84. // Free the memory allocated in the directory node.
  85. lpDir = g_lpDirList;
  86. g_lpDirList = g_lpDirList->next;
  87. FREE(lpDir);
  88. }
  89. //
  90. // Build an g_App.lpFileList given the user settings in the main dialog.
  91. //
  92. void BuildFileList(LPTSTR lpPathName)
  93. {
  94. TCHAR FileName[MAX_PATH];
  95. TCHAR Extension[MAX_PATH];
  96. LPTSTR lpFileName = FileName;
  97. LPTSTR lpExtension = Extension;
  98. LPDIRNODE lpDirNode;
  99. // Check if this is a valid starting directory.
  100. // If not, then pop up an error message.
  101. if (!SetCurrentDirectory(lpPathName)) {
  102. MyErrorBoxId(IDS_BADDIR);
  103. return;
  104. }
  105. // If the "Include Subdirectories" is checked, then bRecurse is TRUE.
  106. if (g_App.bSubFolders)
  107. g_bRecurse = TRUE;
  108. else g_bRecurse = FALSE;
  109. // Get the search pattern from the resource or the user-specified string
  110. if (g_App.bUserScan)
  111. lstrcpy(lpFileName, g_App.szScanPattern);
  112. else MyLoadString(lpFileName, IDS_ALL);
  113. // Allocate the first entry in the g_lpDirList and set it to the
  114. // current directory.
  115. lpDirNode = (LPDIRNODE) MALLOC(sizeof(DIRNODE));
  116. if (lpDirNode) {
  117. lstrcpy(lpDirNode->DirName, lpPathName);
  118. lpDirNode->next = NULL;
  119. }
  120. g_lpDirList = lpDirNode;
  121. g_lpDirEnd = lpDirNode;
  122. // Process the g_lpDirList as long as the user doesn't click STOP!
  123. while (g_lpDirList && !g_App.bStopScan)
  124. FindFile(g_lpDirList, g_lpDirList->DirName, lpFileName);
  125. // Make sure all the memory allocated to the g_lpDirList is freed.
  126. while (g_lpDirList) {
  127. lpDirNode = g_lpDirList->next;
  128. FREE(g_lpDirList);
  129. g_lpDirList = lpDirNode;
  130. }
  131. // If there weren't any files found, then let the user know about it.
  132. if (!g_App.lpFileList)
  133. MyMessageBoxId(IDS_NOFILES);
  134. }