fixincl.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. /* Install modified versions of certain ANSI-incompatible system header
  2. files which are fixed to work correctly with ANSI C and placed in a
  3. directory that GCC will search.
  4. Copyright (C) 1997, 1998, 1999, 2000, 2004, 2009, 2012
  5. Free Software Foundation, Inc.
  6. This file is part of GCC.
  7. GCC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3, or (at your option)
  10. any later version.
  11. GCC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with GCC; see the file COPYING3. If not see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "fixlib.h"
  19. #include <fnmatch.h>
  20. #include <sys/stat.h>
  21. #ifndef SEPARATE_FIX_PROC
  22. #include <sys/wait.h>
  23. #endif
  24. #if defined( HAVE_MMAP_FILE )
  25. #include <sys/mman.h>
  26. #define BAD_ADDR ((void*)-1)
  27. #endif
  28. #ifndef SEPARATE_FIX_PROC
  29. #include "server.h"
  30. #endif
  31. /* The contents of this string are not very important. It is mostly
  32. just used as part of the "I am alive and working" test. */
  33. static const char program_id[] = "fixincl version 1.1";
  34. /* This format will be used at the start of every generated file */
  35. static const char z_std_preamble[] =
  36. "/* DO NOT EDIT THIS FILE.\n\n\
  37. It has been auto-edited by fixincludes from:\n\n\
  38. \t\"%s/%s\"\n\n\
  39. This had to be done to correct non-standard usages in the\n\
  40. original, manufacturer supplied header file. */\n\n";
  41. int find_base_len = 0;
  42. int have_tty = 0;
  43. pid_t process_chain_head = (pid_t) -1;
  44. char* pz_curr_file; /* name of the current file under test/fix */
  45. char* pz_curr_data; /* original contents of that file */
  46. char* pz_temp_file; /* for DOS, a place to stash the temporary
  47. fixed data between system(3) calls */
  48. t_bool curr_data_mapped;
  49. int data_map_fd;
  50. size_t data_map_size;
  51. size_t ttl_data_size = 0;
  52. #ifdef DO_STATS
  53. int process_ct = 0;
  54. int apply_ct = 0;
  55. int fixed_ct = 0;
  56. int altered_ct = 0;
  57. #endif /* DO_STATS */
  58. const char incl_quote_pat[] = "^[ \t]*#[ \t]*include[ \t]*\"[^/]";
  59. tSCC z_fork_err[] = "Error %d (%s) starting filter process for %s\n";
  60. regex_t incl_quote_re;
  61. static void do_version (void) ATTRIBUTE_NORETURN;
  62. char *load_file (const char *);
  63. void run_compiles (void);
  64. void initialize (int argc, char** argv);
  65. void process (void);
  66. /* External Source Code */
  67. #include "fixincl.x"
  68. /* * * * * * * * * * * * * * * * * * *
  69. *
  70. * MAIN ROUTINE
  71. */
  72. extern int main (int, char **);
  73. int
  74. main (int argc, char** argv)
  75. {
  76. char *file_name_buf;
  77. initialize ( argc, argv );
  78. have_tty = isatty (fileno (stderr));
  79. /* Before anything else, ensure we can allocate our file name buffer. */
  80. file_name_buf = load_file_data (stdin);
  81. /* Because of the way server shells work, you have to keep stdin, out
  82. and err open so that the proper input file does not get closed
  83. by accident */
  84. freopen ("/dev/null", "r", stdin);
  85. if (file_name_buf == (char *) NULL)
  86. {
  87. fputs ("No file names listed for fixing\n", stderr);
  88. exit (EXIT_FAILURE);
  89. }
  90. for (;;)
  91. {
  92. char* pz_end;
  93. /* skip to start of name, past any "./" prefixes */
  94. while (ISSPACE (*file_name_buf)) file_name_buf++;
  95. while ((file_name_buf[0] == '.') && (file_name_buf[1] == '/'))
  96. file_name_buf += 2;
  97. /* Check for end of list */
  98. if (*file_name_buf == NUL)
  99. break;
  100. /* Set global file name pointer and find end of name */
  101. pz_curr_file = file_name_buf;
  102. pz_end = strchr( pz_curr_file, '\n' );
  103. if (pz_end == (char*)NULL)
  104. pz_end = file_name_buf = pz_curr_file + strlen (pz_curr_file);
  105. else
  106. file_name_buf = pz_end + 1;
  107. while ((pz_end > pz_curr_file) && ISSPACE( pz_end[-1])) pz_end--;
  108. /* IF no name is found (blank line) or comment marker, skip line */
  109. if ((pz_curr_file == pz_end) || (*pz_curr_file == '#'))
  110. continue;
  111. *pz_end = NUL;
  112. process ();
  113. } /* for (;;) */
  114. #ifdef DO_STATS
  115. if (VLEVEL( VERB_PROGRESS )) {
  116. tSCC zFmt[] =
  117. "\
  118. Processed %5d files containing %d bytes \n\
  119. Applying %5d fixes to %d files\n\
  120. Altering %5d of them\n";
  121. fprintf (stderr, zFmt, process_ct, ttl_data_size, apply_ct,
  122. fixed_ct, altered_ct);
  123. }
  124. #endif /* DO_STATS */
  125. # ifdef SEPARATE_FIX_PROC
  126. unlink( pz_temp_file );
  127. # endif
  128. exit (EXIT_SUCCESS);
  129. }
  130. static void
  131. do_version (void)
  132. {
  133. static const char zFmt[] = "echo '%s'";
  134. char zBuf[ 1024 ];
  135. /* The 'version' option is really used to test that:
  136. 1. The program loads correctly (no missing libraries)
  137. 2. that we can compile all the regular expressions.
  138. 3. we can correctly run our server shell process
  139. */
  140. run_compiles ();
  141. sprintf (zBuf, zFmt, program_id);
  142. #ifndef SEPARATE_FIX_PROC
  143. puts (zBuf + 5);
  144. exit (strcmp (run_shell (zBuf), program_id));
  145. #else
  146. exit (system (zBuf));
  147. #endif
  148. }
  149. /* * * * * * * * * * * * */
  150. void
  151. initialize ( int argc, char** argv )
  152. {
  153. xmalloc_set_program_name (argv[0]);
  154. switch (argc)
  155. {
  156. case 1:
  157. break;
  158. case 2:
  159. if (strcmp (argv[1], "-v") == 0)
  160. do_version ();
  161. if (freopen (argv[1], "r", stdin) == (FILE*)NULL)
  162. {
  163. fprintf (stderr, "Error %d (%s) reopening %s as stdin\n",
  164. errno, xstrerror (errno), argv[1] );
  165. exit (EXIT_FAILURE);
  166. }
  167. break;
  168. default:
  169. fputs ("fixincl ERROR: too many command line arguments\n", stderr);
  170. exit (EXIT_FAILURE);
  171. }
  172. #ifdef SIGCHLD
  173. /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
  174. receive the signal. A different setting is inheritable */
  175. signal (SIGCHLD, SIG_DFL);
  176. #endif
  177. initialize_opts ();
  178. if (ISDIGIT ( *pz_verbose ))
  179. verbose_level = (te_verbose)atoi( pz_verbose );
  180. else
  181. switch (*pz_verbose) {
  182. case 's':
  183. case 'S':
  184. verbose_level = VERB_SILENT; break;
  185. case 'f':
  186. case 'F':
  187. verbose_level = VERB_FIXES; break;
  188. case 'a':
  189. case 'A':
  190. verbose_level = VERB_APPLIES; break;
  191. default:
  192. case 'p':
  193. case 'P':
  194. verbose_level = VERB_PROGRESS; break;
  195. case 't':
  196. case 'T':
  197. verbose_level = VERB_TESTS; break;
  198. case 'e':
  199. case 'E':
  200. verbose_level = VERB_EVERYTHING; break;
  201. }
  202. if (verbose_level >= VERB_EVERYTHING) {
  203. verbose_level = VERB_EVERYTHING;
  204. fputs ("fixinc verbosity: EVERYTHING\n", stderr);
  205. }
  206. while ((pz_find_base[0] == '.') && (pz_find_base[1] == '/'))
  207. pz_find_base += 2;
  208. if ((pz_find_base[0] != '.') || (pz_find_base[1] != NUL))
  209. find_base_len = strlen( pz_find_base );
  210. /* Compile all the regular expressions now.
  211. That way, it is done only once for the whole run.
  212. */
  213. run_compiles ();
  214. # ifdef SEPARATE_FIX_PROC
  215. /* NULL as the first argument to `tempnam' causes it to DTRT
  216. wrt the temporary directory where the file will be created. */
  217. pz_temp_file = tempnam( NULL, "fxinc" );
  218. # endif
  219. signal (SIGQUIT, SIG_IGN);
  220. signal (SIGIOT, SIG_IGN);
  221. signal (SIGPIPE, SIG_IGN);
  222. signal (SIGALRM, SIG_IGN);
  223. signal (SIGTERM, SIG_IGN);
  224. }
  225. /* * * * * * * * * * * * *
  226. load_file loads all the contents of a file into malloc-ed memory.
  227. Its argument is the name of the file to read in; the returned
  228. result is the NUL terminated contents of the file. The file
  229. is presumed to be an ASCII text file containing no NULs. */
  230. char *
  231. load_file ( const char* fname )
  232. {
  233. struct stat stbf;
  234. char* res;
  235. if (stat (fname, &stbf) != 0)
  236. {
  237. if (NOT_SILENT)
  238. fprintf (stderr, "error %d (%s) stat-ing %s\n",
  239. errno, xstrerror (errno), fname );
  240. return (char *) NULL;
  241. }
  242. if (stbf.st_size == 0)
  243. return (char*)NULL;
  244. /* Make the data map size one larger than the file size for documentation
  245. purposes. Truth is that there will be a following NUL character if
  246. the file size is not a multiple of the page size. If it is a multiple,
  247. then this adjustment sometimes fails anyway. */
  248. data_map_size = stbf.st_size+1;
  249. data_map_fd = open (fname, O_RDONLY);
  250. ttl_data_size += data_map_size-1;
  251. if (data_map_fd < 0)
  252. {
  253. if (NOT_SILENT)
  254. fprintf (stderr, "error %d (%s) opening %s for read\n",
  255. errno, xstrerror (errno), fname);
  256. return (char*)NULL;
  257. }
  258. #ifdef HAVE_MMAP_FILE
  259. curr_data_mapped = BOOL_TRUE;
  260. /* IF the file size is a multiple of the page size,
  261. THEN sometimes you will seg fault trying to access a trailing byte */
  262. if ((stbf.st_size & (getpagesize()-1)) == 0)
  263. res = (char*)BAD_ADDR;
  264. else
  265. res = (char*)mmap ((void*)NULL, data_map_size, PROT_READ,
  266. MAP_PRIVATE, data_map_fd, 0);
  267. if (res == (char*)BAD_ADDR)
  268. #endif
  269. {
  270. FILE* fp = fdopen (data_map_fd, "r");
  271. curr_data_mapped = BOOL_FALSE;
  272. res = load_file_data (fp);
  273. fclose (fp);
  274. }
  275. return res;
  276. }
  277. static int
  278. machine_matches( tFixDesc* p_fixd )
  279. {
  280. char const ** papz_machs = p_fixd->papz_machs;
  281. int have_match = BOOL_FALSE;
  282. for (;;)
  283. {
  284. char const * pz_mpat = *(papz_machs++);
  285. if (pz_mpat == NULL)
  286. break;
  287. if (fnmatch(pz_mpat, pz_machine, 0) == 0)
  288. {
  289. have_match = BOOL_TRUE;
  290. break;
  291. }
  292. }
  293. /* Check for sense inversion then set the "skip test" flag, if needed */
  294. if (p_fixd->fd_flags & FD_MACH_IFNOT)
  295. have_match = ! have_match;
  296. if (! have_match)
  297. p_fixd->fd_flags |= FD_SKIP_TEST;
  298. return have_match;
  299. }
  300. /* * * * * * * * * * * * *
  301. *
  302. * run_compiles run all the regexp compiles for all the fixes once.
  303. */
  304. void
  305. run_compiles (void)
  306. {
  307. tFixDesc *p_fixd = fixDescList;
  308. int fix_ct = FIX_COUNT;
  309. regex_t *p_re = XCNEWVEC (regex_t, REGEX_COUNT);
  310. /* Make sure compile_re does not stumble across invalid data */
  311. memset (&incl_quote_re, '\0', sizeof (regex_t));
  312. compile_re (incl_quote_pat, &incl_quote_re, 1,
  313. "quoted include", "run_compiles");
  314. /* Allow machine name tests to be ignored (testing, mainly) */
  315. if (pz_machine && ((*pz_machine == '\0') || (*pz_machine == '*')))
  316. pz_machine = (char*)NULL;
  317. /* FOR every fixup, ... */
  318. do
  319. {
  320. tTestDesc *p_test;
  321. int test_ct;
  322. if (fixinc_mode && (p_fixd->fd_flags & FD_REPLACEMENT))
  323. {
  324. p_fixd->fd_flags |= FD_SKIP_TEST;
  325. continue;
  326. }
  327. p_test = p_fixd->p_test_desc;
  328. test_ct = p_fixd->test_ct;
  329. /* IF the machine type pointer is not NULL (we are not in test mode)
  330. AND this test is for or not done on particular machines
  331. THEN ... */
  332. if ( (pz_machine != NULL)
  333. && (p_fixd->papz_machs != (const char**) NULL)
  334. && ! machine_matches (p_fixd) )
  335. continue;
  336. /* FOR every test for the fixup, ... */
  337. while (--test_ct >= 0)
  338. {
  339. switch (p_test->type)
  340. {
  341. case TT_EGREP:
  342. case TT_NEGREP:
  343. p_test->p_test_regex = p_re++;
  344. compile_re (p_test->pz_test_text, p_test->p_test_regex, 0,
  345. "select test", p_fixd->fix_name);
  346. default: break;
  347. }
  348. p_test++;
  349. }
  350. }
  351. while (p_fixd++, --fix_ct > 0);
  352. }
  353. /* * * * * * * * * * * * *
  354. create_file Create the output modified file.
  355. Input: the name of the file to create
  356. Returns: a file pointer to the new, open file */
  357. #if defined(S_IRUSR) && defined(S_IWUSR) && \
  358. defined(S_IRGRP) && defined(S_IROTH)
  359. # define S_IRALL (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
  360. #else
  361. # define S_IRALL 0644
  362. #endif
  363. #if defined(S_IRWXU) && defined(S_IRGRP) && defined(S_IXGRP) && \
  364. defined(S_IROTH) && defined(S_IXOTH)
  365. # define S_DIRALL (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
  366. #else
  367. # define S_DIRALL 0755
  368. #endif
  369. static FILE *
  370. create_file (void)
  371. {
  372. int fd;
  373. FILE *pf;
  374. char fname[MAXPATHLEN];
  375. sprintf (fname, "%s/%s", pz_dest_dir, pz_curr_file + find_base_len);
  376. fd = open (fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRALL);
  377. /* We may need to create the directories needed... */
  378. if ((fd < 0) && (errno == ENOENT))
  379. {
  380. char *pz_dir = strchr (fname + 1, '/');
  381. struct stat stbf;
  382. while (pz_dir != (char *) NULL)
  383. {
  384. *pz_dir = NUL;
  385. if (stat (fname, &stbf) < 0)
  386. {
  387. #ifdef _WIN32
  388. mkdir (fname);
  389. #else
  390. mkdir (fname, S_IFDIR | S_DIRALL);
  391. #endif
  392. }
  393. *pz_dir = '/';
  394. pz_dir = strchr (pz_dir + 1, '/');
  395. }
  396. /* Now, lets try the open again... */
  397. fd = open (fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRALL);
  398. }
  399. if (fd < 0)
  400. {
  401. fprintf (stderr, "Error %d (%s) creating %s\n",
  402. errno, xstrerror (errno), fname);
  403. exit (EXIT_FAILURE);
  404. }
  405. if (NOT_SILENT)
  406. fprintf (stderr, "Fixed: %s\n", pz_curr_file);
  407. pf = fdopen (fd, "w");
  408. /*
  409. * IF pz_machine is NULL, then we are in some sort of test mode.
  410. * Do not insert the current directory name. Use a constant string.
  411. */
  412. fprintf (pf, z_std_preamble,
  413. (pz_machine == NULL)
  414. ? "fixinc/tests/inc"
  415. : pz_input_dir,
  416. pz_curr_file);
  417. return pf;
  418. }
  419. /* * * * * * * * * * * * *
  420. test_test make sure a shell-style test expression passes.
  421. Input: a pointer to the descriptor of the test to run and
  422. the name of the file that we might want to fix
  423. Result: APPLY_FIX or SKIP_FIX, depending on the result of the
  424. shell script we run. */
  425. #ifndef SEPARATE_FIX_PROC
  426. static int
  427. test_test (tTestDesc* p_test, char* pz_test_file)
  428. {
  429. tSCC cmd_fmt[] =
  430. "file=%s\n\
  431. if ( test %s ) > /dev/null 2>&1\n\
  432. then echo TRUE\n\
  433. else echo FALSE\n\
  434. fi";
  435. char *pz_res;
  436. int res;
  437. static char cmd_buf[4096];
  438. sprintf (cmd_buf, cmd_fmt, pz_test_file, p_test->pz_test_text);
  439. pz_res = run_shell (cmd_buf);
  440. switch (*pz_res) {
  441. case 'T':
  442. res = APPLY_FIX;
  443. break;
  444. case 'F':
  445. res = SKIP_FIX;
  446. break;
  447. default:
  448. fprintf (stderr, "Script yielded bogus result of `%s':\n%s\n\n",
  449. pz_res, cmd_buf );
  450. res = SKIP_FIX;
  451. }
  452. free ((void *) pz_res);
  453. return res;
  454. }
  455. #else
  456. /*
  457. * IF we are in MS-DOS land, then whatever shell-type test is required
  458. * will, by definition, fail
  459. */
  460. #define test_test(t,tf) SKIP_FIX
  461. #endif
  462. /* * * * * * * * * * * * *
  463. egrep_test make sure an egrep expression is found in the file text.
  464. Input: a pointer to the descriptor of the test to run and
  465. the pointer to the contents of the file under suspicion
  466. Result: APPLY_FIX if the pattern is found, SKIP_FIX otherwise
  467. The caller may choose to reverse meaning if the sense of the test
  468. is inverted. */
  469. static int
  470. egrep_test (char* pz_data, tTestDesc* p_test)
  471. {
  472. #ifdef DEBUG
  473. if (p_test->p_test_regex == 0)
  474. fprintf (stderr, "fixincl ERROR RE not compiled: `%s'\n",
  475. p_test->pz_test_text);
  476. #endif
  477. if (xregexec (p_test->p_test_regex, pz_data, 0, 0, 0) == 0)
  478. return APPLY_FIX;
  479. return SKIP_FIX;
  480. }
  481. /* * * * * * * * * * * * *
  482. cksum_test check the sum of the candidate file
  483. Input: the original file contents and the file name
  484. Result: APPLY_FIX if the check sum matches, SKIP_FIX otherwise
  485. The caller may choose to reverse meaning if the sense of the test
  486. is inverted. */
  487. static int
  488. cksum_test (char * pz_data, tTestDesc * p_test, char * fname)
  489. {
  490. unsigned int cksum;
  491. /*
  492. * Testing is off in normal operation mode.
  493. * So, in testing mode, APPLY_FIX is always returned.
  494. */
  495. if (fixinc_mode != TESTING_OFF)
  496. return APPLY_FIX;
  497. {
  498. char * fnm = strrchr(fname, '/');
  499. if (fnm != NULL)
  500. fname = fnm + 1;
  501. errno = 0;
  502. cksum = (unsigned int)strtoul(p_test->pz_test_text, &fnm, 10);
  503. if (errno != 0)
  504. return SKIP_FIX;
  505. if (! ISSPACE(*fnm++))
  506. return SKIP_FIX;
  507. while (ISSPACE(*fnm)) fnm++;
  508. if (! ISDIGIT(*fnm++))
  509. return SKIP_FIX;
  510. while (ISDIGIT(*fnm)) fnm++;
  511. if (! ISSPACE(*fnm++))
  512. return SKIP_FIX;
  513. while (ISSPACE(*fnm)) fnm++;
  514. if (strcmp(fnm, fname) != 0)
  515. return SKIP_FIX;
  516. }
  517. {
  518. unsigned int sum = 0;
  519. while (*pz_data != NUL) {
  520. sum = (sum >> 1) + ((sum & 1) << 15) + (unsigned)(*pz_data++);
  521. sum &= 0xFFFF;
  522. }
  523. return (sum == cksum) ? APPLY_FIX : SKIP_FIX;
  524. }
  525. }
  526. /* * * * * * * * * * * * *
  527. quoted_file_exists Make sure that a file exists before we emit
  528. the file name. If we emit the name, our invoking shell will try
  529. to copy a non-existing file into the destination directory. */
  530. static int
  531. quoted_file_exists (const char* pz_src_path,
  532. const char* pz_file_path,
  533. const char* pz_file)
  534. {
  535. char z[ MAXPATHLEN ];
  536. char* pz;
  537. sprintf (z, "%s/%s/", pz_src_path, pz_file_path);
  538. pz = z + strlen ( z );
  539. for (;;) {
  540. char ch = *pz_file++;
  541. if (! ISGRAPH( ch ))
  542. return 0;
  543. if (ch == '"')
  544. break;
  545. *pz++ = ch;
  546. }
  547. *pz = '\0';
  548. {
  549. struct stat s;
  550. if (stat (z, &s) != 0)
  551. return 0;
  552. return S_ISREG( s.st_mode );
  553. }
  554. }
  555. /* * * * * * * * * * * * *
  556. *
  557. extract_quoted_files
  558. The syntax, `#include "file.h"' specifies that the compiler is to
  559. search the local directory of the current file before the include
  560. list. Consequently, if we have modified a header and stored it in
  561. another directory, any files that are included by that modified
  562. file in that fashion must also be copied into this new directory.
  563. This routine finds those flavors of #include and for each one found
  564. emits a triple of:
  565. 1. source directory of the original file
  566. 2. the relative path file name of the #includ-ed file
  567. 3. the full destination path for this file
  568. Input: the text of the file, the file name and a pointer to the
  569. match list where the match information was stored.
  570. Result: internally nothing. The results are written to stdout
  571. for interpretation by the invoking shell */
  572. static void
  573. extract_quoted_files (char* pz_data,
  574. const char* pz_fixed_file,
  575. regmatch_t* p_re_match)
  576. {
  577. char *pz_dir_end = strrchr (pz_fixed_file, '/');
  578. char *pz_incl_quot = pz_data;
  579. if (VLEVEL( VERB_APPLIES ))
  580. fprintf (stderr, "Quoted includes in %s\n", pz_fixed_file);
  581. /* Set "pz_fixed_file" to point to the containing subdirectory of the source
  582. If there is none, then it is in our current directory, ".". */
  583. if (pz_dir_end == (char *) NULL)
  584. pz_fixed_file = ".";
  585. else
  586. *pz_dir_end = '\0';
  587. for (;;)
  588. {
  589. pz_incl_quot += p_re_match->rm_so;
  590. /* Skip forward to the included file name */
  591. while (*pz_incl_quot != '"')
  592. pz_incl_quot++;
  593. if (quoted_file_exists (pz_src_dir, pz_fixed_file, pz_incl_quot))
  594. {
  595. /* Print the source directory and the subdirectory
  596. of the file in question. */
  597. printf ("%s %s/", pz_src_dir, pz_fixed_file);
  598. pz_dir_end = pz_incl_quot;
  599. /* Append to the directory the relative path of the desired file */
  600. while (*pz_incl_quot != '"')
  601. putc (*pz_incl_quot++, stdout);
  602. /* Now print the destination directory appended with the
  603. relative path of the desired file */
  604. printf (" %s/%s/", pz_dest_dir, pz_fixed_file);
  605. while (*pz_dir_end != '"')
  606. putc (*pz_dir_end++, stdout);
  607. /* End of entry */
  608. putc ('\n', stdout);
  609. }
  610. /* Find the next entry */
  611. if (xregexec (&incl_quote_re, pz_incl_quot, 1, p_re_match, 0) != 0)
  612. break;
  613. }
  614. }
  615. /* * * * * * * * * * * * *
  616. Somebody wrote a *_fix subroutine that we must call.
  617. */
  618. #ifndef SEPARATE_FIX_PROC
  619. static int
  620. internal_fix (int read_fd, tFixDesc* p_fixd)
  621. {
  622. int fd[2];
  623. if (pipe( fd ) != 0)
  624. {
  625. fprintf (stderr, "Error %d on pipe(2) call\n", errno );
  626. exit (EXIT_FAILURE);
  627. }
  628. for (;;)
  629. {
  630. pid_t childid = fork();
  631. switch (childid)
  632. {
  633. case -1:
  634. break;
  635. case 0:
  636. close (fd[0]);
  637. goto do_child_task;
  638. default:
  639. /*
  640. * Parent process
  641. */
  642. close (read_fd);
  643. close (fd[1]);
  644. return fd[0];
  645. }
  646. /*
  647. * Parent in error
  648. */
  649. fprintf (stderr, z_fork_err, errno, xstrerror (errno),
  650. p_fixd->fix_name);
  651. {
  652. static int failCt = 0;
  653. if ((errno != EAGAIN) || (++failCt > 10))
  654. exit (EXIT_FAILURE);
  655. sleep (1);
  656. }
  657. } do_child_task:;
  658. /*
  659. * Close our current stdin and stdout
  660. */
  661. close (STDIN_FILENO);
  662. close (STDOUT_FILENO);
  663. UNLOAD_DATA();
  664. /*
  665. * Make the fd passed in the stdin, and the write end of
  666. * the new pipe become the stdout.
  667. */
  668. dup2 (fd[1], STDOUT_FILENO);
  669. dup2 (read_fd, STDIN_FILENO);
  670. apply_fix (p_fixd, pz_curr_file);
  671. exit (0);
  672. }
  673. #endif /* !SEPARATE_FIX_PROC */
  674. #ifdef SEPARATE_FIX_PROC
  675. static void
  676. fix_with_system (tFixDesc* p_fixd,
  677. tCC* pz_fix_file,
  678. tCC* pz_file_source,
  679. tCC* pz_temp_file)
  680. {
  681. char* pz_cmd;
  682. char* pz_scan;
  683. size_t argsize;
  684. if (p_fixd->fd_flags & FD_SUBROUTINE)
  685. {
  686. static const char z_applyfix_prog[] =
  687. "/../fixincludes/applyfix" EXE_EXT;
  688. struct stat buf;
  689. argsize = 32
  690. + strlen (pz_orig_dir)
  691. + sizeof (z_applyfix_prog)
  692. + strlen (pz_fix_file)
  693. + strlen (pz_file_source)
  694. + strlen (pz_temp_file);
  695. /* Allocate something sure to be big enough for our purposes */
  696. pz_cmd = XNEWVEC (char, argsize);
  697. strcpy (pz_cmd, pz_orig_dir);
  698. pz_scan = pz_cmd + strlen (pz_orig_dir);
  699. strcpy (pz_scan, z_applyfix_prog);
  700. /* IF we can't find the "applyfix" executable file at the first guess,
  701. try one level higher up */
  702. if (stat (pz_cmd, &buf) == -1)
  703. {
  704. strcpy (pz_scan, "/..");
  705. strcpy (pz_scan+3, z_applyfix_prog);
  706. }
  707. pz_scan += strlen (pz_scan);
  708. /*
  709. * Now add the fix number and file names that may be needed
  710. */
  711. sprintf (pz_scan, " %ld '%s' '%s' '%s'", (long) (p_fixd - fixDescList),
  712. pz_fix_file, pz_file_source, pz_temp_file);
  713. }
  714. else /* NOT an "internal" fix: */
  715. {
  716. size_t parg_size;
  717. #ifdef __MSDOS__
  718. /* Don't use the "src > dstX; rm -f dst; mv -f dstX dst" trick:
  719. dst is a temporary file anyway, so we know there's no other
  720. file by that name; and DOS's system(3) doesn't mind to
  721. clobber existing file in redirection. Besides, with DOS 8+3
  722. limited file namespace, we can easily lose if dst already has
  723. an extension that is 3 or more characters long.
  724. I do not think the 8+3 issue is relevant because all the files
  725. we operate on are named "*.h", making 8+2 adequate. Anyway,
  726. the following bizarre use of 'cat' only works on DOS boxes.
  727. It causes the file to be dropped into a temporary file for
  728. 'cat' to read (pipes do not work on DOS). */
  729. tSCC z_cmd_fmt[] = " '%s' | cat > '%s'";
  730. #else
  731. /* Don't use positional formatting arguments because some lame-o
  732. implementations cannot cope :-(. */
  733. tSCC z_cmd_fmt[] = " %s > %sX ; rm -f %s; mv -f %sX %s";
  734. #endif
  735. tCC** ppArgs = p_fixd->patch_args;
  736. argsize = sizeof( z_cmd_fmt ) + strlen( pz_temp_file )
  737. + strlen( pz_file_source );
  738. parg_size = argsize;
  739. /*
  740. * Compute the size of the command line. Add lotsa extra space
  741. * because some of the args to sed use lotsa single quotes.
  742. * (This requires three extra bytes per quote. Here we allow
  743. * for up to 8 single quotes for each argument, including the
  744. * command name "sed" itself. Nobody will *ever* need more. :)
  745. */
  746. for (;;)
  747. {
  748. tCC* p_arg = *(ppArgs++);
  749. if (p_arg == NULL)
  750. break;
  751. argsize += 24 + strlen( p_arg );
  752. }
  753. /* Estimated buffer size we will need. */
  754. pz_scan = pz_cmd = XNEWVEC (char, argsize);
  755. /* How much of it do we allot to the program name and its
  756. arguments. */
  757. parg_size = argsize - parg_size;
  758. ppArgs = p_fixd->patch_args;
  759. /*
  760. * Copy the program name, unquoted
  761. */
  762. {
  763. tCC* pArg = *(ppArgs++);
  764. for (;;)
  765. {
  766. char ch = *(pArg++);
  767. if (ch == NUL)
  768. break;
  769. *(pz_scan++) = ch;
  770. }
  771. }
  772. /*
  773. * Copy the program arguments, quoted
  774. */
  775. for (;;)
  776. {
  777. tCC* pArg = *(ppArgs++);
  778. char* pz_scan_save;
  779. if (pArg == NULL)
  780. break;
  781. *(pz_scan++) = ' ';
  782. pz_scan = make_raw_shell_str( pz_scan_save = pz_scan, pArg,
  783. parg_size - (pz_scan - pz_cmd) );
  784. /*
  785. * Make sure we don't overflow the buffer due to sloppy
  786. * size estimation.
  787. */
  788. while (pz_scan == (char*)NULL)
  789. {
  790. size_t already_filled = pz_scan_save - pz_cmd;
  791. pz_cmd = xrealloc (pz_cmd, argsize += 100);
  792. pz_scan_save = pz_scan = pz_cmd + already_filled;
  793. parg_size += 100;
  794. pz_scan = make_raw_shell_str( pz_scan, pArg,
  795. parg_size - (pz_scan - pz_cmd) );
  796. }
  797. }
  798. /*
  799. * add the file machinations.
  800. */
  801. #ifdef __MSDOS__
  802. sprintf (pz_scan, z_cmd_fmt, pz_file_source, pz_temp_file );
  803. #else
  804. sprintf (pz_scan, z_cmd_fmt, pz_file_source, pz_temp_file,
  805. pz_temp_file, pz_temp_file, pz_temp_file);
  806. #endif
  807. }
  808. system( pz_cmd );
  809. free( (void*)pz_cmd );
  810. }
  811. /* * * * * * * * * * * * *
  812. This loop should only cycle for 1/2 of one loop.
  813. "chain_open" starts a process that uses "read_fd" as
  814. its stdin and returns the new fd this process will use
  815. for stdout. */
  816. #else /* is *NOT* SEPARATE_FIX_PROC */
  817. static int
  818. start_fixer (int read_fd, tFixDesc* p_fixd, char* pz_fix_file)
  819. {
  820. tCC* pz_cmd_save;
  821. char* pz_cmd;
  822. if ((p_fixd->fd_flags & FD_SUBROUTINE) != 0)
  823. return internal_fix (read_fd, p_fixd);
  824. if ((p_fixd->fd_flags & FD_SHELL_SCRIPT) == 0)
  825. {
  826. pz_cmd = NULL;
  827. pz_cmd_save = NULL;
  828. }
  829. else
  830. {
  831. tSCC z_cmd_fmt[] = "file='%s'\n%s";
  832. pz_cmd = XNEWVEC (char, strlen (p_fixd->patch_args[2])
  833. + sizeof (z_cmd_fmt) + strlen (pz_fix_file));
  834. sprintf (pz_cmd, z_cmd_fmt, pz_fix_file, p_fixd->patch_args[2]);
  835. pz_cmd_save = p_fixd->patch_args[2];
  836. p_fixd->patch_args[2] = pz_cmd;
  837. }
  838. /* Start a fix process, handing off the previous read fd for its
  839. stdin and getting a new fd that reads from the fix process' stdout.
  840. We normally will not loop, but we will up to 10 times if we keep
  841. getting "EAGAIN" errors.
  842. */
  843. for (;;)
  844. {
  845. static int failCt = 0;
  846. int fd;
  847. fd = chain_open (read_fd,
  848. (tCC **) p_fixd->patch_args,
  849. (process_chain_head == -1)
  850. ? &process_chain_head : (pid_t *) NULL);
  851. if (fd != -1)
  852. {
  853. read_fd = fd;
  854. break;
  855. }
  856. fprintf (stderr, z_fork_err, errno, xstrerror (errno),
  857. p_fixd->fix_name);
  858. if ((errno != EAGAIN) || (++failCt > 10))
  859. exit (EXIT_FAILURE);
  860. sleep (1);
  861. }
  862. /* IF we allocated a shell script command,
  863. THEN free it and restore the command format to the fix description */
  864. if (pz_cmd != (char*)NULL)
  865. {
  866. free ((void*)pz_cmd);
  867. p_fixd->patch_args[2] = pz_cmd_save;
  868. }
  869. return read_fd;
  870. }
  871. #endif
  872. #ifdef DEBUG
  873. # define NOTE_SKIP(_ttyp) do { \
  874. if (VLEVEL( VERB_EVERYTHING )) \
  875. fprintf (stderr, z_failed, _ttyp, p_fixd->fix_name, \
  876. pz_fname, p_fixd->test_ct - test_ct); \
  877. } while (0)
  878. #else
  879. # define NOTE_SKIP(_ttyp)
  880. #endif
  881. /* * * * * * * * * * * * *
  882. *
  883. * Process the potential fixes for a particular include file.
  884. * Input: the original text of the file and the file's name
  885. * Result: none. A new file may or may not be created.
  886. */
  887. static t_bool
  888. fix_applies (tFixDesc* p_fixd)
  889. {
  890. const char *pz_fname = pz_curr_file;
  891. const char *pz_scan = p_fixd->file_list;
  892. int test_ct;
  893. tTestDesc *p_test;
  894. t_bool saw_sum_test = BOOL_FALSE;
  895. t_bool one_sum_passed = BOOL_FALSE;
  896. #ifdef SEPARATE_FIX_PROC
  897. /*
  898. * There is only one fix that uses a shell script as of this writing.
  899. * I hope to nuke it anyway, it does not apply to DOS and it would
  900. * be painful to implement. Therefore, no "shell" fixes for DOS.
  901. */
  902. if (p_fixd->fd_flags & (FD_SHELL_SCRIPT | FD_SKIP_TEST))
  903. return BOOL_FALSE;
  904. #else
  905. if (p_fixd->fd_flags & FD_SKIP_TEST)
  906. return BOOL_FALSE;
  907. #endif
  908. /* IF there is a file name restriction,
  909. THEN ensure the current file name matches one in the pattern */
  910. if (pz_scan != (char *) NULL)
  911. {
  912. while ((pz_fname[0] == '.') && (pz_fname[1] == '/'))
  913. pz_fname += 2;
  914. for (;;)
  915. {
  916. if (fnmatch (pz_scan, pz_fname, 0) == 0)
  917. break;
  918. pz_scan += strlen (pz_scan) + 1;
  919. if (*pz_scan == NUL)
  920. return BOOL_FALSE;
  921. }
  922. }
  923. /* FOR each test, see if it fails.
  924. "sum" fails only if all "sum" tests fail.
  925. IF it does fail, then we go on to the next test */
  926. for (p_test = p_fixd->p_test_desc, test_ct = p_fixd->test_ct;
  927. test_ct-- > 0;
  928. p_test++)
  929. {
  930. switch (p_test->type)
  931. {
  932. case TT_TEST:
  933. if (test_test (p_test, pz_curr_file) != APPLY_FIX) {
  934. NOTE_SKIP("TEST");
  935. return BOOL_FALSE;
  936. }
  937. break;
  938. case TT_EGREP:
  939. if (egrep_test (pz_curr_data, p_test) != APPLY_FIX) {
  940. NOTE_SKIP("EGREP");
  941. return BOOL_FALSE;
  942. }
  943. break;
  944. case TT_NEGREP:
  945. if (egrep_test (pz_curr_data, p_test) == APPLY_FIX) {
  946. NOTE_SKIP("NEGREP");
  947. /* Negated sense */
  948. return BOOL_FALSE;
  949. }
  950. break;
  951. case TT_CKSUM:
  952. if (one_sum_passed)
  953. break; /* No need to check any more */
  954. saw_sum_test = BOOL_TRUE;
  955. if (cksum_test (pz_curr_data, p_test, pz_curr_file) != APPLY_FIX) {
  956. NOTE_SKIP("CKSUM");
  957. } else {
  958. one_sum_passed = BOOL_TRUE;
  959. }
  960. break;
  961. case TT_FUNCTION:
  962. if (run_test (p_test->pz_test_text, pz_curr_file, pz_curr_data)
  963. != APPLY_FIX) {
  964. NOTE_SKIP("FTEST");
  965. return BOOL_FALSE;
  966. }
  967. break;
  968. }
  969. }
  970. if (saw_sum_test)
  971. return one_sum_passed;
  972. return BOOL_TRUE;
  973. }
  974. /* * * * * * * * * * * * *
  975. Write out a replacement file */
  976. static void
  977. write_replacement (tFixDesc* p_fixd)
  978. {
  979. const char* pz_text = p_fixd->patch_args[0];
  980. if ((pz_text == (char*)NULL) || (*pz_text == NUL))
  981. return;
  982. {
  983. FILE* out_fp = create_file ();
  984. size_t sz = strlen (pz_text);
  985. fwrite (pz_text, sz, 1, out_fp);
  986. if (pz_text[ sz-1 ] != '\n')
  987. fputc ('\n', out_fp);
  988. fclose (out_fp);
  989. }
  990. }
  991. /* * * * * * * * * * * * *
  992. We have work to do. Read back in the output
  993. of the filtering chain. Compare each byte as we read it with
  994. the contents of the original file. As soon as we find any
  995. difference, we will create the output file, write out all
  996. the matched text and then copy any remaining data from the
  997. output of the filter chain.
  998. */
  999. static void
  1000. test_for_changes (int read_fd)
  1001. {
  1002. FILE *in_fp = fdopen (read_fd, "r");
  1003. FILE *out_fp = (FILE *) NULL;
  1004. unsigned char *pz_cmp = (unsigned char*)pz_curr_data;
  1005. #ifdef DO_STATS
  1006. fixed_ct++;
  1007. #endif
  1008. for (;;)
  1009. {
  1010. int ch;
  1011. ch = getc (in_fp);
  1012. if (ch == EOF)
  1013. break;
  1014. ch &= 0xFF; /* all bytes are 8 bits */
  1015. /* IF we are emitting the output
  1016. THEN emit this character, too.
  1017. */
  1018. if (out_fp != (FILE *) NULL)
  1019. putc (ch, out_fp);
  1020. /* ELSE if this character does not match the original,
  1021. THEN now is the time to start the output.
  1022. */
  1023. else if (ch != *pz_cmp)
  1024. {
  1025. out_fp = create_file ();
  1026. #ifdef DO_STATS
  1027. altered_ct++;
  1028. #endif
  1029. /* IF there are matched data, write the matched part now. */
  1030. if ((char*)pz_cmp != pz_curr_data)
  1031. fwrite (pz_curr_data, (size_t)((char*)pz_cmp - pz_curr_data),
  1032. 1, out_fp);
  1033. /* Emit the current unmatching character */
  1034. putc (ch, out_fp);
  1035. }
  1036. else
  1037. /* ELSE the character matches. Advance the compare ptr */
  1038. pz_cmp++;
  1039. }
  1040. /* IF we created the output file, ... */
  1041. if (out_fp != (FILE *) NULL)
  1042. {
  1043. regmatch_t match;
  1044. /* Close the file and see if we have to worry about
  1045. `#include "file.h"' constructs. */
  1046. fclose (out_fp);
  1047. if (xregexec (&incl_quote_re, pz_curr_data, 1, &match, 0) == 0)
  1048. extract_quoted_files (pz_curr_data, pz_curr_file, &match);
  1049. }
  1050. fclose (in_fp);
  1051. close (read_fd); /* probably redundant, but I'm paranoid */
  1052. }
  1053. /* * * * * * * * * * * * *
  1054. Process the potential fixes for a particular include file.
  1055. Input: the original text of the file and the file's name
  1056. Result: none. A new file may or may not be created. */
  1057. void
  1058. process (void)
  1059. {
  1060. tFixDesc *p_fixd = fixDescList;
  1061. int todo_ct = FIX_COUNT;
  1062. int read_fd = -1;
  1063. # ifndef SEPARATE_FIX_PROC
  1064. int num_children = 0;
  1065. # else /* is SEPARATE_FIX_PROC */
  1066. char* pz_file_source = pz_curr_file;
  1067. # endif
  1068. if (access (pz_curr_file, R_OK) != 0)
  1069. {
  1070. int erno = errno;
  1071. fprintf (stderr, "Cannot access %s from %s\n\terror %d (%s)\n",
  1072. pz_curr_file, getcwd ((char *) NULL, MAXPATHLEN),
  1073. erno, xstrerror (erno));
  1074. return;
  1075. }
  1076. pz_curr_data = load_file (pz_curr_file);
  1077. if (pz_curr_data == (char *) NULL)
  1078. return;
  1079. #ifdef DO_STATS
  1080. process_ct++;
  1081. #endif
  1082. if (VLEVEL( VERB_PROGRESS ) && have_tty)
  1083. fprintf (stderr, "%6lu %-50s \r",
  1084. (unsigned long) data_map_size, pz_curr_file);
  1085. # ifndef SEPARATE_FIX_PROC
  1086. process_chain_head = NOPROCESS;
  1087. /* For every fix in our fix list, ... */
  1088. for (; todo_ct > 0; p_fixd++, todo_ct--)
  1089. {
  1090. if (! fix_applies (p_fixd))
  1091. continue;
  1092. if (VLEVEL( VERB_APPLIES ))
  1093. fprintf (stderr, "Applying %-24s to %s\n",
  1094. p_fixd->fix_name, pz_curr_file);
  1095. if (p_fixd->fd_flags & FD_REPLACEMENT)
  1096. {
  1097. write_replacement (p_fixd);
  1098. UNLOAD_DATA();
  1099. return;
  1100. }
  1101. /* IF we do not have a read pointer,
  1102. THEN this is the first fix for the current file.
  1103. Open the source file. That will be used as stdin for
  1104. the first fix. Any subsequent fixes will use the
  1105. stdout descriptor of the previous fix for its stdin. */
  1106. if (read_fd == -1)
  1107. {
  1108. read_fd = open (pz_curr_file, O_RDONLY);
  1109. if (read_fd < 0)
  1110. {
  1111. fprintf (stderr, "Error %d (%s) opening %s\n", errno,
  1112. xstrerror (errno), pz_curr_file);
  1113. exit (EXIT_FAILURE);
  1114. }
  1115. /* Ensure we do not get duplicate output */
  1116. fflush (stdout);
  1117. }
  1118. read_fd = start_fixer (read_fd, p_fixd, pz_curr_file);
  1119. num_children++;
  1120. }
  1121. /* IF we have a read-back file descriptor,
  1122. THEN check for changes and write output if changed. */
  1123. if (read_fd >= 0)
  1124. {
  1125. test_for_changes (read_fd);
  1126. #ifdef DO_STATS
  1127. apply_ct += num_children;
  1128. #endif
  1129. /* Wait for child processes created by chain_open()
  1130. to avoid leaving zombies. */
  1131. do {
  1132. wait ((int *) NULL);
  1133. } while (--num_children > 0);
  1134. }
  1135. # else /* is SEPARATE_FIX_PROC */
  1136. for (; todo_ct > 0; p_fixd++, todo_ct--)
  1137. {
  1138. if (! fix_applies (p_fixd))
  1139. continue;
  1140. if (VLEVEL( VERB_APPLIES ))
  1141. fprintf (stderr, "Applying %-24s to %s\n",
  1142. p_fixd->fix_name, pz_curr_file);
  1143. if (p_fixd->fd_flags & FD_REPLACEMENT)
  1144. {
  1145. write_replacement (p_fixd);
  1146. UNLOAD_DATA();
  1147. return;
  1148. }
  1149. fix_with_system (p_fixd, pz_curr_file, pz_file_source, pz_temp_file);
  1150. pz_file_source = pz_temp_file;
  1151. }
  1152. read_fd = open (pz_temp_file, O_RDONLY);
  1153. if (read_fd < 0)
  1154. {
  1155. if (errno != ENOENT)
  1156. fprintf (stderr, "error %d (%s) opening output (%s) for read\n",
  1157. errno, xstrerror (errno), pz_temp_file);
  1158. }
  1159. else
  1160. {
  1161. test_for_changes (read_fd);
  1162. /* Unlinking a file while it is still open is a Bad Idea on
  1163. DOS/Windows. */
  1164. close (read_fd);
  1165. unlink (pz_temp_file);
  1166. }
  1167. # endif
  1168. UNLOAD_DATA();
  1169. }