fixincl.c 38 KB

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