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.

529 lines
15 KiB

  1. #include <stdio.h>
  2. #include <fstream.h>
  3. #include <windows.h>
  4. #include <tchar.h>
  5. #include "resource.h"
  6. #include "other.h"
  7. //
  8. // FList - a file list munger
  9. // Written by: AaronL
  10. //
  11. //
  12. // prototypes...
  13. //
  14. int __cdecl wmain(int ,wchar_t *argv[]);
  15. void ShowHelp(void);
  16. BOOL DoStuff(void);
  17. int ProcessFor_A(void);
  18. int ProcessFor_B(void);
  19. int ProcessFor_C(void);
  20. int ProcessFor_D(void);
  21. int ProcessFor_E(void);
  22. int ProcessFor_F(void);
  23. int MyLoadString(int nID, TCHAR *szResult);
  24. //
  25. // Globals
  26. //
  27. HINSTANCE g_hModuleHandle = NULL;
  28. int g_Flag_a = FALSE;
  29. int g_Flag_b = FALSE;
  30. int g_Flag_c = FALSE;
  31. int g_Flag_d = FALSE;
  32. int g_Flag_e = FALSE;
  33. int g_Flag_f = FALSE;
  34. TCHAR g_Flag_f_Data[_MAX_PATH];
  35. int g_Flag_g = FALSE;
  36. int g_Flag_z = FALSE;
  37. TCHAR g_Flag_z_Data[_MAX_PATH];
  38. TCHAR g_szFileList1[_MAX_PATH];
  39. TCHAR g_szFileList2[_MAX_PATH];
  40. //-------------------------------------------------------------------
  41. // purpose: wmain
  42. //-------------------------------------------------------------------
  43. int __cdecl wmain(int argc,wchar_t *argv[])
  44. {
  45. LPWSTR pArg = NULL;
  46. int argno = 0;
  47. int nflags = 0;
  48. g_hModuleHandle = GetModuleHandle(NULL);
  49. _tcscpy(g_Flag_z_Data, _T(""));
  50. _tcscpy(g_szFileList1, _T(""));
  51. _tcscpy(g_szFileList2, _T(""));
  52. // process command line arguments
  53. for (argno=1; argno<argc; argno++) {
  54. if ( argv[argno][0] == L'-' || argv[argno][0] == L'/' ) {
  55. nflags++;
  56. switch (argv[argno][1]) {
  57. case L'a':
  58. case L'A':
  59. g_Flag_a = TRUE;
  60. break;
  61. case L'b':
  62. case L'B':
  63. g_Flag_b = TRUE;
  64. break;
  65. case L'c':
  66. case L'C':
  67. g_Flag_c = TRUE;
  68. break;
  69. case L'd':
  70. case L'D':
  71. g_Flag_d = TRUE;
  72. break;
  73. case L'e':
  74. case L'E':
  75. g_Flag_e = TRUE;
  76. break;
  77. case L'f':
  78. case L'F':
  79. g_Flag_f = TRUE;
  80. // Get the string for this flag
  81. pArg = CharNext(argv[argno]);
  82. pArg = CharNext(pArg);
  83. if (*pArg == L':') {
  84. LPWSTR pCmdStart = NULL;
  85. pArg = CharNext(pArg);
  86. // Check if it's quoted
  87. if (*pArg == '\"') {
  88. pArg = CharNext(pArg);
  89. pCmdStart = pArg;
  90. while ((*pArg) && (*pArg != L'\"')) {pArg = CharNext(pArg);}
  91. } else {
  92. pCmdStart = pArg;
  93. // while ((*pArg) && (*pArg != '/') && (*pArg != '-')){pArg = CharNextA(pArg);}
  94. while (*pArg) {pArg = CharNext(pArg);}
  95. }
  96. *pArg = '\0';
  97. lstrcpy(g_Flag_f_Data, StripWhitespace(pCmdStart));
  98. }
  99. break;
  100. case 'g':
  101. case 'G':
  102. g_Flag_g = TRUE;
  103. break;
  104. case 'z':
  105. case 'Z':
  106. g_Flag_z = TRUE;
  107. // Get the string for this flag
  108. pArg = CharNext(argv[argno]);
  109. pArg = CharNext(pArg);
  110. if (*pArg == ':') {
  111. LPWSTR pCmdStart = NULL;
  112. pArg = CharNext(pArg);
  113. // Check if it's quoted
  114. if (*pArg == '\"') {
  115. pArg = CharNext(pArg);
  116. pCmdStart = pArg;
  117. while ((*pArg) && (*pArg != '\"')) {pArg = CharNext(pArg);}
  118. } else {
  119. pCmdStart = pArg;
  120. // while ((*pArg) && (*pArg != '/') && (*pArg != '-')){pArg = CharNextA(pArg);}
  121. while (*pArg) {pArg = CharNext(pArg);}
  122. }
  123. *pArg = '\0';
  124. lstrcpy(g_Flag_z_Data, StripWhitespace(pCmdStart));
  125. }
  126. break;
  127. case '?':
  128. goto main_exit_with_help;
  129. break;
  130. } // end of switch
  131. } // if switch character found
  132. else {
  133. if (_tcscmp(g_szFileList1, _T("")) == 0) {
  134. int iLen = min((_MAX_PATH-1), wcslen(argv[argno]));
  135. wcsncpy(g_szFileList1, argv[argno], iLen);
  136. } else {
  137. if (_tcscmp(g_szFileList2, _T("")) == 0) {
  138. lstrcpy(g_szFileList2, argv[argno]);
  139. }
  140. }
  141. } // non-switch char found
  142. } // for all arguments
  143. if (_tcscmp(g_Flag_z_Data, _T("")) == 0) {g_Flag_z = FALSE;}
  144. if (FALSE == DoStuff()) {goto main_exit_with_help;}
  145. goto main_exit_gracefully;
  146. main_exit_gracefully:
  147. exit(0);
  148. return TRUE;
  149. main_exit_with_help:
  150. ShowHelp();
  151. exit(1);
  152. return FALSE;
  153. }
  154. void ShowHelp()
  155. {
  156. TCHAR szModuleName_Full[_MAX_PATH];
  157. TCHAR szModuleName_Fname[_MAX_FNAME];
  158. GetModuleFileName(NULL, szModuleName_Full, _MAX_PATH);
  159. // Trim off the filename only.
  160. _tsplitpath(szModuleName_Full, NULL, NULL, szModuleName_Fname, NULL);
  161. TCHAR szMyBigString[255];
  162. if (MyLoadString(IDS_STRING1, szMyBigString)) {OutputToConsole(szMyBigString,szModuleName_Fname);}
  163. if (MyLoadString(IDS_STRING2, szMyBigString)) {OutputToConsole(szMyBigString,szModuleName_Fname);}
  164. if (MyLoadString(IDS_STRING3, szMyBigString)) {OutputToConsole(szMyBigString);}
  165. if (MyLoadString(IDS_STRING4, szMyBigString)) {OutputToConsole(szMyBigString,szModuleName_Fname);}
  166. if (MyLoadString(IDS_STRING5, szMyBigString)) {OutputToConsole(szMyBigString);}
  167. if (MyLoadString(IDS_STRING6, szMyBigString)) {OutputToConsole(szMyBigString,szModuleName_Fname);}
  168. if (MyLoadString(IDS_STRING7, szMyBigString)) {OutputToConsole(szMyBigString);}
  169. if (MyLoadString(IDS_STRING8, szMyBigString)) {OutputToConsole(szMyBigString,szModuleName_Fname);}
  170. if (MyLoadString(IDS_STRING9, szMyBigString)) {OutputToConsole(szMyBigString);}
  171. if (MyLoadString(IDS_STRING10, szMyBigString)) {OutputToConsole(szMyBigString,szModuleName_Fname);}
  172. if (MyLoadString(IDS_STRING11, szMyBigString)) {OutputToConsole(szMyBigString);}
  173. if (MyLoadString(IDS_STRING15, szMyBigString)) {OutputToConsole(szMyBigString,szModuleName_Fname);}
  174. if (MyLoadString(IDS_STRING16, szMyBigString)) {OutputToConsole(szMyBigString);}
  175. return;
  176. }
  177. //-------------------------------------------------------------------
  178. BOOL DoStuff(void)
  179. {
  180. BOOL bReturn = FALSE;
  181. if (TRUE == ProcessFor_A()) {bReturn = TRUE; goto DoStuff_Exit;}
  182. if (TRUE == ProcessFor_B()) {bReturn = TRUE; goto DoStuff_Exit;}
  183. if (TRUE == ProcessFor_C()) {bReturn = TRUE; goto DoStuff_Exit;}
  184. if (TRUE == ProcessFor_D()) {bReturn = TRUE; goto DoStuff_Exit;}
  185. if (TRUE == ProcessFor_E()) {bReturn = TRUE; goto DoStuff_Exit;}
  186. if (TRUE == ProcessFor_F()) {bReturn = TRUE; goto DoStuff_Exit;}
  187. goto DoStuff_Exit;
  188. DoStuff_Exit:
  189. return bReturn;
  190. }
  191. void MissingFileListEntry1(TCHAR * szMySwitch)
  192. {
  193. TCHAR szMyBigString[255];
  194. if (MyLoadString(IDS_STRING12, szMyBigString)) {OutputToConsole(szMyBigString,szMySwitch);}
  195. return;
  196. }
  197. void MissingFileListEntry2(TCHAR * szMySwitch)
  198. {
  199. TCHAR szMyBigString[255];
  200. if (MyLoadString(IDS_STRING13, szMyBigString)) {OutputToConsole(szMyBigString,szMySwitch);}
  201. return;
  202. }
  203. void MissingFile(TCHAR * szMissingFileName)
  204. {
  205. TCHAR szMyBigString[255];
  206. if (MyLoadString(IDS_STRING14, szMyBigString)) {OutputToConsole(szMyBigString,szMissingFileName);}
  207. return;
  208. }
  209. //
  210. // Flist -a FileList1.txt FileList2.Txt
  211. // [Prints out common entries between FileList1.txt and FileList2.txt]
  212. //
  213. int ProcessFor_A(void)
  214. {
  215. int iReturn = FALSE;
  216. TCHAR szMySwitch[] = _T("-a");
  217. MyFileList FileList1 = {0};
  218. MyFileList FileList2 = {0};
  219. if (TRUE != g_Flag_a) {goto ProcessFor_A_Exit;}
  220. if (_tcscmp(g_szFileList1, _T("")) == 0) {
  221. MissingFileListEntry1(szMySwitch);
  222. goto ProcessFor_A_Exit;
  223. }
  224. if (_tcscmp(g_szFileList2, _T("")) == 0) {
  225. MissingFileListEntry2(szMySwitch);
  226. goto ProcessFor_A_Exit;
  227. }
  228. // Check if the file exists
  229. if (TRUE != IsFileExist(g_szFileList1)) {
  230. MissingFile(g_szFileList1);
  231. goto ProcessFor_A_Exit;
  232. }
  233. // Check if the file exists
  234. if (TRUE != IsFileExist(g_szFileList2)) {
  235. MissingFile(g_szFileList2);
  236. goto ProcessFor_A_Exit;
  237. }
  238. //
  239. // we've got everything we need.
  240. //
  241. FileList1.next = &FileList1; // make it a sentinel
  242. FileList1.prev = &FileList1; // make it a sentinel
  243. ReadFileIntoList(g_szFileList1, &FileList1);
  244. FileList2.next = &FileList2; // make it a sentinel
  245. FileList2.prev = &FileList2; // make it a sentinel
  246. ReadFileIntoList(g_szFileList2, &FileList2);
  247. //DumpOutLinkedFileList(&FileList1);
  248. //DumpOutLinkedFileList(&FileList2);
  249. //
  250. // Do the actual work
  251. //
  252. DumpOutCommonEntries(&FileList1, &FileList2);
  253. FreeLinkedFileList(&FileList1);
  254. FreeLinkedFileList(&FileList2);
  255. iReturn = TRUE;
  256. ProcessFor_A_Exit:
  257. return iReturn;
  258. }
  259. int ProcessFor_B(void)
  260. {
  261. int iReturn = FALSE;
  262. TCHAR szMySwitch[] = _T("-b");
  263. MyFileList FileList1 = {0};
  264. MyFileList FileList2 = {0};
  265. if (TRUE != g_Flag_b) {goto ProcessFor_B_Exit;}
  266. if (_tcscmp(g_szFileList1, _T("")) == 0) {
  267. MissingFileListEntry1(szMySwitch);
  268. goto ProcessFor_B_Exit;
  269. }
  270. if (_tcscmp(g_szFileList2, _T("")) == 0) {
  271. MissingFileListEntry2(szMySwitch);
  272. goto ProcessFor_B_Exit;
  273. }
  274. // Check if the file exists
  275. if (TRUE != IsFileExist(g_szFileList1)) {
  276. MissingFile(g_szFileList1);
  277. goto ProcessFor_B_Exit;
  278. }
  279. // Check if the file exists
  280. if (TRUE != IsFileExist(g_szFileList2)) {
  281. MissingFile(g_szFileList2);
  282. goto ProcessFor_B_Exit;
  283. }
  284. //
  285. // we've got everything we need.
  286. //
  287. FileList1.next = &FileList1; // make it a sentinel
  288. FileList1.prev = &FileList1; // make it a sentinel
  289. ReadFileIntoList(g_szFileList1, &FileList1);
  290. FileList2.next = &FileList2; // make it a sentinel
  291. FileList2.prev = &FileList2; // make it a sentinel
  292. ReadFileIntoList(g_szFileList2, &FileList2);
  293. //
  294. // Do the actual work
  295. //
  296. DumpOutDifferences(&FileList1, &FileList2);
  297. FreeLinkedFileList(&FileList1);
  298. FreeLinkedFileList(&FileList2);
  299. iReturn = TRUE;
  300. ProcessFor_B_Exit:
  301. return iReturn;
  302. }
  303. int ProcessFor_C(void)
  304. {
  305. int iReturn = FALSE;
  306. TCHAR szMySwitch[] = _T("-c");
  307. MyFileList FileList1 = {0};
  308. if (TRUE != g_Flag_c) {goto ProcessFor_C_Exit;}
  309. if (_tcscmp(g_szFileList1, _T("")) == 0) {
  310. MissingFileListEntry1(szMySwitch);
  311. goto ProcessFor_C_Exit;
  312. }
  313. // Check if the file exists
  314. if (TRUE != IsFileExist(g_szFileList1)) {
  315. MissingFile(g_szFileList1);
  316. goto ProcessFor_C_Exit;
  317. }
  318. //
  319. // we've got everything we need.
  320. //
  321. FileList1.next = &FileList1; // make it a sentinel
  322. FileList1.prev = &FileList1; // make it a sentinel
  323. ReadFileIntoList(g_szFileList1, &FileList1);
  324. DumpOutLinkedFileList(&FileList1);
  325. FreeLinkedFileList(&FileList1);
  326. iReturn = TRUE;
  327. ProcessFor_C_Exit:
  328. return iReturn;
  329. }
  330. int ProcessFor_D(void)
  331. {
  332. int iReturn = FALSE;
  333. TCHAR szMySwitch[] = _T("-d");
  334. MyFileList FileList1 = {0};
  335. if (TRUE != g_Flag_d) {goto ProcessFor_D_Exit;}
  336. if (_tcscmp(g_szFileList1, _T("")) == 0) {
  337. MissingFileListEntry1(szMySwitch);
  338. goto ProcessFor_D_Exit;
  339. }
  340. // Check if the file exists
  341. if (TRUE != IsFileExist(g_szFileList1)) {
  342. MissingFile(g_szFileList1);
  343. goto ProcessFor_D_Exit;
  344. }
  345. //
  346. // we've got everything we need.
  347. //
  348. FileList1.next = &FileList1; // make it a sentinel
  349. FileList1.prev = &FileList1; // make it a sentinel
  350. ReadFileIntoList(g_szFileList1, &FileList1);
  351. DumpOutLinkedFileList(&FileList1);
  352. FreeLinkedFileList(&FileList1);
  353. iReturn = TRUE;
  354. ProcessFor_D_Exit:
  355. return iReturn;
  356. }
  357. int ProcessFor_E(void)
  358. {
  359. int iReturn = FALSE;
  360. TCHAR szMySwitch[] = _T("-e");
  361. MyFileList FileList1 = {0};
  362. if (TRUE != g_Flag_e) {goto ProcessFor_E_Exit;}
  363. if (_tcscmp(g_szFileList1, _T("")) == 0) {
  364. MissingFileListEntry1(szMySwitch);
  365. goto ProcessFor_E_Exit;
  366. }
  367. // Check if the file exists
  368. if (TRUE != IsFileExist(g_szFileList1)) {
  369. MissingFile(g_szFileList1);
  370. goto ProcessFor_E_Exit;
  371. }
  372. //
  373. // we've got everything we need.
  374. //
  375. FileList1.next = &FileList1; // make it a sentinel
  376. FileList1.prev = &FileList1; // make it a sentinel
  377. ReadFileIntoList(g_szFileList1, &FileList1);
  378. DumpOutLinkedFileList(&FileList1);
  379. FreeLinkedFileList(&FileList1);
  380. iReturn = TRUE;
  381. ProcessFor_E_Exit:
  382. return iReturn;
  383. }
  384. int ProcessFor_F(void)
  385. {
  386. int iReturn = FALSE;
  387. TCHAR szMySwitch[] = _T("-f");
  388. MyFileList FileList1 = {0};
  389. if (TRUE != g_Flag_f) {goto ProcessFor_F_Exit;}
  390. if (_tcscmp(g_szFileList1, _T("")) == 0) {
  391. MissingFileListEntry1(szMySwitch);
  392. goto ProcessFor_F_Exit;
  393. }
  394. // Check if the file exists
  395. if (TRUE != IsFileExist(g_szFileList1)) {
  396. MissingFile(g_szFileList1);
  397. goto ProcessFor_F_Exit;
  398. }
  399. //
  400. // we've got everything we need.
  401. //
  402. FileList1.next = &FileList1; // make it a sentinel
  403. FileList1.prev = &FileList1; // make it a sentinel
  404. ReadFileIntoList(g_szFileList1, &FileList1);
  405. DumpOutLinkedFileList(&FileList1);
  406. FreeLinkedFileList(&FileList1);
  407. iReturn = TRUE;
  408. ProcessFor_F_Exit:
  409. return iReturn;
  410. }
  411. #define BUFFER_MAX 1024
  412. int MyLoadString(int nID, TCHAR *szResult)
  413. {
  414. TCHAR buf[BUFFER_MAX];
  415. int iReturn = FALSE;
  416. if (g_hModuleHandle == NULL) {return iReturn;}
  417. if (LoadString(g_hModuleHandle, nID, buf, BUFFER_MAX)) {
  418. iReturn = TRUE;
  419. _tcscpy(szResult, buf);
  420. }
  421. return iReturn;
  422. }