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.

219 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. tolower.c
  5. Abstract:
  6. A tool for issuing persistent reserve commands to a device to see how
  7. it behaves.
  8. Environment:
  9. User mode only
  10. Revision History:
  11. 03-26-96 : Created
  12. --*/
  13. #define UNICODE 1
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <assert.h>
  18. #include <windows.h>
  19. #if 0
  20. #define dbg(x) x
  21. #define HELP_ME() printf("Reached line %4d\n", __LINE__);
  22. #else
  23. #define dbg(x) /* x */
  24. #define HELP_ME() /* printf("Reached line %4d\n", __LINE__); */
  25. #endif
  26. BOOL LowerDirectory(WCHAR *DirectoryName, BOOL Recurse, WCHAR **Patterns, int PatternCount);
  27. BOOL LowerFile(WCHAR *FileName, BOOL Directory);
  28. void PrintUsage(void) {
  29. printf("Usage: tolower [-s] <file_pattern> ...\n");
  30. printf(" -s: recurse though directories\n");
  31. return;
  32. }
  33. int __cdecl wmain(int argc, wchar_t *argv[])
  34. {
  35. BOOL recurse = FALSE;
  36. int i = 0;
  37. HANDLE h;
  38. WCHAR **patternArray;
  39. int patternCount;
  40. if(argc < 2) {
  41. PrintUsage();
  42. return -1;
  43. }
  44. if(_wcsnicmp(argv[1], L"-s", sizeof(L"s")) == 0) {
  45. recurse = TRUE;
  46. dbg(printf("recursive operation\n"));
  47. patternArray = &(argv[2]);
  48. patternCount = argc - 2;
  49. } else {
  50. patternArray = &(argv[1]);
  51. patternCount = argc - 1;
  52. }
  53. wprintf(L"Will %slowercase files matching the following patterns:\n",
  54. recurse ? L"recursively" : L"");
  55. for(i = 0; i < patternCount; i++) {
  56. wprintf(L" %d: %s\n", i, patternArray[i]);
  57. }
  58. LowerDirectory(L".", recurse, patternArray, patternCount);
  59. return 0;
  60. }
  61. BOOL
  62. LowerDirectory(
  63. WCHAR *DirectoryName,
  64. BOOL Recurse,
  65. WCHAR **Patterns,
  66. int PatternCount
  67. )
  68. {
  69. WCHAR oldDir[MAX_PATH];
  70. int i;
  71. int count = 0;
  72. BOOL result = TRUE;
  73. HANDLE iterator = INVALID_HANDLE_VALUE;
  74. WIN32_FIND_DATA data;
  75. //
  76. // Make a pass through all the files in the directory to lower case them
  77. // if they match one of the patterns.
  78. //
  79. GetCurrentDirectory(MAX_PATH, oldDir);
  80. SetCurrentDirectory(DirectoryName);
  81. wprintf(L"Scanning directory %s\n", DirectoryName);
  82. for(i = 0; i < PatternCount; i++) {
  83. dbg(wprintf(L"Checking for %s\n", Patterns[i]));
  84. for(iterator = FindFirstFile(Patterns[i], &data), result = TRUE;
  85. (iterator != INVALID_HANDLE_VALUE) && (result == TRUE);
  86. result = FindNextFile(iterator, &data)) {
  87. //
  88. // Don't process . or ..
  89. //
  90. if((wcscmp(data.cFileName, L".") == 0) ||
  91. (wcscmp(data.cFileName, L"..") == 0)) {
  92. continue;
  93. }
  94. LowerFile(data.cFileName,
  95. (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
  96. count++;
  97. }
  98. if(iterator != INVALID_HANDLE_VALUE) {
  99. FindClose(iterator);
  100. iterator = INVALID_HANDLE_VALUE;
  101. }
  102. }
  103. // wprintf(L"%d files or directories processed\n", count);
  104. count = 0;
  105. assert(iterator == INVALID_HANDLE_VALUE);
  106. memset(&data, 0, sizeof(WIN32_FIND_DATA));
  107. // dbg(wprintf(L"Processing directories in %s\n", buffer));
  108. for(iterator = FindFirstFile(L"*", &data), result = TRUE;
  109. (iterator != INVALID_HANDLE_VALUE) && (result == TRUE);
  110. result = FindNextFile(iterator, &data)) {
  111. if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  112. //
  113. // Don't process . or ..
  114. //
  115. if((wcscmp(data.cFileName, L".") == 0) ||
  116. (wcscmp(data.cFileName, L"..") == 0)) {
  117. continue;
  118. }
  119. LowerDirectory(data.cFileName, Recurse, Patterns, PatternCount);
  120. count++;
  121. }
  122. }
  123. if(iterator == INVALID_HANDLE_VALUE) {
  124. wprintf(L"Error %d scanning directory a second time\n", GetLastError());
  125. }
  126. // wprintf(L"%d directories processed\n", count);
  127. SetCurrentDirectory(oldDir);
  128. return TRUE;
  129. }
  130. BOOL
  131. LowerFile(
  132. WCHAR *FileName,
  133. BOOL Directory
  134. )
  135. {
  136. WCHAR buffer[_MAX_FNAME] = L"hello";
  137. int i;
  138. BOOL result;
  139. wcsncpy(buffer, FileName, _MAX_FNAME);
  140. for(i = 0; buffer[i] != UNICODE_NULL; i++) {
  141. buffer[i] = towlower(buffer[i]);
  142. }
  143. if(wcscmp(buffer, FileName) == 0) {
  144. return TRUE;
  145. }
  146. wprintf(L"%s: %s -> %s: ",
  147. Directory ? L"Dir" : L"File",
  148. FileName,
  149. buffer);
  150. result = MoveFile(FileName, buffer);
  151. if(result) {
  152. wprintf(L"succeeded\n");
  153. } else {
  154. wprintf(L"error %d\n", GetLastError());
  155. }
  156. return TRUE;
  157. }