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.

590 lines
20 KiB

  1. #include "compdir.h"
  2. #define printtext( Text) fprintf( stdout, Text);
  3. #define printstring( String) fprintf( stdout, "%s\n",String);
  4. #define Next( List) ( List == NULL) ? NULL : &(*List).Next
  5. #define CALCULATE_HEIGHT( Node) \
  6. \
  7. if ( (*Node).Left == NULL) \
  8. if ( (*Node).Right == NULL) \
  9. (*Node).Height = 1; \
  10. else \
  11. (*Node).Height = (*(*Node).Right).Height + 1; \
  12. else \
  13. if ( (*Node).Right == NULL) \
  14. (*Node).Height = (*(*Node).Left).Height + 1; \
  15. else \
  16. (*Node).Height = ( (*(*Node).Right).Height > (*(*Node).Left).Height) ? \
  17. (*(*Node).Right).Height + 1 : (*(*Node).Left).Height + 1;
  18. #define ROTATELEFT( List) \
  19. \
  20. LinkedFileList TmpPtr; \
  21. \
  22. TmpPtr = (**List).Right; \
  23. (**List).Right = (*TmpPtr).Left; \
  24. (*TmpPtr).Left = (*List); \
  25. *List = TmpPtr; \
  26. \
  27. if ( (*(**List).Left).Right != NULL) \
  28. (*(**List).Left).Last = (*(*(**List).Left).Right).Last; \
  29. else \
  30. (*(**List).Left).Last = (**List).Left; \
  31. (**List).First = (*(**List).Left).First; \
  32. \
  33. CALCULATE_HEIGHT( (**List).Left); \
  34. \
  35. CALCULATE_HEIGHT(*List);
  36. #define ROTATERIGHT( List) \
  37. \
  38. LinkedFileList TmpPtr; \
  39. \
  40. TmpPtr = (**List).Left; \
  41. (**List).Left = (*TmpPtr).Right; \
  42. (*TmpPtr).Right = (*List); \
  43. *List = TmpPtr; \
  44. \
  45. if ( (*(**List).Right).Left != NULL) \
  46. (*(**List).Right).First = (*(*(**List).Right).Left).First; \
  47. else \
  48. (*(**List).Right).First = (**List).Right; \
  49. (**List).Last = (*(**List).Right).Last; \
  50. \
  51. CALCULATE_HEIGHT( (**List).Right); \
  52. \
  53. CALCULATE_HEIGHT( *List);
  54. #define ROTATEUPLEFT( List) \
  55. \
  56. LinkedFileList TmpPtr; \
  57. \
  58. TmpPtr = (*(**List).Right).Left; \
  59. (*(**List).Right).Left = (*TmpPtr).Right; \
  60. (*TmpPtr).Right = (**List).Right; \
  61. (**List).Right = (*TmpPtr).Left; \
  62. (*TmpPtr).Left = (*List); \
  63. *List = TmpPtr; \
  64. \
  65. if ( (*(**List).Left).Right != NULL) \
  66. (*(**List).Left).Last = (*(*(**List).Left).Right).Last; \
  67. else \
  68. (*(**List).Left).Last = (**List).Left; \
  69. (**List).First = (*(**List).Left).First; \
  70. \
  71. if ( (*(**List).Right).Left != NULL) \
  72. (*(**List).Right).First = (*(*(**List).Right).Left).First; \
  73. else \
  74. (*(**List).Right).First = (**List).Right; \
  75. (**List).Last = (*(**List).Right).Last; \
  76. \
  77. CALCULATE_HEIGHT( (**List).Left); \
  78. \
  79. CALCULATE_HEIGHT( (**List).Right); \
  80. \
  81. CALCULATE_HEIGHT( *List);
  82. #define ROTATEUPRIGHT( List) \
  83. \
  84. LinkedFileList TmpPtr; \
  85. \
  86. TmpPtr = (*(**List).Left).Right; \
  87. (*(**List).Left).Right = (*TmpPtr).Left; \
  88. (*TmpPtr).Left = (**List).Left; \
  89. (**List).Left = (*TmpPtr).Right; \
  90. (*TmpPtr).Right = (*List); \
  91. *List = TmpPtr; \
  92. \
  93. if ( (*(**List).Right).Left != NULL) \
  94. (*(**List).Right).First = (*(*(**List).Right).Left).First; \
  95. else \
  96. (*(**List).Right).First = (**List).Right; \
  97. (**List).Last = (*(**List).Right).Last; \
  98. \
  99. if ( (*(**List).Left).Right != NULL) \
  100. (*(**List).Left).Last = (*(*(**List).Left).Right).Last; \
  101. else \
  102. (*(**List).Left).Last = (**List).Left; \
  103. (**List).First = (*(**List).Left).First; \
  104. \
  105. CALCULATE_HEIGHT( (**List).Right); \
  106. \
  107. CALCULATE_HEIGHT( (**List).Left); \
  108. \
  109. CALCULATE_HEIGHT( *List);
  110. //
  111. // Walk down list and add Nodes
  112. //
  113. BOOL AddToList( LinkedFileList Node, LinkedFileList *List)
  114. {
  115. int Result;
  116. BOOL Changed = FALSE;
  117. BOOL ChangedVal = FALSE;
  118. //
  119. // If Node is empty do nothing
  120. //
  121. if ( Node == NULL)
  122. {
  123. return Changed;
  124. }
  125. //
  126. // If list is empty just point to Node
  127. //
  128. if ( *List == NULL)
  129. {
  130. *List = Node;
  131. Changed = TRUE;
  132. //
  133. // Otherwise go down the list and add
  134. // in sorted order
  135. //
  136. } else
  137. {
  138. Result = _stricmp( (*Node).Name, (**List).Name);
  139. if ( Result < 0)
  140. {
  141. if ( (**List).Left == NULL)
  142. {
  143. (**List).Left = Node;
  144. (**List).First = (*Node).First;
  145. (*(*Node).Last).Next = *List;
  146. Changed = TRUE;
  147. CALCULATE_HEIGHT(*List);
  148. } else
  149. {
  150. ChangedVal = AddToList( Node, &(**List).Left);
  151. if ( ChangedVal)
  152. {
  153. if ( (**List).First != (*(**List).Left).First)
  154. {
  155. Changed = TRUE;
  156. (**List).First = (*(**List).Left).First;
  157. }
  158. if ( (*(*(**List).Left).Last).Next != *List)
  159. {
  160. Changed = TRUE;
  161. (*(*(**List).Left).Last).Next = *List;
  162. }
  163. }
  164. Result = (**List).Height;
  165. CALCULATE_HEIGHT( *List);
  166. if ( (**List).Height != Result)
  167. {
  168. Changed = TRUE;
  169. }
  170. }
  171. if ( Changed)
  172. {
  173. if ( (**List).Right == NULL)
  174. {
  175. if ( (*(**List).Left).Height > 1 )
  176. {
  177. if ( (*(**List).Left).Left == NULL )
  178. {
  179. ROTATEUPRIGHT( List);
  180. Changed = TRUE;
  181. } else
  182. {
  183. ROTATERIGHT( List);
  184. Changed = TRUE;
  185. }
  186. }
  187. } else if ( ( (*(**List).Left).Height - (*(**List).Right).Height) > 1)
  188. {
  189. ROTATERIGHT( List);
  190. Changed = TRUE;
  191. }
  192. }
  193. }
  194. else if ( Result > 0)
  195. {
  196. if ( (**List).Right == NULL)
  197. {
  198. (**List).Right = Node;
  199. (**List).Next = (*Node).First;
  200. (**List).Last = (*Node).Last;
  201. Changed = TRUE;
  202. CALCULATE_HEIGHT( *List);
  203. } else
  204. {
  205. ChangedVal = AddToList( Node, &(**List).Right);
  206. if ( ChangedVal)
  207. {
  208. if ( (**List).Next != (*(**List).Right).First)
  209. {
  210. Changed = TRUE;
  211. (**List).Next = (*(**List).Right).First;
  212. }
  213. if ( (**List).Last != (*(**List).Right).Last)
  214. {
  215. Changed = TRUE;
  216. (**List).Last = (*(**List).Right).Last;
  217. }
  218. }
  219. Result = (**List).Height;
  220. CALCULATE_HEIGHT( *List);
  221. if ( (**List).Height != Result)
  222. {
  223. Changed = TRUE;
  224. }
  225. }
  226. if ( Changed)
  227. {
  228. if ( (**List).Left == NULL)
  229. {
  230. if ( (*(**List).Right).Height > 1 )
  231. {
  232. if ( (*(**List).Right).Right == NULL )
  233. {
  234. ROTATEUPLEFT( List);
  235. Changed = TRUE;
  236. } else
  237. {
  238. ROTATELEFT( List);
  239. Changed = TRUE;
  240. }
  241. }
  242. }
  243. else if ( ( (*(**List).Right).Height - (*(**List).Left).Height) > 1)
  244. {
  245. ROTATELEFT( List);
  246. Changed = TRUE;
  247. }
  248. }
  249. } else if ( Result == 0)
  250. {
  251. // Don't add if already here
  252. }
  253. }
  254. return Changed;
  255. } /* AddToList */
  256. LPSTR CombineThreeStrings( char *FirstString, char *SecondString, char *ThirdString)
  257. {
  258. char *String;
  259. String = malloc( strlen( FirstString) + strlen( SecondString) + strlen( ThirdString) + 1);
  260. if ( String == NULL)
  261. {
  262. OutOfMem();
  263. }
  264. strcpy( String, FirstString);
  265. strcpy( &(String[strlen( FirstString)]), SecondString);
  266. strcpy( &(String[strlen( FirstString) + strlen( SecondString)]), ThirdString);
  267. return( String);
  268. } /* CombineThreeStrings */
  269. void CreateNode( LinkedFileList *Node, WIN32_FIND_DATA *Buff)
  270. {
  271. (*Node) = malloc( sizeof( struct NodeStruct));
  272. if ( (*Node) == NULL)
  273. {
  274. OutOfMem();
  275. }
  276. (**Node).Name = _strrev( _strdup( (*Buff).cFileName));
  277. if ( (**Node).Name == NULL)
  278. {
  279. OutOfMem();
  280. }
  281. if ( !fDontLowerCase)
  282. {
  283. _strlwr( (**Node).Name);
  284. }
  285. strcpy( (**Node).Flag, "");
  286. (**Node).Attributes = (*Buff).dwFileAttributes;
  287. if ( (*Buff).dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  288. {
  289. strcpy( (**Node).Flag, "DIR");
  290. }
  291. (**Node).SizeHigh = (*Buff).nFileSizeHigh;
  292. (**Node).SizeLow = (*Buff).nFileSizeLow;
  293. (**Node).Time.dwLowDateTime = (*Buff).ftLastWriteTime.dwLowDateTime;
  294. (**Node).Time.dwHighDateTime = (*Buff).ftLastWriteTime.dwHighDateTime;
  295. (**Node).First = (*Node);
  296. (**Node).Last = (*Node);
  297. (**Node).Left = NULL;
  298. (**Node).Next = NULL;
  299. (**Node).Process = ProcessModeDefault;
  300. (**Node).Right = NULL;
  301. (**Node).DiffNode = NULL;
  302. (**Node).Height = 1;
  303. } /* CreateNode */
  304. void CreateNameNode( LinkedFileList *Node, char *Name)
  305. {
  306. (*Node) = malloc( sizeof( struct NodeStruct));
  307. if ( (*Node) == NULL)
  308. {
  309. OutOfMem();
  310. }
  311. (**Node).Name = _strdup( Name);
  312. if ( (**Node).Name == NULL)
  313. {
  314. OutOfMem();
  315. }
  316. if ( !fDontLowerCase)
  317. {
  318. _strlwr( (**Node).Name);
  319. }
  320. strcpy( (**Node).Flag, "");
  321. (**Node).Attributes = 0;
  322. (**Node).SizeHigh = 0;
  323. (**Node).SizeLow = 0;
  324. (**Node).Time.dwLowDateTime = 0;
  325. (**Node).Time.dwHighDateTime = 0;
  326. (**Node).First = (*Node);
  327. (**Node).Last = (*Node);
  328. (**Node).Left = NULL;
  329. (**Node).Next = NULL;
  330. (**Node).Process = TRUE;
  331. (**Node).Right = NULL;
  332. (**Node).DiffNode = NULL;
  333. (**Node).Height = 1;
  334. } /* CreateNode */
  335. void DuplicateNode( LinkedFileList FirstNode, LinkedFileList *SecondNode)
  336. {
  337. (*SecondNode) = malloc( sizeof( struct NodeStruct));
  338. if ( (*SecondNode) == NULL)
  339. {
  340. OutOfMem();
  341. }
  342. (**SecondNode).Name = _strdup( (*FirstNode).Name);
  343. if ( (**SecondNode).Name == NULL)
  344. {
  345. OutOfMem();
  346. }
  347. if ( !fDontLowerCase)
  348. {
  349. _strlwr( (**SecondNode).Name);
  350. }
  351. strcpy( (**SecondNode).Flag, (*FirstNode).Flag);
  352. (**SecondNode).Attributes = (*FirstNode).Attributes;
  353. (**SecondNode).SizeHigh = (*FirstNode).SizeHigh;
  354. (**SecondNode).SizeLow = (*FirstNode).SizeLow;
  355. (**SecondNode).Time.dwLowDateTime = (*FirstNode).Time.dwLowDateTime;
  356. (**SecondNode).Time.dwHighDateTime = (*FirstNode).Time.dwHighDateTime;
  357. (**SecondNode).First = (*SecondNode);
  358. (**SecondNode).Last = (*SecondNode);
  359. (**SecondNode).Left = NULL;
  360. (**SecondNode).Next = NULL;
  361. (**SecondNode).Process = ProcessModeDefault;
  362. (**SecondNode).Right = NULL;
  363. (**SecondNode).DiffNode = NULL;
  364. (**SecondNode).Height = 0;
  365. } // DuplicateNode
  366. LinkedFileList *FindInList( char *Name, LinkedFileList *List)
  367. {
  368. int Result;
  369. LinkedFileList *tmpptr = List;
  370. while ( *tmpptr != NULL)
  371. {
  372. Result = _stricmp( (**tmpptr).Name, Name);
  373. if ( Result == 0)
  374. {
  375. return tmpptr;
  376. }
  377. if ( Result > 0)
  378. {
  379. tmpptr = &(**tmpptr).Left;
  380. }
  381. if ( Result < 0)
  382. {
  383. tmpptr = &(**tmpptr).Right;
  384. }
  385. }
  386. return NULL;
  387. } /* FindInList */
  388. BOOL FindInMatchListTop( char *Name, LinkedFileList *List)
  389. {
  390. int Result;
  391. LinkedFileList *tmpptr = List;
  392. while ( *tmpptr != NULL)
  393. {
  394. Result = _stricmp( (**tmpptr).Name, Name);
  395. if ( Result == 0)
  396. {
  397. return TRUE;
  398. }
  399. if ( strchr( (**tmpptr).Name, '*') != NULL)
  400. {
  401. if ( Match( (**tmpptr).Name, Name))
  402. {
  403. return TRUE;
  404. }
  405. }
  406. if ( Result > 0)
  407. {
  408. tmpptr = &(**tmpptr).Left;
  409. }
  410. if ( Result < 0)
  411. {
  412. tmpptr = &(**tmpptr).Right;
  413. }
  414. }
  415. return FALSE;
  416. } /* FindInList */
  417. BOOL FindInMatchListFront( char *Name, LinkedFileList *List)
  418. {
  419. LinkedFileList *tmpptr = List;
  420. if ( *tmpptr != NULL)
  421. {
  422. tmpptr = &(**tmpptr).First;
  423. }
  424. while ( *tmpptr != NULL)
  425. {
  426. if ( Match( (**tmpptr).Name, Name))
  427. {
  428. return TRUE;
  429. }
  430. tmpptr = &(**tmpptr).Next;
  431. }
  432. return FALSE;
  433. } /* FindInList */
  434. //
  435. // Walk down list and free each entry
  436. //
  437. void FreeList( LinkedFileList *List)
  438. {
  439. if ( (*List) != NULL)
  440. {
  441. FreeList( &(**List).Left);
  442. FreeList( &(**List).Right);
  443. FreeList( &(**List).DiffNode);
  444. free( (**List).Name);
  445. free( *List);
  446. }
  447. } // FreeList
  448. void PrintTree( LinkedFileList List, int Level)
  449. {
  450. int Counter = 0;
  451. if ( List == NULL)
  452. {
  453. return;
  454. }
  455. PrintTree( (*List).Right, Level + 1);
  456. while ( Counter++ < Level)
  457. {
  458. fprintf( stdout, " ");
  459. }
  460. fprintf( stdout, "%s %d\n", (*List).Name, (*List).Height);
  461. PrintTree( (*List).Left, Level + 1);
  462. } // Print Tree
  463. //
  464. // This function is is the same as strcat except
  465. // that it does the memory allocation for the string
  466. //
  467. LPSTR MyStrCat( char *FirstString, char *SecondString)
  468. {
  469. char *String;
  470. String = malloc( strlen( FirstString) + strlen( SecondString) + 1);
  471. if ( String == NULL)
  472. {
  473. OutOfMem();
  474. }
  475. strcpy( String, FirstString);
  476. strcpy( &(String[strlen( FirstString)]), SecondString);
  477. return( String);
  478. } /* MyStrCat */
  479. BOOL Match( char *Pat, char* Text)
  480. {
  481. switch ( *Pat)
  482. {
  483. case '\0':
  484. return *Text == '\0';
  485. case '?':
  486. return *Text != '\0' && Match( Pat + 1, Text + 1);
  487. case '*':
  488. do
  489. {
  490. if ( Match( Pat + 1, Text))
  491. {
  492. return TRUE;
  493. }
  494. } while ( *Text++);
  495. return FALSE;
  496. default:
  497. return toupper( *Text) == toupper( *Pat) && Match( Pat + 1, Text + 1);
  498. }
  499. } /* Match */
  500. /*
  501. LinkedFileList *Next( LinkedFileList List)
  502. {
  503. if ( List == NULL)
  504. {
  505. return NULL;
  506. }
  507. return &(*List).Next;
  508. } /* /* Next */
  509. void OutOfMem( void)
  510. {
  511. fprintf( stderr, "-out of memory-\n");
  512. exit(1);
  513. } // OutOfMem