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.

203 lines
4.2 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #define DIM(x) (sizeof(x) / sizeof(x[0]))
  6. FILE *file1 = NULL;
  7. FILE *file2 = NULL;
  8. FILE *fdiff = NULL;
  9. FILE *fmerg = NULL;
  10. char *pszFile1 = NULL;
  11. char *pszFile2 = NULL;
  12. void CleanUp()
  13. {
  14. if (file1)
  15. {
  16. fclose(file1);
  17. }
  18. if (file2)
  19. {
  20. fclose(file2);
  21. }
  22. if (fdiff)
  23. {
  24. fclose(fdiff);
  25. }
  26. if (fmerg)
  27. {
  28. fclose(fmerg);
  29. }
  30. if (pszFile1)
  31. {
  32. delete [] pszFile1;
  33. }
  34. if (pszFile2)
  35. {
  36. delete [] pszFile2;
  37. }
  38. }
  39. void ErrorExit(char *fmt, ...)
  40. {
  41. va_list va;
  42. va_start(va, fmt);
  43. vprintf(fmt, va);
  44. CleanUp();
  45. exit(1);
  46. }
  47. void ShowHow()
  48. {
  49. ErrorExit("USAGE:\n\n" \
  50. "dhcmpmrg <file1> <file2> <diff> <merge>\n"
  51. );
  52. }
  53. FILE *OpenIt(char *pszName, char *pszMode)
  54. {
  55. FILE *f = fopen(pszName, pszMode);
  56. if (!f)
  57. {
  58. ErrorExit("Couldn't open %s - mode %s\nError is %s\n", pszName, pszMode,
  59. strerror(NULL));
  60. }
  61. return f;
  62. }
  63. void Merge(char *pszFile, char tok, char *pszFileName)
  64. {
  65. char szCmp[1024];
  66. // Walk the diff file line by line
  67. while (fgets(szCmp, DIM(szCmp), fdiff) && (szCmp[0] == tok))
  68. {
  69. int len = strlen(szCmp);
  70. if (szCmp[len - 1] == '\n')
  71. {
  72. szCmp[len - 1] = 0;
  73. }
  74. // Look for a BackTrace the K & R way
  75. char *pszbt = strstr(szCmp, "BackTrace");
  76. // At this point pszbt should point to a string of the form
  77. // BackTrace2032
  78. if (pszbt)
  79. {
  80. fprintf(fmerg, "%s\n", szCmp);
  81. // Look for the specific back trace
  82. char *pStart = strstr(pszFile, pszbt);
  83. char *pEnd = pStart;
  84. if (pStart)
  85. {
  86. // Walk backwards to find the start of this line
  87. while ((pStart != pszFile) && (*pStart != '\n'))
  88. {
  89. pStart--;
  90. }
  91. if (*pStart == '\n')
  92. {
  93. pStart++;
  94. }
  95. // Walk forwards to find the end of this stack trace.
  96. // We use a blank line as the delimiter here.
  97. while (*pEnd != 0)
  98. {
  99. // If we hit a CR we need to look for and eat spaces
  100. // before checking for a second CR or EOF
  101. if (*pEnd == '\n')
  102. {
  103. pEnd++;
  104. while ((*pEnd != 0) && (*pEnd != '\n') && isspace(*pEnd))
  105. pEnd++;
  106. if ((*(pEnd) == '\n') || (*pEnd == 0))
  107. {
  108. break;
  109. }
  110. }
  111. pEnd++;
  112. }
  113. fwrite(pStart, sizeof(char), pEnd - pStart, fmerg);
  114. fprintf(fmerg, "\n");
  115. }
  116. else
  117. {
  118. // Yikes!
  119. fprintf(fmerg, "Couldn't find %s in %s...\n", pszbt, pszFileName);
  120. printf("Couldn't find %s in %s...\n", pszbt, pszFileName);
  121. }
  122. }
  123. }
  124. }
  125. long fsize(FILE *f)
  126. {
  127. long pos = ftell(f);
  128. long size;
  129. fseek(f, 0, SEEK_END);
  130. size = ftell(f);
  131. fseek(f, pos, SEEK_SET);
  132. return size;
  133. }
  134. int _cdecl main(int argc, char **argv)
  135. {
  136. if (argc < 5)
  137. {
  138. ShowHow();
  139. }
  140. // This is not what I would call robust...
  141. file1 = OpenIt(argv[1], "r");
  142. file2 = OpenIt(argv[2], "r");
  143. fdiff = OpenIt(argv[3], "r");
  144. fmerg = OpenIt(argv[4], "w");
  145. long size;
  146. printf("Reading %s...\n", argv[1]);
  147. size = fsize(file1);
  148. pszFile1 = new char[size + 1];
  149. fread(pszFile1, sizeof(char), size, file1);
  150. *(pszFile1 + size) = 0;
  151. printf("Reading %s...\n", argv[2]);
  152. size = fsize(file2);
  153. pszFile2 = new char[size + 1];
  154. fread(pszFile2, sizeof(char), size, file2);
  155. *(pszFile2 + size) = 0;
  156. printf("Merging leaks...\n");
  157. Merge(pszFile2, '+', argv[2]);
  158. printf("Merging frees...\n");
  159. Merge(pszFile1, '-', argv[1]);
  160. CleanUp();
  161. return 0;
  162. }