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.

185 lines
4.8 KiB

  1. /*
  2. read a file that
  3. has semicolon initiated comments
  4. and lines with five or six space delimited columns
  5. make the sixth column match the last path element of the first column, %02d.cat, with %02d increasing.
  6. */
  7. #include "strtok_r.h"
  8. #include "strtok_r.c"
  9. #if defined(NULL)
  10. #undef NULL
  11. #endif
  12. #define NUMBER_OF(x) (sizeof(x)/sizeof((x)[0]))
  13. #include <stdio.h>
  14. #include <string.h>
  15. typedef unsigned long ULONG;
  16. typedef int BOOL;
  17. #define TRUE 1
  18. #define FALSE 0
  19. #define MAX_PATH 260
  20. #pragma warning(disable:4706) /* assignment within conditional */
  21. #pragma warning(disable:4100) /* unused parameter */
  22. BOOL IsComment(const char * s)
  23. {
  24. s += strspn(s, " \t");
  25. if (*s == 0 || *s == ';')
  26. return TRUE;
  27. return FALSE;
  28. }
  29. void SplitFields(char * String, const char * Delims, char ** Fields, ULONG MaxNumberOfFields, ULONG * NumberOfFields)
  30. {
  31. char * Field = 0;
  32. char * strtok_state = 0;
  33. *NumberOfFields = 0;
  34. for ( *NumberOfFields += ((Fields[*NumberOfFields] = Field = strtok_r(String, Delims, &strtok_state)) ? 1 : 0) ;
  35. Field && *NumberOfFields < MaxNumberOfFields;
  36. *NumberOfFields += ((Fields[*NumberOfFields] = Field = strtok_r(NULL, Delims, &strtok_state)) ? 1 : 0) )
  37. {
  38. }
  39. }
  40. char * GetLastPathElement(char * s)
  41. {
  42. char * t = strrchr(s, '/');
  43. char * u = strrchr(s, '\\');
  44. if (t == NULL && u == NULL)
  45. return NULL;
  46. if (t == NULL && u != NULL)
  47. return u + 1;
  48. if (u == NULL && t != NULL)
  49. return t + 1;
  50. if (t > u)
  51. return t + 1;
  52. return u + 1;
  53. }
  54. char * GetExtension(char * s)
  55. {
  56. return strrchr(s, '.');
  57. }
  58. void ChangeExtension(char * s, const char * t)
  59. {
  60. s = GetExtension(s);
  61. if (s != NULL)
  62. strcpy(s, t);
  63. }
  64. void Echo(const char * s)
  65. {
  66. printf("%s\n", s);
  67. }
  68. BOOL Readline(FILE * File, char * Buffer, ULONG BufferSize)
  69. {
  70. return (fgets(Buffer, BufferSize, File) ? TRUE : FALSE);
  71. }
  72. void TrimLeadingAndTrailingWhitespace(char * s)
  73. {
  74. char * t = s + strspn(s, " \t");
  75. if (t != s)
  76. memmove(s, t, strlen(t) + 1);
  77. if (*s == 0)
  78. return;
  79. for ( t = s + strlen(s) ; t != s ; --t )
  80. {
  81. if (strchr(" \t", *(t - 1)))
  82. *(t - 1) = 0;
  83. else
  84. break;
  85. }
  86. }
  87. void RemoveTrailingNewline(char * s)
  88. {
  89. if (*s == 0)
  90. return;
  91. s += strlen(s) - 1;
  92. if (*s == '\n')
  93. *s = 0;
  94. }
  95. void UniquizeFusionCatalogNames(int argc, char ** argv)
  96. {
  97. static const char Function[] = __FUNCTION__;
  98. static const char SourceFile[] = __FILE__;
  99. #define SourceLine ((int)__LINE__)
  100. ULONG Counter = 0;
  101. char OriginalLine[MAX_PATH * 6];
  102. char Line[MAX_PATH * 6];
  103. char * Fields[16] = { 0 };
  104. ULONG NumberOfFields = 0;
  105. char * Extension = 0;
  106. ULONG i = 0;
  107. char * LastPathElement = 0;
  108. FILE * File;
  109. size_t LineLength;
  110. File = stdin;
  111. if (argv[0] != NULL && argv[1] != NULL && argv[1][0] != 0)
  112. {
  113. File = fopen(argv[1], "r");
  114. if (File == NULL)
  115. {
  116. fprintf(stderr, "%s:%s:%d failed to open %s\n", SourceFile, Function, SourceLine, argv[1]);
  117. goto Exit;
  118. }
  119. }
  120. while (Readline(File, OriginalLine, NUMBER_OF(OriginalLine)))
  121. {
  122. strcpy(Line, OriginalLine);
  123. _strlwr(Line);
  124. RemoveTrailingNewline(Line);
  125. if (IsComment(Line))
  126. {
  127. Echo(Line);
  128. continue;
  129. }
  130. TrimLeadingAndTrailingWhitespace(Line);
  131. LineLength = strlen(Line);
  132. SplitFields(Line, " \t", Fields, NUMBER_OF(Fields), &NumberOfFields);
  133. //printf("NumberOfFields %lu\n", NumberOfFields);
  134. if (NumberOfFields != 5 && NumberOfFields != 6)
  135. {
  136. Echo(Line);
  137. continue;
  138. }
  139. if (NumberOfFields == 5)
  140. {
  141. Fields[5] = Line + LineLength + 1;
  142. }
  143. LastPathElement = GetLastPathElement(Fields[0]);
  144. if (LastPathElement == NULL)
  145. {
  146. fprintf(stderr, "%s:%s:%d failed to get last path element, Line=%s\n", SourceFile, Function, SourceLine, OriginalLine);
  147. goto Exit;
  148. }
  149. memmove(Fields[5], LastPathElement, strlen(LastPathElement) + 1);
  150. Extension = GetExtension(Fields[5]);
  151. if (Extension == NULL)
  152. {
  153. fprintf(stderr, "%s:%s:%d failed to get extension, Line=%s\n", SourceFile, Function, SourceLine, OriginalLine);
  154. goto Exit;
  155. }
  156. sprintf(Extension, "%03lu.cat", ++Counter);
  157. for ( i = 0 ; i != 5 ; ++i)
  158. {
  159. Fields[i][strlen(Fields[i])] = ' ';
  160. }
  161. Echo(Line);
  162. }
  163. Exit:
  164. return;
  165. }
  166. int __cdecl main(int argc, char ** argv)
  167. {
  168. UniquizeFusionCatalogNames(argc, argv);
  169. return 0;
  170. }