Leaked source code of windows server 2003
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.

305 lines
6.8 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. main.c
  5. Abstract:
  6. <TODO: fill in abstract>
  7. Author:
  8. TODO: <full name> (<alias>) <date>
  9. Revision History:
  10. <full name> (<alias>) <date> <comments>
  11. --*/
  12. #include "pch.h"
  13. #include <conio.h>
  14. HANDLE g_hHeap;
  15. HINSTANCE g_hInst;
  16. BOOL
  17. pCallEntryPoints (
  18. DWORD Reason
  19. )
  20. {
  21. //
  22. // Initialize the common libs
  23. //
  24. if (Reason == DLL_PROCESS_ATTACH) {
  25. UtInitialize (NULL);
  26. if (!FileEnumInitialize ()) {
  27. return FALSE;
  28. }
  29. } else {
  30. FileEnumTerminate ();
  31. UtTerminate ();
  32. }
  33. return TRUE;
  34. }
  35. BOOL
  36. Init (
  37. VOID
  38. )
  39. {
  40. g_hHeap = GetProcessHeap();
  41. g_hInst = GetModuleHandle (NULL);
  42. return pCallEntryPoints (DLL_PROCESS_ATTACH);
  43. }
  44. VOID
  45. Terminate (
  46. VOID
  47. )
  48. {
  49. pCallEntryPoints (DLL_PROCESS_DETACH);
  50. }
  51. VOID
  52. HelpAndExit (
  53. VOID
  54. )
  55. {
  56. //
  57. // This routine is called whenever command line args are wrong
  58. //
  59. fprintf (
  60. stderr,
  61. "Command Line Syntax:\n\n"
  62. //
  63. // TODO: Describe command line syntax(es), indent 2 spaces
  64. //
  65. " fileenum [/N] [/S] [/W] [/F] [/L:MaxSubLevel] [/X] <NodePattern> <LeafPattern>\n"
  66. "\nDescription:\n\n"
  67. //
  68. // TODO: Describe tool, indent 2 spaces
  69. //
  70. " Enumerates the part of file system that matches <Pattern>.\n"
  71. " Uses C:\\exclude.inf if present and /X not specified to determine what paths/files\n"
  72. " are excluded.\n"
  73. "\nArguments:\n\n"
  74. //
  75. // TODO: Describe args, indent 2 spaces, say optional if necessary
  76. //
  77. " /N Specifies exclusion of directory names from enumeration; optional\n"
  78. " /S Specifies sub-directories to be enumerated before files for any dir; optional\n"
  79. " /W Specifies enumeration should be width-first; optional\n"
  80. " /F Specifies output should use \"dir <filename>\" format; optional\n"
  81. " /L:MaxSubLevel Specifies the maximum sub-level starting from the root of enum;\n"
  82. " -1 = all levels, 0 = only the root level etc.; optional\n"
  83. " /X Specifies to use exclusions in C:\\exclude.inf; optional\n"
  84. " <NodePattern> Specifies the dir pattern\n"
  85. " <LeafPattern> Specifies the file pattern\n"
  86. );
  87. exit (1);
  88. }
  89. BOOL
  90. BuildExclusionList (
  91. IN PCTSTR FileName
  92. )
  93. {
  94. HINF h;
  95. INFCONTEXT ic;
  96. TCHAR buf[256];
  97. h = SetupOpenInfFile (FileName, NULL, INF_STYLE_WIN4 | INF_STYLE_OLDNT, NULL);
  98. if (h == INVALID_HANDLE_VALUE) {
  99. return FALSE;
  100. }
  101. if (SetupFindFirstLine (h, TEXT("Paths"), NULL, &ic)) {
  102. do {
  103. if (SetupGetStringField (&ic, 0, buf, DWSIZEOF (buf) / DWSIZEOF (TCHAR), NULL)) {
  104. ElAdd (ELT_PATH, buf);
  105. }
  106. } while (SetupFindNextLine (&ic, &ic));
  107. }
  108. if (SetupFindFirstLine (h, TEXT("Files"), NULL, &ic)) {
  109. do {
  110. if (SetupGetStringField (&ic, 0, buf, DWSIZEOF (buf) / DWSIZEOF (TCHAR), NULL)) {
  111. ElAdd (ELT_FILE, buf);
  112. }
  113. } while (SetupFindNextLine (&ic, &ic));
  114. }
  115. SetupCloseInfFile (h);
  116. return TRUE;
  117. }
  118. BOOL
  119. FileEnumCallback (
  120. IN PDIRNODE DirNode OPTIONAL
  121. )
  122. {
  123. BOOL b;
  124. if (!DirNode) {
  125. _ftprintf (stderr, TEXT("\nOut of memory\n"));
  126. return FALSE;
  127. }
  128. _ftprintf (stderr, TEXT("Error creating dir node: %s; continue anyway ? (yn):"), DirNode->DirName);
  129. b = _totupper(_getche ()) == TEXT('Y');
  130. _ftprintf (stderr, TEXT("\n"));
  131. return b;
  132. }
  133. INT
  134. __cdecl
  135. _tmain (
  136. INT argc,
  137. PCTSTR argv[]
  138. )
  139. {
  140. INT i;
  141. PCTSTR nodePattern = NULL;
  142. PCTSTR leafPattern = NULL;
  143. PTSTR encodedPattern = NULL;
  144. PTSTR p;
  145. PCTSTR level;
  146. BOOL enumDirNames = TRUE;
  147. BOOL filesFirst = TRUE;
  148. BOOL depthFirst = TRUE;
  149. BOOL nativeFormat = TRUE;
  150. UINT maxSubLevel = -1;
  151. BOOL exclusions = FALSE;
  152. INT pos;
  153. FILETREE_ENUM e;
  154. //
  155. // TODO: Parse command line here
  156. //
  157. if (argc < 2) {
  158. HelpAndExit ();
  159. }
  160. for (i = 1 ; i < argc ; i++) {
  161. if (argv[i][0] == TEXT('/') || argv[i][0] == TEXT('-')) {
  162. switch (_totlower (_tcsnextc (&argv[i][1]))) {
  163. case TEXT('x'):
  164. exclusions = TRUE;
  165. break;
  166. case TEXT('n'):
  167. enumDirNames = FALSE;
  168. break;
  169. case TEXT('s'):
  170. filesFirst = FALSE;
  171. break;
  172. case TEXT('w'):
  173. depthFirst = FALSE;
  174. break;
  175. case TEXT('f'):
  176. nativeFormat = FALSE;
  177. break;
  178. case TEXT('l'):
  179. if (argv[i][2] == TEXT(':')) {
  180. level = &argv[i][3];
  181. } else if (i + 1 < argc) {
  182. level = argv[++i];
  183. } else {
  184. HelpAndExit();
  185. }
  186. if (!_stscanf (level, TEXT("%ld%n"), &maxSubLevel, &pos) || level[pos]) {
  187. HelpAndExit();
  188. }
  189. break;
  190. default:
  191. HelpAndExit();
  192. }
  193. } else {
  194. //
  195. // Parse other args that don't require / or -
  196. //
  197. if (nodePattern || leafPattern) {
  198. HelpAndExit();
  199. }
  200. nodePattern = argv[i];
  201. if (*nodePattern == TEXT('\"')) {
  202. nodePattern++;
  203. p = _tcsdec2 (nodePattern, GetEndOfString (nodePattern));
  204. if (p && *p == TEXT('\"')) {
  205. *p = 0;
  206. }
  207. }
  208. leafPattern = argv[i+1];
  209. i++;
  210. }
  211. }
  212. //
  213. // Begin processing
  214. //
  215. if (!Init()) {
  216. return 2;
  217. }
  218. //
  219. // TODO: Do work here
  220. //
  221. if (exclusions) {
  222. BuildExclusionList (TEXT("C:\\exclude.inf"));
  223. }
  224. encodedPattern = ObsBuildEncodedObjectStringEx (nodePattern, leafPattern, FALSE);
  225. if (EnumFirstFileInTreeEx (
  226. &e,
  227. encodedPattern,
  228. DRIVEENUM_ALLVALID,
  229. enumDirNames,
  230. filesFirst,
  231. depthFirst,
  232. maxSubLevel,
  233. exclusions,
  234. FileEnumCallback
  235. )) {
  236. do {
  237. _putts (nativeFormat ? e.NativeFullName : e.EncodedFullName);
  238. } while (EnumNextFileInTree (&e));
  239. }
  240. //
  241. // End of processing
  242. //
  243. if (exclusions) {
  244. ElRemoveAll ();
  245. }
  246. Terminate();
  247. return 0;
  248. }