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.

239 lines
6.5 KiB

  1. /*
  2. * Utility program to munge a set of files, translating names from
  3. * one form to another. Usage:
  4. *
  5. * munge scriptFile files...
  6. *
  7. * where the first parameter is the name of a file that consists of
  8. * one or more lines of the following format:
  9. *
  10. * oldName newName
  11. *
  12. * and the remaining parameters are the names of the files to munge.
  13. * Each file is _read into memory, scanned once, where each occurence
  14. * of an oldName string is replaced by its corresponding newName.
  15. * If any changes are made to a file, the old version is RMed and
  16. * the new version written out under the same name.
  17. *
  18. */
  19. #include "munge.h"
  20. int fQuery = FALSE;
  21. int fRecurse = FALSE;
  22. int fVerbose = FALSE;
  23. void
  24. DoFiles(
  25. char *p,
  26. struct findType *b,
  27. void *Args
  28. );
  29. void
  30. DoFile( p )
  31. char *p;
  32. {
  33. HANDLE FileHandle;
  34. SYSTEMTIME SystemTime;
  35. FILETIME CurrentTime;
  36. FILETIME CreationTime;
  37. FILETIME LastAccessTime;
  38. FILETIME LastWriteTime;
  39. BOOL DatesBogus = FALSE;
  40. if (fVerbose)
  41. fprintf( stderr, "Checking %s\n", p );
  42. FileHandle = CreateFile( p,
  43. GENERIC_READ,
  44. FILE_SHARE_READ | FILE_SHARE_WRITE,
  45. NULL,
  46. OPEN_EXISTING,
  47. 0,
  48. NULL
  49. );
  50. if (FileHandle == NULL) {
  51. fprintf( stderr, "%s - unable to open (%d)\n", p, GetLastError() );
  52. return;
  53. }
  54. if (!GetFileTime( FileHandle,
  55. &CreationTime,
  56. &LastAccessTime,
  57. &LastWriteTime
  58. )
  59. ) {
  60. fprintf( stderr, "%s - unable to read file dates (%d)\n", p, GetLastError() );
  61. CloseHandle( FileHandle );
  62. return;
  63. }
  64. CloseHandle( FileHandle );
  65. GetSystemTime( &SystemTime );
  66. SystemTimeToFileTime( &SystemTime, &CurrentTime );
  67. if (CompareFileTime( &CreationTime, &CurrentTime ) > 0) {
  68. CreationTime = CurrentTime;
  69. DatesBogus = TRUE;
  70. }
  71. if (CompareFileTime( &LastAccessTime, &CurrentTime ) > 0) {
  72. LastAccessTime = CurrentTime;
  73. DatesBogus = TRUE;
  74. }
  75. if (CompareFileTime( &LastWriteTime, &CurrentTime ) > 0) {
  76. LastWriteTime = CurrentTime;
  77. DatesBogus = TRUE;
  78. }
  79. if (DatesBogus) {
  80. printf( "%s dates invalid", p );
  81. if (fQuery) {
  82. printf( "\n" );
  83. return;
  84. }
  85. FileHandle = CreateFile( p,
  86. GENERIC_WRITE,
  87. FILE_SHARE_READ | FILE_SHARE_WRITE,
  88. NULL,
  89. OPEN_EXISTING,
  90. 0,
  91. NULL
  92. );
  93. if (FileHandle == NULL) {
  94. printf( " - unable to open for write (%d)\n", GetLastError() );
  95. }
  96. else {
  97. if (!SetFileTime( FileHandle,
  98. &CreationTime,
  99. &LastAccessTime,
  100. &LastWriteTime
  101. )
  102. ) {
  103. printf( " - unable to modify file dates (%d)\n", GetLastError() );
  104. }
  105. else {
  106. printf( " - reset to current date/time\n" );
  107. }
  108. CloseHandle( FileHandle );
  109. }
  110. }
  111. return;
  112. }
  113. void
  114. DoFiles(
  115. char *p,
  116. struct findType *b,
  117. void *Args
  118. )
  119. {
  120. if (strcmp (b->fbuf.cFileName, ".") &&
  121. strcmp (b->fbuf.cFileName, "..") &&
  122. _stricmp (b->fbuf.cFileName, "slm.dif")
  123. ) {
  124. if (HASATTR(b->fbuf.dwFileAttributes,FILE_ATTRIBUTE_DIRECTORY)) {
  125. switch (p[strlen(p)-1]) {
  126. case '/':
  127. case '\\': strcat (p, "*.*"); break;
  128. default: strcat (p, "\\*.*");
  129. }
  130. if (fRecurse) {
  131. fprintf( stderr, "Scanning %s\n", p );
  132. forfile( p,
  133. FILE_ATTRIBUTE_DIRECTORY,
  134. DoFiles,
  135. NULL
  136. );
  137. }
  138. }
  139. else {
  140. DoFile( p );
  141. }
  142. }
  143. Args;
  144. }
  145. void
  146. Usage( void )
  147. {
  148. fprintf( stderr, "usage: datefix [-q] [-r] [-v] [DirectorySpec(s)]\n" );
  149. fprintf( stderr, "Where...\n");
  150. fprintf( stderr, "\t-q\tQuery only - don't actually make changes.\n");
  151. fprintf( stderr, "\t-r\tRecurse.\n");
  152. fprintf( stderr, "\t-v\tVerbose - show files being checked.\n");
  153. fprintf( stderr, "\tDirectorySpec - one or more directories to examime.\n" );
  154. exit( 1 );
  155. }
  156. int
  157. __cdecl main( argc, argv )
  158. int argc;
  159. char *argv[];
  160. {
  161. int i;
  162. char *s, pathBuf[ 64 ];
  163. int FileArgsSeen = 0;
  164. if (argc < 2) {
  165. Usage();
  166. }
  167. fQuery = FALSE;
  168. fRecurse = FALSE;
  169. fVerbose = FALSE;
  170. for (i=1; i<argc; i++) {
  171. s = argv[ i ];
  172. if (*s == '-' || *s == '/') {
  173. while (*++s) {
  174. switch( tolower( *s ) ) {
  175. case 'q': fQuery = TRUE; break;
  176. case 'r': fRecurse = TRUE; break;
  177. case 'v': fVerbose = TRUE; break;
  178. default: Usage();
  179. }
  180. }
  181. }
  182. else {
  183. FileArgsSeen++;
  184. if (GetFileAttributes( s ) & FILE_ATTRIBUTE_DIRECTORY) {
  185. s = strcpy( pathBuf, s );
  186. switch (s[strlen(s)-1]) {
  187. case '/':
  188. case '\\': strcat (s, "*.*"); break;
  189. default: strcat (s, "\\*.*");
  190. }
  191. fprintf( stderr, "Scanning %s\n", s );
  192. forfile( s,
  193. FILE_ATTRIBUTE_DIRECTORY,
  194. DoFiles,
  195. NULL
  196. );
  197. }
  198. else {
  199. DoFile( s );
  200. }
  201. }
  202. }
  203. if (FileArgsSeen == 0) {
  204. s = "*.*";
  205. fprintf( stderr, "Scanning .\\%s\n", s );
  206. forfile( s,
  207. FILE_ATTRIBUTE_DIRECTORY,
  208. DoFiles,
  209. NULL
  210. );
  211. }
  212. return( 0 );
  213. }