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.

246 lines
3.5 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. util.c
  5. Abstract:
  6. Utility functions for dbghelp.
  7. Author:
  8. Pat Styles (patst) 6-Dec-2001
  9. Environment:
  10. User Mode
  11. --*/
  12. #include "private.h"
  13. #include "symbols.h"
  14. #include "globals.h"
  15. #include <dbhpriv.h>
  16. int
  17. ReverseCmp(
  18. char *one,
  19. char *two
  20. )
  21. {
  22. char *p1;
  23. char *p2;
  24. int i;
  25. p1 = one + strlen(one) - 1;
  26. p2 = two + strlen(two) - 1;
  27. for (i = 0; p1 >= one && p2 >= two; i++, p1--, p2--) {
  28. if (*p1 != *p2)
  29. return i;
  30. }
  31. return i;
  32. }
  33. BOOL
  34. UpdateBestSrc(
  35. char *cmp,
  36. char *trg,
  37. char *src
  38. )
  39. {
  40. assert(cmp && *cmp && trg && src);
  41. if (!*trg || (ReverseCmp(cmp, src) > ReverseCmp(cmp, trg))) {
  42. strcpy(trg, src);
  43. return true;
  44. }
  45. return false;
  46. }
  47. BOOL
  48. ShortNodeName(
  49. char *in,
  50. char *out,
  51. size_t osize
  52. )
  53. {
  54. char *p;
  55. assert(in);
  56. if (osize < 1 || !out)
  57. return false;
  58. CopyString(out, in, osize);
  59. for (p = out + strlen(out) -1; p >= out; p--) {
  60. if (*p == '~') {
  61. *(++p) = 'X';
  62. *(++p) = 0;
  63. return true;
  64. }
  65. if (*p < '0' || *p > '9')
  66. return false;
  67. }
  68. return false;
  69. }
  70. BOOL
  71. ShortFileName(
  72. char *in,
  73. char *out,
  74. size_t osize)
  75. {
  76. char drive[_MAX_DRIVE + 1];
  77. char dir[_MAX_DIR + 1];
  78. char fname[_MAX_FNAME + 1];
  79. char ext[_MAX_EXT + 1];
  80. char nfname[_MAX_FNAME + 1];
  81. BOOL rc;
  82. _splitpath(in, drive, dir, fname, ext);
  83. rc = ShortNodeName(fname, nfname, DIMA(nfname));
  84. CopyString(out, drive, osize);
  85. CatString(out, dir, osize);
  86. CatString(out, nfname, osize);
  87. CatString(out, ext, osize);
  88. return rc;
  89. }
  90. BOOL
  91. ToggleFailCriticalErrors(
  92. BOOL reset
  93. )
  94. {
  95. static UINT oldmode = 0;
  96. if (!option(SYMOPT_FAIL_CRITICAL_ERRORS))
  97. return false;
  98. if (reset)
  99. SetErrorMode(oldmode);
  100. else
  101. oldmode = SetErrorMode(SEM_FAILCRITICALERRORS);
  102. return true;
  103. }
  104. DWORD
  105. fnGetFileAttributes(
  106. char *lpFileName
  107. )
  108. {
  109. DWORD rc;
  110. SetCriticalErrorMode();
  111. rc = GetFileAttributes(lpFileName);
  112. ResetCriticalErrorMode();
  113. return rc;
  114. }
  115. void
  116. rtrim(
  117. LPSTR sz
  118. )
  119. {
  120. LPSTR p;
  121. assert(sz);
  122. for (p = sz + strlen(sz) - 1; p >= sz; p--) {
  123. if (!isspace(*p))
  124. break;
  125. *p = 0;
  126. }
  127. }
  128. void
  129. ltrim(
  130. LPSTR sz
  131. )
  132. {
  133. LPSTR p;
  134. assert(sz);
  135. for (p = sz; *p; p++) {
  136. if (!isspace(*p))
  137. break;
  138. }
  139. if (p == sz)
  140. return;
  141. for (; *p; p++, sz++)
  142. *sz = *p;
  143. *sz = 0;
  144. }
  145. void
  146. trim(
  147. LPSTR sz
  148. )
  149. {
  150. rtrim(sz);
  151. ltrim(sz);
  152. }
  153. char *errortext(DWORD err)
  154. {
  155. char *sz;
  156. switch (err)
  157. {
  158. case ERROR_FILE_NOT_FOUND:
  159. return "file not found";
  160. case ERROR_PATH_NOT_FOUND:
  161. return "path not found";
  162. case ERROR_NOT_READY:
  163. return "drive not ready";
  164. case ERROR_ACCESS_DENIED:
  165. return "access is denied";
  166. default:
  167. sz = FormatStatus(err);
  168. return (sz) ? sz : "";
  169. }
  170. }
  171. VOID
  172. RemoveTrailingBackslash(
  173. LPSTR sz
  174. )
  175. {
  176. int i;
  177. assert(sz);
  178. i = lstrlen(sz);
  179. if (!i)
  180. return;
  181. if (sz[i - 1] == '\\')
  182. sz[i - 1] = 0;
  183. }