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.

328 lines
10 KiB

  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // chgcpy.CPP
  4. //
  5. //
  6. // Copyright (c)2001 Microsoft Corporation, All Rights Reserved
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. #define FILEBUFFERSIZE 1024*10
  10. #include <windows.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. char gszSourceFiles[MAX_PATH];
  15. char gszReplace[MAX_PATH];
  16. char gszIgnore[MAX_PATH];
  17. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. BOOL ParseCommandLine(int argc, char *argv[])
  19. {
  20. BOOL fRc = TRUE;
  21. //============================================================================================
  22. //
  23. // Loop through the command line and get all of the available arguments.
  24. //
  25. //============================================================================================
  26. for(int i=1; i<argc; i++)
  27. {
  28. if(_stricmp(argv[i], "-SOURCEFILES") == 0)
  29. {
  30. argv[i++];
  31. strcpy( gszSourceFiles, argv[i] );
  32. }
  33. else if(_stricmp(argv[i], "-IGNORE") == 0)
  34. {
  35. argv[i++];
  36. strcpy( gszIgnore, argv[i] );
  37. }
  38. else if(_stricmp(argv[i], "-REPLACE") == 0)
  39. {
  40. argv[i++];
  41. strcpy( gszReplace, argv[i] );
  42. }
  43. }
  44. if( argc < 4 )
  45. {
  46. printf("Usage : %s OPTIONS\n\n", argv[0]);
  47. printf("-SOURCEFILES Files to search for.\n");
  48. printf("-IGNORE Files to ignore.\n");
  49. printf("-REPLACE Fields to be replaced.\n");
  50. }
  51. return fRc;
  52. }
  53. /////////////////////////////////////////////////////////////////////////////////////////////////
  54. BOOL GetNewCopyrightInfo(char * szLineToReplace, char * szNew)
  55. {
  56. BOOL fRc = FALSE;
  57. FILE * fp = NULL;
  58. fp = fopen(gszReplace,"r");
  59. if(fp)
  60. {
  61. char szBuffer[FILEBUFFERSIZE];
  62. char szOld[FILEBUFFERSIZE];
  63. while(!feof(fp))
  64. {
  65. //=====================================================
  66. // Read a line at a time, see if the string is in there
  67. //=====================================================
  68. // fscanf(fp,"%[^\n]%*c",szBuffer);
  69. fgets(szBuffer, FILEBUFFERSIZE, fp );
  70. sscanf( szBuffer, "%[^~]%*c%[^\n]%*c",szOld, szNew );
  71. if( stricmp(szOld, szLineToReplace ) == 0 )
  72. {
  73. fRc = TRUE;
  74. break;
  75. }
  76. }
  77. fclose(fp);
  78. }
  79. return fRc;
  80. }
  81. /////////////////////////////////////////////////////////////////////////////////////////////////
  82. BOOL GenerateReplacementLine( char * szLineToReplace, char * szBuffer, char * pBeginning )
  83. {
  84. char szOriginalLine[FILEBUFFERSIZE];
  85. char szNewLine[FILEBUFFERSIZE];
  86. BOOL fRc = FALSE;
  87. strcpy( szOriginalLine, szBuffer );
  88. int nOldTotal = strlen(szBuffer);
  89. //==========================================================
  90. // Copy anything up to the line to replace
  91. //==========================================================
  92. int nBytesBeforeNew = pBeginning - szBuffer;
  93. strncpy( szBuffer, pBeginning, nBytesBeforeNew);
  94. //==========================================================
  95. // insert new line
  96. //==========================================================
  97. if( GetNewCopyrightInfo( szLineToReplace, szNewLine ))
  98. {
  99. strncpy( &szBuffer[nBytesBeforeNew], szNewLine, strlen(szNewLine) );
  100. //==========================================================
  101. // Copy anything left over
  102. //==========================================================
  103. int nNewPos = nBytesBeforeNew + strlen(szNewLine);
  104. int nLeftOverBytes = nBytesBeforeNew + strlen(szLineToReplace);
  105. if( nLeftOverBytes < nOldTotal )
  106. {
  107. int nOldPos = nBytesBeforeNew + strlen( szLineToReplace );
  108. strncpy( &szBuffer[nNewPos], &szOriginalLine[nOldPos], nLeftOverBytes );
  109. }
  110. fRc = TRUE;
  111. szBuffer[nNewPos]= NULL;
  112. }
  113. return fRc;
  114. }
  115. /////////////////////////////////////////////////////////////////////////////////////////////////
  116. BOOL GetTempFileName(char * szFileLine, char * szNewLine)
  117. {
  118. BOOL fReturn = FALSE;
  119. // tmp tmp
  120. sprintf(szNewLine,"%s.xxx", szFileLine );
  121. fReturn = TRUE;
  122. return fReturn;
  123. }
  124. /////////////////////////////////////////////////////////////////////////////////////////////////
  125. BOOL WriteReplacement(char * szFileLine, char * szLineToReplace, char * szTmpFileLine)
  126. {
  127. BOOL fRc = FALSE;
  128. FILE * fp = NULL, * fp2 = NULL;
  129. fp = fopen(szFileLine,"r");
  130. if(fp)
  131. {
  132. char szBuffer[FILEBUFFERSIZE];
  133. if( GetTempFileName( szFileLine, szTmpFileLine ) )
  134. {
  135. fp2 = fopen(szTmpFileLine,"w");
  136. if(fp2)
  137. {
  138. char * pBeginning = NULL;
  139. char szTmp[FILEBUFFERSIZE];
  140. while(!feof(fp))
  141. {
  142. //=====================================================
  143. // Read a line at a time, see if the string is in there
  144. //=====================================================
  145. fgets(szBuffer, FILEBUFFERSIZE, fp );
  146. sscanf(szBuffer,"%[^\n]",szTmp); // tmp
  147. if( pBeginning = strstr( szTmp, szLineToReplace ))
  148. {
  149. fRc = GenerateReplacementLine(szLineToReplace, szTmp, pBeginning);
  150. if( !fRc )
  151. {
  152. break;
  153. }
  154. fprintf(fp2,"%s\n", szTmp );
  155. //=================================================
  156. // just bulk copy rest of file, we got it in there
  157. // once anyway...
  158. //=================================================
  159. while(!feof(fp))
  160. {
  161. fgets(szBuffer, FILEBUFFERSIZE, fp );
  162. fputs(szBuffer, fp2 );
  163. memset( szBuffer, NULL, FILEBUFFERSIZE );
  164. }
  165. break;
  166. }
  167. else
  168. {
  169. fprintf(fp2,"%s\n", szBuffer );
  170. }
  171. }
  172. fclose(fp2);
  173. }
  174. fclose(fp);
  175. }
  176. }
  177. return fRc;
  178. }
  179. /////////////////////////////////////////////////////////////////////////////////////////////////
  180. BOOL ReplaceCopyright(char * szFileLine)
  181. {
  182. BOOL fRc = FALSE;
  183. char szFile[FILEBUFFERSIZE];
  184. char szCopyInfo[FILEBUFFERSIZE];
  185. char szCommand[FILEBUFFERSIZE];
  186. char szTmpFileLine[FILEBUFFERSIZE];
  187. sscanf( szFileLine, "%[^:]:%[^\n]",szFile,szCopyInfo);
  188. sprintf( szCommand,"sd edit %s",szFile );
  189. system( szCommand );
  190. if( WriteReplacement( szFile, szCopyInfo, szTmpFileLine) )
  191. {
  192. sprintf( szCommand,"copy %s %s.bak",szFile, szFile );
  193. system(szCommand);
  194. sprintf( szCommand,"copy %s %s",szTmpFileLine, szFile );
  195. system(szCommand);
  196. sprintf( szCommand,"del %s",szFile); // testing here - some weirdness
  197. system(szCommand);
  198. sprintf( szCommand,"copy %s %s",szTmpFileLine, szFile);
  199. system(szCommand);
  200. sprintf( szCommand,"del %s",szTmpFileLine);
  201. system(szCommand);
  202. FILE * fp = fopen( "FCCP.BAT","a" );
  203. if( fp )
  204. {
  205. fprintf(fp,"fc %s %s.bak\n", szFile, szFile );
  206. fclose(fp);
  207. }
  208. printf( "edit of %s complete, replaced %s\n",szFile, szCopyInfo);
  209. }
  210. else
  211. {
  212. sprintf( szCommand,"sd revert %s",szFile );
  213. system( szCommand );
  214. }
  215. return fRc;
  216. }
  217. /////////////////////////////////////////////////////////////////////////////////////////////////
  218. BOOL ValidLine(char * szLineToValidate)
  219. {
  220. BOOL fRc = TRUE;
  221. FILE * fp = NULL;
  222. fp = fopen(gszIgnore,"r");
  223. if(fp)
  224. {
  225. char szBuffer[FILEBUFFERSIZE],szTmp[FILEBUFFERSIZE];
  226. while(!feof(fp))
  227. {
  228. //=====================================================
  229. // Read a line at a time, see if the string is in there
  230. //=====================================================
  231. fgets(szBuffer, FILEBUFFERSIZE, fp );
  232. sscanf(szBuffer,"%[^\n]",szTmp); // tmp
  233. char * pChars = strstr( szLineToValidate, szTmp);
  234. if( pChars )
  235. {
  236. fRc = FALSE;
  237. break;
  238. }
  239. }
  240. fclose(fp);
  241. }
  242. return fRc;
  243. }
  244. /////////////////////////////////////////////////////////////////////////////////////////////////
  245. BOOL DoIt()
  246. {
  247. BOOL fRc = FALSE;
  248. FILE * fp = NULL;
  249. fp = fopen(gszSourceFiles,"r");
  250. if(fp)
  251. {
  252. char szBuffer[FILEBUFFERSIZE];
  253. while(!feof(fp))
  254. {
  255. //=====================================================
  256. // Read a line at a time
  257. //=====================================================
  258. // fscanf(fp,"%[^\n]%*c",szBuffer);
  259. fgets(szBuffer, FILEBUFFERSIZE, fp );
  260. if( !ValidLine(szBuffer) )
  261. {
  262. continue;
  263. }
  264. //=====================================================
  265. // If it is something we want to work with, save it
  266. //=====================================================
  267. ReplaceCopyright(szBuffer);
  268. }
  269. fclose(fp);
  270. }
  271. return fRc;
  272. }
  273. /////////////////////////////////////////////////////////////////////////////////////////////////
  274. int main( int argc, char *argv[ ] )
  275. {
  276. int nRc = 1;
  277. memset(gszSourceFiles,NULL,MAX_PATH);
  278. memset(gszIgnore,NULL,MAX_PATH);
  279. memset(gszReplace,NULL,MAX_PATH);
  280. //==============================================================
  281. // Get the command line arguments
  282. //==============================================================
  283. if( ParseCommandLine(argc, argv) )
  284. {
  285. if( strlen(gszSourceFiles) > 0 && strlen(gszReplace) > 0 && strlen(gszIgnore) > 0 )
  286. {
  287. //======================================================
  288. // Generate the list of files to edit
  289. //======================================================
  290. if( DoIt())
  291. {
  292. nRc = 1;
  293. }
  294. }
  295. }
  296. else
  297. {
  298. printf( "Invalid command line.\n");
  299. }
  300. return nRc;
  301. }