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.

446 lines
12 KiB

  1. #include <stdio.h>
  2. #include <fstream.h>
  3. #include <windows.h>
  4. #include <tchar.h>
  5. #include "other.h"
  6. extern int g_Flag_a;
  7. extern int g_Flag_b;
  8. extern int g_Flag_c;
  9. extern int g_Flag_d;
  10. extern int g_Flag_e;
  11. extern int g_Flag_f;
  12. extern TCHAR * g_Flag_f_Data;
  13. extern int g_Flag_g;
  14. extern int g_Flag_z;
  15. extern TCHAR * g_Flag_z_Data;
  16. void OutputToConsole(TCHAR *szInsertionStringFormat, TCHAR *szInsertionString)
  17. {
  18. TCHAR BigString[1000];
  19. _stprintf(BigString,szInsertionStringFormat,szInsertionString);
  20. _tprintf(BigString);
  21. return;
  22. }
  23. void OutputToConsole(TCHAR *szInsertionStringFormat, int iTheInteger)
  24. {
  25. TCHAR BigString[1000];
  26. _stprintf(BigString,szInsertionStringFormat,iTheInteger);
  27. _tprintf(BigString);
  28. return;
  29. }
  30. void OutputToConsole(TCHAR *szString)
  31. {
  32. _tprintf(szString);
  33. return;
  34. }
  35. void DumpOutCommonEntries(MyFileList *pTheMasterFileList1, MyFileList *pTheMasterFileList2)
  36. {
  37. MyFileList *t = NULL;
  38. MyFileList *t2 = NULL;
  39. t = pTheMasterFileList1->next;
  40. while (t != pTheMasterFileList1)
  41. {
  42. //
  43. // Loop thru all of list 2 looking for this entry
  44. //
  45. t2 = pTheMasterFileList2->next;
  46. while (t2 != pTheMasterFileList2)
  47. {
  48. if (0 == _tcsicmp(t->szFileNameEntry, t2->szFileNameEntry))
  49. {
  50. //OutputToConsole(_T("%s\r\n"),t->szFileNameEntry);
  51. OutputToConsole(_T("%s\n"),t->szFileNameEntry);
  52. }
  53. t2 = t2->next;
  54. }
  55. // Get the next entry from list 2
  56. t = t->next;
  57. }
  58. return;
  59. }
  60. void DumpOutLinkedFileList(MyFileList *pTheMasterFileList)
  61. {
  62. MyFileList *t = NULL;
  63. t = pTheMasterFileList->next;
  64. while (t != pTheMasterFileList)
  65. {
  66. //OutputToConsole(_T("%s\r\n"),t->szFileNameEntry);
  67. OutputToConsole(_T("%s\n"),t->szFileNameEntry);
  68. t = t->next;
  69. }
  70. return;
  71. }
  72. void DumpOutDifferences(MyFileList *pTheMasterFileList1, MyFileList *pTheMasterFileList2)
  73. {
  74. int iFound = FALSE;
  75. MyFileList *t = NULL;
  76. MyFileList *t2 = NULL;
  77. // Loop thru list #1
  78. t = pTheMasterFileList1->next;
  79. while (t != pTheMasterFileList1)
  80. {
  81. //
  82. // Loop thru all of list 2 looking for this entry
  83. //
  84. iFound = FALSE;
  85. t2 = pTheMasterFileList2->next;
  86. while (t2 != pTheMasterFileList2 && iFound != TRUE)
  87. {
  88. if (0 == _tcsicmp(t->szFileNameEntry, t2->szFileNameEntry))
  89. {iFound = TRUE;}
  90. t2 = t2->next;
  91. }
  92. if (FALSE == iFound)
  93. {
  94. //OutputToConsole(_T("%s\r\n"),t->szFileNameEntry);
  95. OutputToConsole(_T("%s\n"),t->szFileNameEntry);
  96. }
  97. // Get the next entry from list 2
  98. t = t->next;
  99. }
  100. // Loop thru list #2
  101. t2 = pTheMasterFileList2->next;
  102. while (t2 != pTheMasterFileList2)
  103. {
  104. //
  105. // Loop thru all of list 2 looking for this entry
  106. //
  107. iFound = FALSE;
  108. t = pTheMasterFileList1->next;
  109. while (t != pTheMasterFileList1 && iFound != TRUE)
  110. {
  111. if (0 == _tcsicmp(t2->szFileNameEntry, t->szFileNameEntry))
  112. {iFound = TRUE;}
  113. t = t->next;
  114. }
  115. if (FALSE == iFound)
  116. {
  117. //OutputToConsole(_T("%s\r\n"),t2->szFileNameEntry);
  118. OutputToConsole(_T("%s\n"),t2->szFileNameEntry);
  119. }
  120. // Get the next entry from list 2
  121. t2 = t2->next;
  122. }
  123. return;
  124. }
  125. void ReadFileIntoList(LPTSTR szTheFileNameToOpen,MyFileList *pListToFill)
  126. {
  127. ifstream inputfile;
  128. char fileinputbuffer[_MAX_PATH];
  129. TCHAR UnicodeFileBuf[_MAX_PATH];
  130. TCHAR UnicodeFileBuf_Real[_MAX_PATH];
  131. TCHAR szDrive_only[_MAX_DRIVE];
  132. TCHAR szPath_only[_MAX_PATH];
  133. TCHAR szFilename_only[_MAX_FNAME];
  134. TCHAR szFilename_ext_only[_MAX_EXT];
  135. char szAnsiFileName[_MAX_PATH];
  136. WideCharToMultiByte( CP_ACP, 0, (TCHAR*)szTheFileNameToOpen, -1, szAnsiFileName, _MAX_PATH, NULL, NULL );
  137. // Read flat file and put into huge array
  138. inputfile.open(szAnsiFileName, ios::in);
  139. inputfile.getline(fileinputbuffer, sizeof(fileinputbuffer));
  140. do
  141. {
  142. if (*fileinputbuffer)
  143. {
  144. // convert to unicode.
  145. MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fileinputbuffer, -1, (LPTSTR) UnicodeFileBuf, _MAX_PATH);
  146. _tcscpy(UnicodeFileBuf_Real, UnicodeFileBuf);
  147. if (TRUE == g_Flag_c)
  148. {
  149. // take out the path and only store the filename.
  150. _tsplitpath(UnicodeFileBuf, NULL, NULL, szFilename_only, szFilename_ext_only);
  151. _tcscpy(UnicodeFileBuf_Real, szFilename_only);
  152. _tcscat(UnicodeFileBuf_Real, szFilename_ext_only);
  153. _tcscat(UnicodeFileBuf_Real, _T("\0\0"));
  154. }
  155. else if (TRUE == g_Flag_d)
  156. {
  157. // take out the path and only store the filename.
  158. _tsplitpath(UnicodeFileBuf, szDrive_only, szPath_only, NULL, NULL);
  159. _tcscpy(UnicodeFileBuf_Real, szDrive_only);
  160. _tcscat(UnicodeFileBuf_Real, szPath_only);
  161. _tcscat(UnicodeFileBuf_Real, _T("\0\0"));
  162. }
  163. //
  164. // trim spaces or tabs from either side
  165. //
  166. if (TRUE == g_Flag_e)
  167. {
  168. TCHAR *p;
  169. p = UnicodeFileBuf_Real;
  170. _tcscpy(UnicodeFileBuf_Real,StripWhitespace(p));
  171. }
  172. //
  173. // remove everything after the "=" character
  174. //
  175. #ifndef _WIN64
  176. if (TRUE == g_Flag_f)
  177. {
  178. TCHAR *p = NULL;
  179. TCHAR *pDest = NULL;
  180. TCHAR MyDelim = _T('=');
  181. p = UnicodeFileBuf_Real;
  182. // check if there is a defined delimiter.
  183. if( _tcsicmp((const wchar_t *) &g_Flag_f_Data, _T("") ) != 0)
  184. {
  185. MyDelim = (TCHAR) &g_Flag_f_Data[0];
  186. }
  187. pDest = _tcsrchr(p, MyDelim);
  188. if (pDest){*pDest = _T('\0');}
  189. }
  190. #endif
  191. //
  192. // Trim any /r/n characters from the end.
  193. //
  194. TCHAR *p;
  195. p = UnicodeFileBuf_Real;
  196. _tcscpy(UnicodeFileBuf_Real,StripLineFeedReturns(p));
  197. MyFileList *pNew = NULL;
  198. pNew = (MyFileList *)calloc(1, sizeof(MyFileList));
  199. if (pNew)
  200. {
  201. //OutputToConsole(_T("Entry=%s"),UnicodeFileBuf_Real);
  202. _tcscpy(pNew->szFileNameEntry, UnicodeFileBuf_Real);
  203. pNew->prev = NULL;
  204. pNew->next = NULL;
  205. }
  206. // Add it in there.
  207. AddToLinkedListFileList(pListToFill, pNew);
  208. }
  209. } while (inputfile.getline(fileinputbuffer, sizeof(fileinputbuffer)));
  210. inputfile.close();
  211. return;
  212. }
  213. void AddToLinkedListFileList(MyFileList *pMasterList,MyFileList *pEntryToadd)
  214. {
  215. MyFileList *pTempMasterList;
  216. int i;
  217. int bFound = FALSE;
  218. BOOL fReplace = TRUE;
  219. if (!pEntryToadd) {return;}
  220. pTempMasterList = pMasterList->next;
  221. while (pTempMasterList != pMasterList)
  222. {
  223. i = _tcsicmp(pTempMasterList->szFileNameEntry, pEntryToadd->szFileNameEntry);
  224. // if the next entry in the list is less than what we have.
  225. // then
  226. if (i < 0)
  227. {
  228. pTempMasterList = pTempMasterList->next;
  229. // continue
  230. }
  231. if (i == 0)
  232. {
  233. if (fReplace)
  234. {
  235. // replace pTempMasterList
  236. pEntryToadd->next = pTempMasterList->next;
  237. pEntryToadd->prev = pTempMasterList->prev;
  238. (pTempMasterList->prev)->next = pEntryToadd;
  239. (pTempMasterList->next)->prev = pEntryToadd;
  240. free(pTempMasterList);
  241. }
  242. else
  243. {
  244. // don't replace pTempMasterList
  245. free(pEntryToadd);
  246. }
  247. return;
  248. }
  249. if (i > 0)
  250. {
  251. // location found: insert before pTempMasterList
  252. break;
  253. }
  254. }
  255. // insert before pTempMasterList
  256. pEntryToadd->next = pTempMasterList;
  257. pEntryToadd->prev = pTempMasterList->prev;
  258. (pTempMasterList->prev)->next = pEntryToadd;
  259. pTempMasterList->prev = pEntryToadd;
  260. return;
  261. }
  262. void FreeLinkedFileList(MyFileList *pList)
  263. {
  264. if (!pList) {return;}
  265. MyFileList *t = NULL, *p = NULL;
  266. t = pList->next;
  267. while (t != pList)
  268. {
  269. p = t->next;
  270. free(t);
  271. t = p;
  272. }
  273. t->prev = t;
  274. t->next = t;
  275. return;
  276. }
  277. BOOL IsFileExist(LPCTSTR szFile)
  278. {
  279. // Check if the file has expandable Environment strings
  280. LPTSTR pch = NULL;
  281. pch = _tcschr( (LPTSTR) szFile, _T('%'));
  282. if (pch)
  283. {
  284. TCHAR szValue[_MAX_PATH];
  285. _tcscpy(szValue,szFile);
  286. if (!ExpandEnvironmentStrings( (LPCTSTR)szFile, szValue, _MAX_PATH))
  287. {_tcscpy(szValue,szFile);}
  288. return (GetFileAttributes(szValue) != 0xFFFFFFFF);
  289. }
  290. else
  291. {
  292. return (GetFileAttributes(szFile) != 0xFFFFFFFF);
  293. }
  294. }
  295. //***************************************************************************
  296. //* NAME: StripWhitespace *
  297. //* SYNOPSIS: Strips spaces and tabs from both sides of given string. *
  298. //***************************************************************************
  299. LPSTR StripWhitespaceA( LPSTR pszString )
  300. {
  301. LPSTR pszTemp = NULL;
  302. if ( pszString == NULL ) {
  303. return NULL;
  304. }
  305. while ( *pszString == ' ' || *pszString == '\t' ) {
  306. pszString += 1;
  307. }
  308. // Catch case where string consists entirely of whitespace or empty string.
  309. if ( *pszString == '\0' ) {
  310. return pszString;
  311. }
  312. pszTemp = pszString;
  313. pszString += lstrlenA(pszString) - 1;
  314. while ( *pszString == ' ' || *pszString == '\t' ) {
  315. *pszString = '\0';
  316. pszString -= 1;
  317. }
  318. return pszTemp;
  319. }
  320. //***************************************************************************
  321. //* NAME: StripWhitespace *
  322. //* SYNOPSIS: Strips spaces and tabs from both sides of given string. *
  323. //***************************************************************************
  324. LPTSTR StripWhitespace(LPTSTR pszString )
  325. {
  326. LPTSTR pszTemp = NULL;
  327. if ( pszString == NULL ) {
  328. return NULL;
  329. }
  330. while ( *pszString == _T(' ') || *pszString == _T('\t') ) {
  331. pszString += 1;
  332. }
  333. // Catch case where string consists entirely of whitespace or empty string.
  334. if ( *pszString == _T('\0') ) {
  335. return pszString;
  336. }
  337. pszTemp = pszString;
  338. pszString += lstrlenW(pszString) - 1;
  339. while ( *pszString == _T(' ') || *pszString == _T('\t') ) {
  340. *pszString = _T('\0');
  341. pszString -= 1;
  342. }
  343. return pszTemp;
  344. }
  345. //***************************************************************************
  346. //* NAME: StripLineFeedReturns *
  347. //* SYNOPSIS: Strips linefeeds and returns from both sides of given string*
  348. //***************************************************************************
  349. LPTSTR StripLineFeedReturns(LPTSTR pszString )
  350. {
  351. LPTSTR pszTemp = NULL;
  352. if ( pszString == NULL ) {
  353. return NULL;
  354. }
  355. while ( *pszString == _T('\n') || *pszString == _T('\r') ) {
  356. pszString += 1;
  357. }
  358. // Catch case where string consists entirely of whitespace or empty string.
  359. if ( *pszString == _T('\0') ) {
  360. return pszString;
  361. }
  362. pszTemp = pszString;
  363. pszString += lstrlenW(pszString) - 1;
  364. while ( *pszString == _T('\n') || *pszString == _T('\r') ) {
  365. *pszString = _T('\0');
  366. pszString -= 1;
  367. }
  368. return pszTemp;
  369. }