strip.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /* strip certain symbols from a rel file.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. NO WARRANTY
  4. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  5. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  6. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  7. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  8. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  9. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  10. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  11. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  12. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  13. CORRECTION.
  14. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  15. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  16. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  17. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  18. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  19. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  20. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  21. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  22. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  23. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  24. GENERAL PUBLIC LICENSE TO COPY
  25. 1. You may copy and distribute verbatim copies of this source file
  26. as you receive it, in any medium, provided that you conspicuously
  27. and appropriately publish on each copy a valid copyright notice
  28. "Copyright (C) 1986 Free Software Foundation, Inc.", and include
  29. following the copyright notice a verbatim copy of the above disclaimer
  30. of warranty and of this License.
  31. 2. You may modify your copy or copies of this source file or
  32. any portion of it, and copy and distribute such modifications under
  33. the terms of Paragraph 1 above, provided that you also do the following:
  34. a) cause the modified files to carry prominent notices stating
  35. that you changed the files and the date of any change; and
  36. b) cause the whole of any work that you distribute or publish,
  37. that in whole or in part contains or is a derivative of this
  38. program or any part thereof, to be licensed at no charge to all
  39. third parties on terms identical to those contained in this
  40. License Agreement (except that you may choose to grant more extensive
  41. warranty protection to some or all third parties, at your option).
  42. c) You may charge a distribution fee for the physical act of
  43. transferring a copy, and you may at your option offer warranty
  44. protection in exchange for a fee.
  45. Mere aggregation of another unrelated program with this program (or its
  46. derivative) on a volume of a storage or distribution medium does not bring
  47. the other program under the scope of these terms.
  48. 3. You may copy and distribute this program (or a portion or derivative
  49. of it, under Paragraph 2) in object code or executable form under the terms
  50. of Paragraphs 1 and 2 above provided that you also do one of the following:
  51. a) accompany it with the complete corresponding machine-readable
  52. source code, which must be distributed under the terms of
  53. Paragraphs 1 and 2 above; or,
  54. b) accompany it with a written offer, valid for at least three
  55. years, to give any third party free (except for a nominal
  56. shipping charge) a complete machine-readable copy of the
  57. corresponding source code, to be distributed under the terms of
  58. Paragraphs 1 and 2 above; or,
  59. c) accompany it with the information you received as to where the
  60. corresponding source code may be obtained. (This alternative is
  61. allowed only for noncommercial distribution and only if you
  62. received the program in object code or executable form alone.)
  63. For an executable file, complete source code means all the source code for
  64. all modules it contains; but, as a special exception, it need not include
  65. source code for modules which are standard libraries that accompany the
  66. operating system on which the executable file runs.
  67. 4. You may not copy, sublicense, distribute or transfer this program
  68. except as expressly provided under this License Agreement. Any attempt
  69. otherwise to copy, sublicense, distribute or transfer this program is void and
  70. your rights to use the program under this License agreement shall be
  71. automatically terminated. However, parties who have received computer
  72. software programs from you with this License Agreement will not have
  73. their licenses terminated so long as such parties remain in full compliance.
  74. 5. If you wish to incorporate parts of this program into other free
  75. programs whose distribution conditions are different, write to the Free
  76. Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
  77. worked out a simple rule that can be stated here, but we will often permit
  78. this. We will be guided by the two goals of preserving the free status of
  79. all derivatives of our free software and of promoting the sharing and reuse of
  80. software.
  81. In other words, you are welcome to use, share and improve this program.
  82. You are forbidden to forbid anyone else to use, share and improve
  83. what you give them. Help stamp out software-hoarding! */
  84. #include <stdio.h>
  85. #include <sys/types.h>
  86. #include <sys/file.h>
  87. #include <sys/stat.h>
  88. #ifdef USG
  89. #include <fcntl.h>
  90. #include <string.h>
  91. #else
  92. #include <strings.h>
  93. #endif
  94. #ifdef COFF_ENCAPSULATE
  95. #include "a.out.encap.h"
  96. #else
  97. /* On native BSD systems, use the system's own a.out.h. */
  98. #include <a.out.h>
  99. #endif
  100. /* If BSD, we can use `ftruncate'. */
  101. #ifndef USG
  102. #define HAVE_FTRUNCATE
  103. #endif
  104. /* Struct or union for header of object file. */
  105. #define HEADER_TYPE struct exec
  106. /* Count the number of nlist entries that are for local symbols. */
  107. int local_sym_count;
  108. /* Count number of nlist entries that are for local symbols
  109. whose names don't start with L. */
  110. int non_L_local_sym_count;
  111. /* Count the number of nlist entries for debugger info. */
  112. int debugger_sym_count;
  113. /* Count the number of global symbols referenced or defined. */
  114. int global_sym_count;
  115. /* Total number of symbols to be preserved in the current file. */
  116. int nsyms;
  117. /* Number of files specified in the command line. */
  118. int number_of_files;
  119. /* Each specified file has a file_entry structure for it.
  120. These are contained in the vector which file_table points to. */
  121. struct file_entry {
  122. char *filename;
  123. HEADER_TYPE header; /* the file's header */
  124. int ss_size; /* size, in bytes, of symbols_and_strings data */
  125. struct nlist *symbols_and_strings;
  126. };
  127. struct file_entry *file_table;
  128. /* Descriptor on which current file is open. */
  129. int input_desc;
  130. /* Stream for writing that file using stdio. */
  131. FILE *outstream;
  132. /* 1 => strip all symbols; 2 => strip all debugger symbols */
  133. int strip_symbols;
  134. /* 1 => discard locals starting with L; 2 => discard all locals */
  135. int discard_locals;
  136. void strip_file ();
  137. int file_open ();
  138. void rewrite_file_symbols(), file_close();
  139. int read_header (), read_entry_symbols (), read_file_symbols ();
  140. void count_file_symbols ();
  141. char *concat ();
  142. main (argc, argv)
  143. char **argv;
  144. int argc;
  145. {
  146. int c;
  147. extern int optind;
  148. struct file_entry *p;
  149. int i;
  150. strip_symbols = 0; /* default is to strip everything. */
  151. discard_locals = 0;
  152. while ((c = getopt (argc, argv, "sSxX")) != EOF)
  153. switch (c)
  154. {
  155. case 's':
  156. strip_symbols = 1;
  157. break;
  158. case 'S':
  159. strip_symbols = 2;
  160. break;
  161. case 'x':
  162. discard_locals = 2;
  163. break;
  164. case 'X':
  165. discard_locals = 1;
  166. break;
  167. }
  168. /* Default is to strip all symbols. */
  169. if (strip_symbols == 0 && discard_locals == 0)
  170. strip_symbols = 1;
  171. number_of_files = argc - optind;
  172. if (!number_of_files)
  173. fatal ("no files specified", 0);
  174. p = file_table
  175. = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  176. /* Now fill in file_table */
  177. for (i = 0; i < number_of_files; i++)
  178. {
  179. p->filename = argv[i + optind];
  180. p->symbols_and_strings = 0;
  181. p++;
  182. }
  183. for (i = 0; i < number_of_files; i++)
  184. strip_file (&file_table[i]);
  185. }
  186. /* process one input file */
  187. void
  188. strip_file (entry)
  189. struct file_entry *entry;
  190. {
  191. int val;
  192. local_sym_count = 0;
  193. non_L_local_sym_count = 0;
  194. debugger_sym_count = 0;
  195. global_sym_count = 0;
  196. val = file_open (entry);
  197. if (val < 0)
  198. return;
  199. if (strip_symbols != 1)
  200. /* Read in the existing symbols unless we are discarding everything. */
  201. {
  202. if (read_file_symbols (entry) < 0)
  203. return;
  204. }
  205. rewrite_file_symbols (entry);
  206. if (strip_symbols != 1)
  207. free (entry->symbols_and_strings);
  208. file_close ();
  209. }
  210. /** Convenient functions for operating on one or all files being processed. */
  211. /* Close the file that is now open. */
  212. void
  213. file_close ()
  214. {
  215. close (input_desc);
  216. input_desc = 0;
  217. }
  218. /* Open the file specified by 'entry', and return a descriptor.
  219. The descriptor is also saved in input_desc. */
  220. /* JF this also makes sure the file is in rel format */
  221. int
  222. file_open (entry)
  223. struct file_entry *entry;
  224. {
  225. int desc;
  226. int len, magicnum;
  227. desc = open (entry->filename, O_RDWR, 0);
  228. if (desc > 0)
  229. {
  230. input_desc = desc;
  231. #ifdef HEADER_SEEK_FD
  232. /* Skip the headers that encapsulate our data in some other format
  233. such as COFF. */
  234. HEADER_SEEK_FD (desc);
  235. #endif
  236. len = read (input_desc, (char *)&magicnum, sizeof magicnum);
  237. if (len != sizeof magicnum)
  238. {
  239. error_with_file ("failure reading header", entry);
  240. return -1;
  241. }
  242. if (N_BADMAG (*((struct exec *)&magicnum)))
  243. {
  244. error_with_file ("malformed input file (not an object file)", entry);
  245. return -1;
  246. }
  247. if (read_header (desc, &entry->header, entry) < 0)
  248. return -1;
  249. return desc;
  250. }
  251. perror_file (entry);
  252. return -1;
  253. }
  254. /* Print the filename of ENTRY on OUTFILE (a stdio stream), then a newline. */
  255. prline_file_name (entry, outfile)
  256. struct file_entry *entry;
  257. FILE *outfile;
  258. {
  259. print_file_name (entry, outfile);
  260. fprintf (outfile, "\n");
  261. }
  262. /* Print the filename of ENTRY on OUTFILE (a stdio stream). */
  263. print_file_name (entry, outfile)
  264. struct file_entry *entry;
  265. FILE *outfile;
  266. {
  267. fprintf (outfile, "%s", entry->filename);
  268. }
  269. /* Validate file ENTRY and read its symbol and string sections into core. */
  270. int
  271. read_file_symbols (entry)
  272. struct file_entry *entry;
  273. {
  274. if (read_entry_symbols (input_desc, entry) < 0)
  275. return -1;
  276. count_file_symbols (entry);
  277. return 0;
  278. }
  279. /* Read a file's header into the proper place in the file_entry.
  280. Return -1 on failure. */
  281. int
  282. read_header (desc, loc, entry)
  283. int desc;
  284. struct exec *loc;
  285. struct file_entry *entry;
  286. {
  287. int len;
  288. lseek (desc, 0, 0);
  289. #ifdef HEADER_SEEK_FD
  290. /* Skip the headers that encapsulate our data in some other format
  291. such as COFF. */
  292. HEADER_SEEK_FD (desc);
  293. #endif
  294. len = read (desc, loc, sizeof (struct exec));
  295. if (len != sizeof (struct exec))
  296. {
  297. error_with_file ("failure reading header", entry);
  298. return -1;
  299. }
  300. if (N_BADMAG (*loc))
  301. {
  302. error_with_file ("bad magic number", entry);
  303. return -1;
  304. }
  305. return 0;
  306. }
  307. /* Read the symbols and strings of file ENTRY into core.
  308. Assume it is already open, on descriptor DESC.
  309. Return -1 on failure. */
  310. int
  311. read_entry_symbols (desc, entry)
  312. struct file_entry *entry;
  313. int desc;
  314. {
  315. int string_size;
  316. lseek (desc, N_STROFF (entry->header), 0);
  317. if (sizeof string_size != read (desc, &string_size, sizeof string_size))
  318. {
  319. error_with_file ("bad string table", entry);
  320. return -1;
  321. }
  322. entry->ss_size = string_size + entry->header.a_syms;
  323. entry->symbols_and_strings = (struct nlist *) xmalloc (entry->ss_size);
  324. lseek (desc, N_SYMOFF (entry->header), 0);
  325. if (entry->ss_size != read (desc, entry->symbols_and_strings, entry->ss_size))
  326. {
  327. error_with_file ("premature end of file in symbols/strings", entry);
  328. return -1;
  329. }
  330. return 0;
  331. }
  332. /* Count the number of symbols of various categories in the file of ENTRY. */
  333. void
  334. count_file_symbols (entry)
  335. struct file_entry *entry;
  336. {
  337. struct nlist *p, *end = entry->symbols_and_strings + entry->header.a_syms / sizeof (struct nlist);
  338. char *name_base = entry->header.a_syms + (char *) entry->symbols_and_strings;
  339. for (p = entry->symbols_and_strings; p < end; p++)
  340. if (p->n_type & N_EXT)
  341. global_sym_count++;
  342. else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  343. {
  344. if ((p->n_un.n_strx + name_base)[0] != 'L')
  345. non_L_local_sym_count++;
  346. local_sym_count++;
  347. }
  348. else debugger_sym_count++;
  349. }
  350. void write_file_syms (), modify_relocation ();
  351. /* Total size of string table strings allocated so far */
  352. int strtab_size;
  353. /* Vector whose elements are the strings to go in the string table */
  354. char **strtab_vector;
  355. /* Index in strtab_vector at which the next string will be stored */
  356. int strtab_index;
  357. int sym_written_count;
  358. int
  359. assign_string_table_index (name)
  360. char *name;
  361. {
  362. int index = strtab_size;
  363. strtab_size += strlen (name) + 1;
  364. strtab_vector[strtab_index++] = name;
  365. return index;
  366. }
  367. void
  368. rewrite_file_symbols (entry)
  369. struct file_entry *entry;
  370. {
  371. int i;
  372. struct nlist *newsyms;
  373. /* Calculate number of symbols to be preserved. */
  374. if (strip_symbols == 1)
  375. nsyms = 0;
  376. else
  377. {
  378. nsyms = global_sym_count;
  379. if (discard_locals == 1)
  380. nsyms += non_L_local_sym_count;
  381. else if (discard_locals == 0)
  382. nsyms += local_sym_count;
  383. }
  384. if (strip_symbols == 0)
  385. nsyms += debugger_sym_count;
  386. strtab_vector = (char **) xmalloc (nsyms * sizeof (char *));
  387. strtab_index = 0;
  388. strtab_size = 4;
  389. /* Accumulate in 'newsyms' the symbol table to be written. */
  390. newsyms = (struct nlist *) xmalloc (nsyms * sizeof (struct nlist));
  391. sym_written_count = 0;
  392. if (strip_symbols != 1)
  393. /* Write into newsyms the symbols we want to keep. */
  394. write_file_syms (entry, newsyms);
  395. if (sym_written_count != nsyms)
  396. {
  397. fprintf (stderr, "written = %d, expected = %d\n",
  398. sym_written_count, nsyms);
  399. abort ();
  400. }
  401. /* Modify the symbol-numbers in the relocation in the file,
  402. to preserve its meaning */
  403. modify_relocation (input_desc, entry);
  404. #ifndef HAVE_FTRUNCATE
  405. {
  406. int size = N_SYMOFF (entry->header), mode;
  407. char *renamed = (char *)concat ("~", entry->filename, "~");
  408. char *copy_buffer = (char *)xmalloc (size);
  409. struct stat statbuf;
  410. lseek (input_desc, 0, 0);
  411. if (read (input_desc, copy_buffer, size) != size)
  412. {
  413. error_with_file ("can't read up to symbol table", entry);
  414. return;
  415. }
  416. mode = fstat (input_desc, &statbuf) ? 0666 : statbuf.st_mode;
  417. if (rename (entry->filename, renamed))
  418. {
  419. perror_file (entry);
  420. return;
  421. }
  422. input_desc = creat (entry->filename, mode);
  423. if (input_desc < 0)
  424. {
  425. perror_file (entry);
  426. return;
  427. }
  428. if (write (input_desc, copy_buffer, size) != size)
  429. perror_file (entry);
  430. if (unlink (renamed))
  431. perror_name (renamed);
  432. free (copy_buffer);
  433. free (renamed);
  434. }
  435. #endif /* not HAVE_FTRUNCATE */
  436. /* Now write contents of NEWSYMS into the file. */
  437. lseek (input_desc, N_SYMOFF (entry->header), 0);
  438. write (input_desc, newsyms, nsyms * sizeof (struct nlist));
  439. free (newsyms);
  440. /* Now write the string table. */
  441. {
  442. char *strvec = (char *) xmalloc (strtab_size);
  443. char *p;
  444. *((long *) strvec) = strtab_size;
  445. p = strvec + sizeof (long);
  446. for (i = 0; i < strtab_index; i++)
  447. {
  448. int len = strlen (strtab_vector[i]);
  449. strcpy (p, strtab_vector[i]);
  450. *(p+len) = 0;
  451. p += len + 1;
  452. }
  453. write (input_desc, strvec, strtab_size);
  454. free (strvec);
  455. }
  456. /* Adjust file to be smaller */
  457. #ifdef HAVE_FTRUNCATE
  458. if (ftruncate (input_desc, tell (input_desc)) < 0)
  459. perror_file (entry);
  460. #endif
  461. /* Write new symbol table size into file header. */
  462. entry->header.a_syms = nsyms * sizeof (struct nlist);
  463. lseek (input_desc, 0, 0);
  464. write (input_desc, &entry->header, sizeof (struct exec));
  465. free (strtab_vector);
  466. }
  467. /* Copy into NEWSYMS the symbol entries to be preserved.
  468. Count them in sym_written_count. */
  469. /* We record, for each symbol written, its symbol number in the resulting file.
  470. This is so that the relocation can be updated later.
  471. Since the symbol names will not be needed again,
  472. this index goes in the `n_strx' field.
  473. If a symbol is not written, -1 is stored there. */
  474. void
  475. write_file_syms (entry, newsyms)
  476. struct file_entry *entry;
  477. struct nlist *newsyms;
  478. {
  479. struct nlist *p = entry->symbols_and_strings;
  480. struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  481. char *string_base = (char *) end; /* address of start of file's string table */
  482. struct nlist *outp = newsyms;
  483. for (; p < end; p++)
  484. {
  485. int type = p->n_type;
  486. int write;
  487. if (p->n_type & N_EXT)
  488. write = 1;
  489. else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  490. /* ordinary local symbol */
  491. write = (discard_locals != 2)
  492. && !(discard_locals == 1 &&
  493. (p->n_un.n_strx + string_base)[0] == 'L');
  494. else
  495. /* debugger symbol */
  496. write = (strip_symbols == 0);
  497. if (write)
  498. {
  499. if (p->n_un.n_strx)
  500. p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx + string_base);
  501. *outp++ = *p;
  502. p->n_un.n_strx = sym_written_count++;
  503. }
  504. else p->n_un.n_strx = -1;
  505. }
  506. }
  507. /* Read in ENTRY's relocation, alter the symbolnums in it,
  508. and write it out again. */
  509. void
  510. modify_relocation (desc, entry)
  511. int desc;
  512. struct file_entry *entry;
  513. {
  514. struct relocation_info *reloc, *p, *end;
  515. int size = entry->header.a_trsize + entry->header.a_drsize;
  516. struct nlist *sym_base = (struct nlist *) entry->symbols_and_strings;
  517. int losing = 0;
  518. reloc = (struct relocation_info *) xmalloc (size);
  519. lseek (desc, N_TXTOFF (entry->header) + entry->header.a_text + entry->header.a_data, 0);
  520. read (desc, reloc, size);
  521. p = reloc;
  522. end = (struct relocation_info *) (size + (char *) reloc);
  523. while (p < end)
  524. {
  525. if (p->r_extern)
  526. {
  527. int newnum = (sym_base == 0 ? -1
  528. :(sym_base + p->r_symbolnum) -> n_un.n_strx);
  529. if (newnum < 0)
  530. {
  531. if (losing == 0)
  532. error_with_file ("warning: file is now unlinkable", entry);
  533. losing = 1;
  534. }
  535. p->r_symbolnum = newnum;
  536. }
  537. p++;
  538. }
  539. lseek (desc, N_TXTOFF (entry->header) + entry->header.a_text + entry->header.a_data, 0);
  540. write (desc, reloc, size);
  541. }
  542. /* Report a fatal error.
  543. STRING is a printf format string and ARG is one arg for it. */
  544. fatal (string, arg)
  545. char *string, *arg;
  546. {
  547. fprintf (stderr, "strip: ");
  548. fprintf (stderr, string, arg);
  549. fprintf (stderr, "\n");
  550. exit (1);
  551. }
  552. /* Report an error using the message for the last failed system call,
  553. followed by the string NAME. */
  554. perror_name (name)
  555. char *name;
  556. {
  557. extern int errno, sys_nerr;
  558. extern char *sys_errlist[];
  559. char *s;
  560. if (errno < sys_nerr)
  561. s = concat ("", sys_errlist[errno], " for %s");
  562. else
  563. s = "cannot open %s";
  564. error (s, name);
  565. }
  566. /* Report an error using the message for the last failed system call,
  567. followed by the name of file ENTRY. */
  568. perror_file (entry)
  569. struct file_entry *entry;
  570. {
  571. extern int errno, sys_nerr;
  572. extern char *sys_errlist[];
  573. char *s;
  574. if (errno < sys_nerr)
  575. s = sys_errlist[errno];
  576. else
  577. s = "cannot open";
  578. error_with_file (s, entry);
  579. }
  580. /* Report an error. STRING is printed, and the filename of ENTRY. */
  581. error_with_file (string, entry, arg1, arg2)
  582. char *string;
  583. struct file_entry *entry;
  584. int arg1, arg2;
  585. {
  586. fprintf (stderr, "strip: ");
  587. print_file_name (entry, stderr);
  588. fprintf (stderr, ": ");
  589. fprintf (stderr, string, arg1, arg2);
  590. fprintf (stderr, "\n");
  591. }
  592. /* Report a nonfatal error.
  593. STRING is a format for printf, and ARG1 ... ARG3 are args for it. */
  594. error (string, arg1, arg2, arg3)
  595. char *string, *arg1, *arg2, *arg3;
  596. {
  597. fprintf (stderr, string, arg1, arg2, arg3);
  598. fprintf (stderr, "\n");
  599. }
  600. /* Return a newly-allocated string whose contents
  601. concatenate those of S1, S2, S3. */
  602. char *
  603. concat (s1, s2, s3)
  604. char *s1, *s2, *s3;
  605. {
  606. int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  607. char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  608. strcpy (result, s1);
  609. strcpy (result + len1, s2);
  610. strcpy (result + len1 + len2, s3);
  611. *(result + len1 + len2 + len3) = 0;
  612. return result;
  613. }
  614. /* Like malloc but get fatal error if memory is exhausted. */
  615. int
  616. xmalloc (size)
  617. int size;
  618. {
  619. int result = malloc (size);
  620. if (!result)
  621. fatal ("virtual memory exhausted", 0);
  622. return result;
  623. }
  624. #ifdef USG
  625. rename (from, to)
  626. char *from, *to;
  627. {
  628. if (unlink (to) < 0
  629. || link (from, to) < 0
  630. || unlink (from) < 0)
  631. return -1;
  632. else
  633. return 0;
  634. }
  635. #endif