home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / gccmsdos.arj / GNU / BINUTY / STRIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-30  |  18.3 KB  |  771 lines

  1. /* strip certain symbols from a rel file.
  2.    Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <sys/file.h>
  21. #include <sys/stat.h>
  22. #include <signal.h>
  23. #include "getopt.h"
  24.  
  25. #ifdef USG
  26. #include <fcntl.h>
  27. #include <string.h>
  28. #else
  29. #include <strings.h>
  30. #endif
  31.  
  32. #ifdef COFF_ENCAPSULATE
  33. #include "a.out.encap.h"
  34. #else
  35. #ifdef TOWNS
  36. #incude  "a_out.h"
  37. #else
  38. /* On native BSD systems, use the system's own a.out.h.  */
  39. #include <a.out.h>
  40. #endif
  41. #endif
  42.  
  43. #ifdef nounderscore
  44. #define LPREFIX '.'
  45. #else
  46. #define LPREFIX 'L'
  47. #endif
  48.  
  49. /* If BSD, we can use `ftruncate'.  */
  50.  
  51. #ifndef USG
  52. #define HAVE_FTRUNCATE
  53. #endif
  54.  
  55. /* Struct or union for header of object file.  */
  56.  
  57. #define HEADER_TYPE struct exec
  58.  
  59. /* Count the number of nlist entries that are for local symbols. */
  60. int local_sym_count;
  61.  
  62. /* Count number of nlist entries that are for local symbols
  63.    whose names don't start with L. */
  64. int non_L_local_sym_count;
  65.  
  66. /* Count the number of nlist entries for debugger info.  */
  67. int debugger_sym_count;
  68.  
  69. /* Count the number of global symbols referenced or defined.  */
  70. int global_sym_count;
  71.  
  72. /* Total number of symbols to be preserved in the current file.  */
  73. int nsyms;
  74.  
  75. /* Number of files specified in the command line. */
  76.  
  77. int number_of_files;
  78.  
  79. /* Each specified file has a file_entry structure for it.
  80.    These are contained in the vector which file_table points to.  */
  81.  
  82. struct file_entry {
  83.   char *filename;
  84.   HEADER_TYPE header;        /* the file's header */
  85.   int ss_size;            /* size, in bytes, of symbols_and_strings data */
  86.   struct nlist *symbols_and_strings;
  87. };
  88.  
  89. struct file_entry *file_table;
  90.  
  91. /* Descriptor on which current file is open.  */
  92.  
  93. int input_desc;
  94.  
  95. /* Stream for writing that file using stdio.  */
  96.  
  97. FILE *outstream;
  98.  
  99. /* 1 => strip all symbols; 2 => strip all debugger symbols */
  100. int strip_symbols;
  101.  
  102. /* 1 => discard locals starting with L; 2 => discard all locals */
  103. int discard_locals;
  104.  
  105. void strip_file ();
  106. int file_open ();
  107. void rewrite_file_symbols(), file_close();
  108. int read_header (), read_entry_symbols (), read_file_symbols ();
  109. void count_file_symbols ();
  110. char *concat ();
  111.  
  112. main (argc, argv)
  113.      char **argv;
  114.      int argc;
  115. {
  116.   int c;
  117.   extern int optind;
  118.   int ind;
  119.   
  120.   /* structure containing the short options expanded into long form */
  121.   static struct option long_options[] =
  122.     {{"strip-all",   0, &strip_symbols,  1},
  123.      {"strip-debug", 0, &strip_symbols,  2},
  124.      {"discard-all", 0, &discard_locals, 2},
  125.      {"discard-locals",   0, &discard_locals, 1}};
  126.   
  127.   /* string which will hold the name of the long option, if it exists */
  128. /*  char *name = '\0';*/
  129.  
  130.   struct file_entry *p;
  131.   int i;
  132.  
  133.   strip_symbols = 0;   /* default is to strip everything.  */
  134.   discard_locals = 0;
  135.  
  136.   while ((c = getopt_long (argc, argv, "sSxX", long_options, &ind)) != EOF) 
  137.  
  138.  
  139.       switch (c)
  140.     {
  141.     case  0 :
  142.       break;
  143.     case 's':
  144.       strip_symbols = 1;
  145.       break;
  146.     case 'S':
  147.       strip_symbols = 2;
  148.       break;
  149.     case 'x':
  150.       discard_locals = 2;
  151.       break;
  152.     case 'X':
  153.       discard_locals = 1;
  154.       break;
  155.     }
  156.  
  157.   /* Default is to strip all symbols.  */
  158.   if (strip_symbols == 0 && discard_locals == 0)
  159.     strip_symbols = 1;
  160.  
  161.   number_of_files = argc - optind;
  162.  
  163.   if (!number_of_files)
  164.     fatal ("no files specified", 0);
  165.  
  166.   p = file_table
  167.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  168.  
  169.   /* Now fill in file_table */
  170.  
  171.   for (i = 0; i < number_of_files; i++)
  172.     {
  173.       p->filename = argv[i + optind];
  174.       p->symbols_and_strings = 0;
  175.       p++;
  176.     }
  177.  
  178.   for (i = 0; i < number_of_files; i++)
  179.     strip_file (&file_table[i]);
  180. }
  181.  
  182. int delayed_signal;
  183.  
  184. void
  185. delay_signal (signo)
  186.      int signo;
  187. {
  188.   delayed_signal = signo;
  189.   signal (signo, delay_signal);
  190. }
  191.  
  192. /* process one input file */
  193.  
  194. void
  195. strip_file (entry)
  196.      struct file_entry *entry;
  197. {
  198.   int val;
  199.   int sigint_handled = 0;
  200.   int sighup_handled = 0;
  201.   int sigterm_handled = 0;
  202.  
  203.   local_sym_count = 0;
  204.   non_L_local_sym_count = 0;
  205.   debugger_sym_count = 0;
  206.   global_sym_count = 0;
  207.  
  208.   val = file_open (entry);
  209.   if (val < 0)
  210.     return;
  211.  
  212.   if (strip_symbols != 1)
  213.     /* Read in the existing symbols unless we are discarding everything.  */
  214.     {
  215.       if (read_file_symbols (entry) < 0)
  216.     return;
  217.     }
  218.  
  219.   /* Effectively defer handling of asynchronous kill signals.  */
  220.   delayed_signal = 0;
  221.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  222.     sigint_handled = 1, signal (SIGINT, delay_signal);
  223.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  224.     sighup_handled = 1, signal (SIGHUP, delay_signal);
  225.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  226.     sigterm_handled = 1, signal (SIGTERM, delay_signal);
  227.  
  228.   /* Change the file.  */
  229.  
  230.   rewrite_file_symbols (entry);
  231.   if (strip_symbols != 1)
  232.     free (entry->symbols_and_strings);
  233.  
  234.   file_close ();
  235.  
  236.   /* Effectively undefer handling.  */
  237.   if (sigint_handled)
  238.     signal (SIGINT, SIG_DFL);
  239.   if (sighup_handled)
  240.     signal (SIGHUP, SIG_DFL);
  241.   if (sigterm_handled)
  242.     signal (SIGTERM, SIG_DFL);
  243.  
  244.   /* Handle any signal that came in while they were deferred.  */
  245.   if (delayed_signal)
  246.     kill (getpid (), delayed_signal);
  247. }
  248.  
  249. /** Convenient functions for operating on one or all files being processed.  */
  250.  
  251. /* Close the file that is now open.  */
  252.  
  253. void
  254. file_close ()
  255. {
  256.   close (input_desc);
  257.   input_desc = 0;
  258. }
  259.  
  260. /* Open the file specified by 'entry', and return a descriptor.
  261.    The descriptor is also saved in input_desc.  */
  262.  
  263. /* JF this also makes sure the file is in rel format */
  264.  
  265. int
  266. file_open (entry)
  267.      struct file_entry *entry;
  268. {
  269.   int desc;
  270.   int len, magicnum;
  271.  
  272.   desc = open (entry->filename, O_RDWR, 0);
  273.  
  274.   if (desc > 0)
  275.     {
  276.       input_desc = desc;
  277. #ifdef HEADER_SEEK_FD
  278.       /* Skip the headers that encapsulate our data in some other format
  279.      such as COFF.  */
  280.       HEADER_SEEK_FD (desc);
  281. #endif
  282.       len = read (input_desc, (char *)&magicnum, sizeof magicnum);
  283.       if (len != sizeof magicnum)
  284.     {
  285.       error_with_file ("failure reading header", entry);
  286.       return -1;
  287.     }
  288.       if (N_BADMAG (*((struct exec *)&magicnum)))
  289.     {
  290.       error_with_file ("malformed input file (not an object file)", entry);
  291.       return -1;
  292.     }
  293.       if (read_header (desc, &entry->header, entry) < 0)
  294.     return -1;
  295.       return desc;
  296.     }
  297.  
  298.   perror_file (entry);
  299.   return -1;
  300. }
  301.  
  302. /* Print the filename of ENTRY on OUTFILE (a stdio stream), then a newline.  */
  303.  
  304. prline_file_name (entry, outfile)
  305.      struct file_entry *entry;
  306.      FILE *outfile;
  307. {
  308.   print_file_name (entry, outfile);
  309.   fprintf (outfile, "\n");
  310. }
  311.  
  312. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  313.  
  314. print_file_name (entry, outfile)
  315.      struct file_entry *entry;
  316.      FILE *outfile;
  317. {
  318.   fprintf (outfile, "%s", entry->filename);
  319. }
  320.  
  321. /* Validate file ENTRY and read its symbol and string sections into core. */
  322.  
  323. int
  324. read_file_symbols (entry)
  325.      struct file_entry *entry;
  326. {
  327.   if (read_entry_symbols (input_desc, entry) < 0)
  328.     return -1;
  329.   count_file_symbols (entry);
  330.   return 0;
  331. }
  332.  
  333. /* Read a file's header into the proper place in the file_entry.
  334.    Return -1 on failure.  */
  335.  
  336. int
  337. read_header (desc, loc, entry)
  338.      int desc;
  339.      struct exec *loc;
  340.      struct file_entry *entry;
  341. {
  342.   int len;
  343.   lseek (desc, 0, 0);
  344. #ifdef HEADER_SEEK_FD
  345.   /* Skip the headers that encapsulate our data in some other format
  346.      such as COFF.  */
  347.   HEADER_SEEK_FD (desc);
  348. #endif
  349.   len = read (desc, loc, sizeof (struct exec));
  350.   if (len != sizeof (struct exec))
  351.     {
  352.       error_with_file ("failure reading header", entry);
  353.       return -1;
  354.     }
  355.   if (N_BADMAG (*loc))
  356.     {
  357.       error_with_file ("bad magic number", entry);
  358.       return -1;
  359.     }
  360.   return 0;
  361. }
  362.  
  363. /* Read the symbols and strings of file ENTRY into core.
  364.    Assume it is already open, on descriptor DESC.
  365.    Return -1 on failure.  */
  366.  
  367. int
  368. read_entry_symbols (desc, entry)
  369.      struct file_entry *entry;
  370.      int desc;
  371. {
  372.   int string_size;
  373.  
  374.   lseek (desc, N_STROFF (entry->header), 0);
  375.   if (sizeof string_size != read (desc, &string_size, sizeof string_size))
  376.     {
  377.       error_with_file ("bad string table", entry);
  378.       return -1;
  379.     }
  380.  
  381.   entry->ss_size = string_size + entry->header.a_syms;
  382.   entry->symbols_and_strings = (struct nlist *) xmalloc (entry->ss_size);
  383.  
  384.   lseek (desc, N_SYMOFF (entry->header), 0);
  385.   if (entry->ss_size != read (desc, entry->symbols_and_strings, entry->ss_size))
  386.     {
  387.       error_with_file ("premature end of file in symbols/strings", entry);
  388.       return -1;
  389.     }
  390.   return 0;
  391. }
  392.  
  393.  
  394. /* Count the number of symbols of various categories in the file of ENTRY.  */
  395.  
  396. void
  397. count_file_symbols (entry)
  398.      struct file_entry *entry;
  399. {
  400.   struct nlist *p, *end = entry->symbols_and_strings + entry->header.a_syms / sizeof (struct nlist);
  401.   char *name_base = entry->header.a_syms + (char *) entry->symbols_and_strings;
  402.  
  403.   for (p = entry->symbols_and_strings; p < end; p++)
  404.     if (p->n_type & N_EXT)
  405.       global_sym_count++;
  406.     else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  407.       {
  408.     if ((p->n_un.n_strx + name_base)[0] != LPREFIX)
  409.       non_L_local_sym_count++;
  410.     local_sym_count++;
  411.       }
  412.     else debugger_sym_count++;
  413. }
  414.  
  415. void write_file_syms (), modify_relocation ();
  416.  
  417. /* Total size of string table strings allocated so far */
  418. int strtab_size;
  419.  
  420. /* Vector whose elements are the strings to go in the string table */
  421. char **strtab_vector;
  422.  
  423. /* Index in strtab_vector at which the next string will be stored */
  424. int strtab_index;
  425.  
  426. int sym_written_count;
  427.  
  428. int
  429. assign_string_table_index (name)
  430.      char *name;
  431. {
  432.   int index = strtab_size;
  433.  
  434.   strtab_size += strlen (name) + 1;
  435.   strtab_vector[strtab_index++] = name;
  436.  
  437.   return index;
  438. }
  439.  
  440. void
  441. rewrite_file_symbols (entry)
  442.      struct file_entry *entry;
  443. {
  444.   int i;
  445.   struct nlist *newsyms;
  446.  
  447.   /* Calculate number of symbols to be preserved.  */
  448.  
  449.   if (strip_symbols == 1)
  450.     nsyms = 0;
  451.   else
  452.     {
  453.       nsyms = global_sym_count;
  454.       if (discard_locals == 1)
  455.     nsyms += non_L_local_sym_count;
  456.       else if (discard_locals == 0)
  457.     nsyms += local_sym_count;
  458.     }
  459.  
  460.   if (strip_symbols == 0)
  461.     nsyms += debugger_sym_count;
  462.  
  463.   strtab_vector = (char **) xmalloc (nsyms * sizeof (char *));
  464.   strtab_index = 0;
  465.  
  466.   strtab_size = 4;
  467.  
  468.   /* Accumulate in 'newsyms' the symbol table to be written.  */
  469.  
  470.   newsyms = (struct nlist *) xmalloc (nsyms * sizeof (struct nlist));
  471.  
  472.   sym_written_count = 0;
  473.  
  474.   if (strip_symbols != 1)
  475.     /* Write into newsyms the symbols we want to keep.  */
  476.     write_file_syms (entry, newsyms);
  477.  
  478.   if (sym_written_count != nsyms)
  479.     {
  480.       fprintf (stderr, "written = %d, expected = %d\n",
  481.            sym_written_count, nsyms);
  482.       abort ();
  483.     }
  484.  
  485.   /* Modify the symbol-numbers in the relocation in the file,
  486.      to preserve its meaning */
  487.   modify_relocation (input_desc, entry);
  488.  
  489. #ifndef    HAVE_FTRUNCATE
  490.   {
  491.     int size = N_SYMOFF (entry->header), mode;
  492.     char *renamed = (char *)concat ("~", entry->filename, "~");
  493.     char *copy_buffer = (char *)xmalloc (size);
  494.     struct stat statbuf;
  495.  
  496.     lseek (input_desc, 0, 0);
  497.     if (read (input_desc, copy_buffer, size) != size)
  498.       {
  499.     error_with_file ("can't read up to symbol table", entry);
  500.     return;
  501.       }
  502.     mode = fstat (input_desc, &statbuf) ? 0666 : statbuf.st_mode;
  503.     if (rename (entry->filename, renamed))
  504.       {
  505.     perror_file (entry);
  506.     return;
  507.       }
  508.     input_desc = creat (entry->filename, mode);
  509.     if (input_desc < 0)
  510.       {
  511.     perror_file (entry);
  512.     return;
  513.       }
  514.     if (write (input_desc, copy_buffer, size) != size)
  515.       perror_file (entry);
  516.     if (unlink (renamed))
  517.       perror_name (renamed);
  518.     free (copy_buffer);
  519.     free (renamed);
  520.   }
  521. #endif /* not HAVE_FTRUNCATE */
  522.  
  523.   /* Now write contents of NEWSYMS into the file. */
  524.  
  525.   lseek (input_desc, N_SYMOFF (entry->header), 0);
  526.   write (input_desc, newsyms, nsyms * sizeof (struct nlist));
  527.   free (newsyms);
  528.  
  529.   /* Now write the string table.  */
  530.  
  531.   {
  532.     char *strvec = (char *) xmalloc (strtab_size);
  533.     char *p;
  534.  
  535.     *((long *) strvec) = strtab_size;
  536.  
  537.     p = strvec + sizeof (long);
  538.  
  539.     for (i = 0; i < strtab_index; i++)
  540.       {
  541.     int len = strlen (strtab_vector[i]);
  542.     strcpy (p, strtab_vector[i]);
  543.     *(p+len) = 0;
  544.     p += len + 1;
  545.       }
  546.  
  547.     write (input_desc, strvec, strtab_size);
  548.     free (strvec);
  549.   }
  550.  
  551.   /* Adjust file to be smaller */
  552.  
  553. #ifdef HAVE_FTRUNCATE
  554.   if (ftruncate (input_desc, tell (input_desc)) < 0)
  555.     perror_file (entry);
  556. #endif
  557.  
  558.   /* Write new symbol table size into file header.  */
  559.  
  560.   entry->header.a_syms = nsyms * sizeof (struct nlist);
  561.  
  562.   lseek (input_desc, 0, 0);
  563.   write (input_desc, &entry->header, sizeof (struct exec));
  564.  
  565.   free (strtab_vector);
  566. }
  567.  
  568. /* Copy into NEWSYMS the symbol entries to be preserved.
  569.    Count them in sym_written_count.  */
  570.  
  571. /* We record, for each symbol written, its symbol number in the resulting file.
  572.    This is so that the relocation can be updated later.
  573.    Since the symbol names will not be needed again,
  574.    this index goes in the `n_strx' field.
  575.    If a symbol is not written, -1 is stored there.  */
  576.  
  577. void
  578. write_file_syms (entry, newsyms)
  579.      struct file_entry *entry;
  580.      struct nlist *newsyms;
  581. {
  582.   struct nlist *p = entry->symbols_and_strings;
  583.   struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  584.   char *string_base = (char *) end;   /* address of start of file's string table */
  585.   struct nlist *outp = newsyms;
  586.  
  587.   for (; p < end; p++)
  588.     {
  589.       int type = p->n_type;
  590.       int write;
  591.   
  592.       if (p->n_type & N_EXT)
  593.     write = 1;
  594.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  595.     /* ordinary local symbol */
  596.     write = (discard_locals != 2)
  597.         && !(discard_locals == 1 &&
  598.              (p->n_un.n_strx + string_base)[0] == LPREFIX);
  599.       else
  600.     /* debugger symbol */
  601.     write = (strip_symbols == 0);
  602.  
  603.       if (write)
  604.     {
  605.       if (p->n_un.n_strx)
  606.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx + string_base);
  607.  
  608.       *outp++ = *p;
  609.  
  610.       p->n_un.n_strx = sym_written_count++;
  611.     }
  612.       else p->n_un.n_strx = -1;
  613.     }
  614. }
  615.  
  616. /* Read in ENTRY's relocation, alter the symbolnums in it,
  617.    and write it out again.  */
  618.  
  619. void
  620. modify_relocation (desc, entry)
  621.      int desc;
  622.      struct file_entry *entry;
  623. {
  624.   struct relocation_info *reloc, *p, *end;
  625.   int size = entry->header.a_trsize + entry->header.a_drsize;
  626.   struct nlist *sym_base = (struct nlist *) entry->symbols_and_strings;
  627.   int losing = 0;
  628.  
  629.   reloc = (struct relocation_info *) xmalloc (size);
  630.   lseek (desc, N_TXTOFF (entry->header) + entry->header.a_text + entry->header.a_data, 0);
  631.   read (desc, reloc, size);
  632.  
  633.   p = reloc;
  634.   end = (struct relocation_info *) (size + (char *) reloc);
  635.   while (p < end)
  636.     {
  637.       if (p->r_extern)
  638.     {
  639.       int newnum = (sym_base == 0 ? -1
  640.             :(sym_base + p->r_symbolnum) -> n_un.n_strx);
  641.       if (newnum < 0)
  642.         {
  643.           if (losing == 0)
  644.         error_with_file ("warning: file is now unlinkable", entry);
  645.           losing = 1;
  646.         }
  647.       p->r_symbolnum = newnum;
  648.     }
  649.       p++;
  650.     }
  651.  
  652.   lseek (desc, N_TXTOFF (entry->header) + entry->header.a_text + entry->header.a_data, 0);
  653.   write (desc, reloc, size);
  654. }
  655.  
  656. /* Report a fatal error.
  657.    STRING is a printf format string and ARG is one arg for it.  */
  658.  
  659. fatal (string, arg)
  660.      char *string, *arg;
  661. {
  662.   fprintf (stderr, "strip: ");
  663.   fprintf (stderr, string, arg);
  664.   fprintf (stderr, "\n");
  665.   exit (1);
  666. }
  667.  
  668. /* Report an error using the message for the last failed system call,
  669.    followed by the string NAME.  */
  670.  
  671. perror_name (name)
  672.      char *name;
  673. {
  674.   extern int errno, sys_nerr;
  675.   extern char *sys_errlist[];
  676.   char *s;
  677.  
  678.   if (errno < sys_nerr)
  679.     s = concat ("", sys_errlist[errno], " for %s");
  680.   else
  681.     s = "cannot open %s";
  682.   error (s, name);
  683. }
  684.  
  685. /* Report an error using the message for the last failed system call,
  686.    followed by the name of file ENTRY.  */
  687.  
  688. perror_file (entry)
  689.      struct file_entry *entry;
  690. {
  691.   extern int errno, sys_nerr;
  692.   extern char *sys_errlist[];
  693.   char *s;
  694.  
  695.   if (errno < sys_nerr)
  696.     s = sys_errlist[errno];
  697.   else
  698.     s = "cannot open";
  699.   error_with_file (s, entry);
  700. }
  701.  
  702. /* Report an error.   STRING is printed, and the filename of ENTRY.  */
  703.  
  704. error_with_file (string, entry, arg1, arg2)
  705.      char *string;
  706.      struct file_entry *entry;
  707.      int arg1, arg2;
  708. {
  709.   fprintf (stderr, "strip: ");
  710.   print_file_name (entry, stderr);
  711.   fprintf (stderr, ": ");
  712.   fprintf (stderr, string, arg1, arg2);
  713.   fprintf (stderr, "\n");
  714. }
  715.  
  716. /* Report a nonfatal error.
  717.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  718.  
  719. error (string, arg1, arg2, arg3)
  720.      char *string, *arg1, *arg2, *arg3;
  721. {
  722.   fprintf (stderr, string, arg1, arg2, arg3);
  723.   fprintf (stderr, "\n");
  724. }
  725.  
  726. /* Return a newly-allocated string whose contents 
  727.    concatenate those of S1, S2, S3.  */
  728.  
  729. char *
  730. concat (s1, s2, s3)
  731.      char *s1, *s2, *s3;
  732. {
  733.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  734.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  735.  
  736.   strcpy (result, s1);
  737.   strcpy (result + len1, s2);
  738.   strcpy (result + len1 + len2, s3);
  739.   *(result + len1 + len2 + len3) = 0;
  740.  
  741.   return result;
  742. }
  743.  
  744. /* Like malloc but get fatal error if memory is exhausted.  */
  745.  
  746. int
  747. xmalloc (size)
  748.      int size;
  749. {
  750.   int result = malloc (size);
  751.   if (!result)
  752.     fatal ("virtual memory exhausted", 0);
  753.   return result;
  754. }
  755.  
  756. #ifdef USG
  757.  
  758. rename (from, to)
  759.      char *from, *to;
  760. {
  761.   (void) unlink (to);
  762.   if (link (from, to) < 0
  763.       || unlink (from) < 0)
  764.     return -1;
  765.   else
  766.     return 0;
  767. }
  768.  
  769. #endif
  770.  
  771.