home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / fileutils-3.12-src.lha / fileutils-3.12 / src / du.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-12  |  17.7 KB  |  725 lines

  1. /* du -- summarize disk usage
  2.    Copyright (C) 1988, 1989, 1990, 1991 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 2, 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. /* Differences from the Unix du:
  19.    * Doesn't simply ignore the names of regular files given as arguments
  20.      when -a is given.
  21.    * Additional options:
  22.    -l        Count the size of all files, even if they have appeared
  23.         already in another hard link.
  24.    -x        Do not cross file-system boundaries during the recursion.
  25.    -c        Write a grand total of all of the arguments after all
  26.         arguments have been processed.  This can be used to find
  27.         out the disk usage of a directory, with some files excluded.
  28.    -k        Print sizes in kilobytes instead of 512 byte blocks
  29.         (the default required by POSIX).
  30.    -b        Print sizes in bytes.
  31.    -S        Count the size of each directory separately, not including
  32.         the sizes of subdirectories.
  33.    -D        Dereference only symbolic links given on the command line.
  34.    -L        Dereference all symbolic links.
  35.  
  36.    By tege@sics.se, Torbjorn Granlund,
  37.    and djm@ai.mit.edu, David MacKenzie.  */
  38.  
  39. #ifdef _AIX
  40.  #pragma alloca
  41. #endif
  42.  
  43. #include <config.h>
  44. #include <stdio.h>
  45. #include <getopt.h>
  46. #include <sys/types.h>
  47. #include "system.h"
  48. #include "version.h"
  49. #include "safe-stat.h"
  50. #include "safe-lstat.h"
  51.  
  52. /* Initial number of entries in each hash table entry's table of inodes.  */
  53. #define INITIAL_HASH_MODULE 100
  54.  
  55. /* Initial number of entries in the inode hash table.  */
  56. #define INITIAL_ENTRY_TAB_SIZE 70
  57.  
  58. /* Initial size to allocate for `path'.  */
  59. #define INITIAL_PATH_SIZE 100
  60.  
  61. /* Hash structure for inode and device numbers.  The separate entry
  62.    structure makes it easier to rehash "in place".  */
  63.  
  64. struct entry
  65. {
  66.   ino_t ino;
  67.   dev_t dev;
  68.   struct entry *coll_link;
  69. };
  70.  
  71. /* Structure for a hash table for inode numbers. */
  72.  
  73. struct htab
  74. {
  75.   unsigned modulus;        /* Size of the `hash' pointer vector.  */
  76.   struct entry *entry_tab;    /* Pointer to dynamically growing vector.  */
  77.   unsigned entry_tab_size;    /* Size of current `entry_tab' allocation.  */
  78.   unsigned first_free_entry;    /* Index in `entry_tab'.  */
  79.   struct entry *hash[1];    /* Vector of pointers in `entry_tab'.  */
  80. };
  81.  
  82.  
  83. /* Structure for dynamically resizable strings. */
  84.  
  85. typedef struct
  86. {
  87.   unsigned alloc;        /* Size of allocation for the text.  */
  88.   unsigned length;        /* Length of the text currently.  */
  89.   char *text;            /* Pointer to the text.  */
  90. } *string, stringstruct;
  91.  
  92. char *savedir ();
  93. #ifndef HAVE_FCHDIR
  94. char *xgetcwd ();
  95. #endif
  96. char *xmalloc ();
  97. char *xrealloc ();
  98. void error ();
  99.  
  100. static int hash_insert ();
  101. static int hash_insert2 ();
  102. static long count_entry ();
  103. static void du_files ();
  104. static void hash_init ();
  105. static void hash_reset ();
  106. static void str_concatc ();
  107. static void str_copyc ();
  108. static void str_init ();
  109. static void str_trunc ();
  110.  
  111. /* Name under which this program was invoked.  */
  112. char *program_name;
  113.  
  114. /* If nonzero, display only a total for each argument. */
  115. static int opt_summarize_only = 0;
  116.  
  117. /* If nonzero, display counts for all files, not just directories. */
  118. static int opt_all = 0;
  119.  
  120. /* If nonzero, count each hard link of files with multiple links. */
  121. static int opt_count_all = 0;
  122.  
  123. /* If nonzero, do not cross file-system boundaries. */
  124. static int opt_one_file_system = 0;
  125.  
  126. /* If nonzero, print a grand total at the end. */
  127. static int opt_combined_arguments = 0;
  128.  
  129. /* If nonzero, do not add sizes of subdirectories. */
  130. static int opt_separate_dirs = 0;
  131.  
  132. /* If nonzero, dereference symlinks that are command line arguments. */
  133. static int opt_dereference_arguments = 0;
  134.  
  135. enum output_size
  136. {
  137.   size_blocks,            /* 512-byte blocks. */
  138.   size_kilobytes,        /* 1K blocks. */
  139.   size_bytes            /* 1-byte blocks. */
  140. };
  141.  
  142. /* The units to count in. */
  143. static enum output_size output_size;
  144.  
  145. /* Accumulated path for file or directory being processed.  */
  146. static string path;
  147.  
  148. /* Pointer to hash structure, used by the hash routines.  */
  149. static struct htab *htab;
  150.  
  151. /* Globally used stat buffer.  */
  152. static struct stat stat_buf;
  153.  
  154. /* A pointer to either lstat or stat, depending on whether
  155.    dereferencing of all symbolic links is to be done. */
  156. static int (*xstat) ();
  157.  
  158. /* The exit status to use if we don't get any fatal errors. */
  159. static int exit_status;
  160.  
  161. /* If non-zero, display usage information and exit.  */
  162. static int show_help;
  163.  
  164. /* If non-zero, print the version on standard output and exit.  */
  165. static int show_version;
  166.  
  167. /* Grand total size of all args. */
  168. static long tot_size = 0L;
  169.  
  170. static struct option const long_options[] =
  171. {
  172.   {"all", no_argument, &opt_all, 1},
  173.   {"bytes", no_argument, NULL, 'b'},
  174.   {"count-links", no_argument, &opt_count_all, 1},
  175.   {"dereference", no_argument, NULL, 'L'},
  176.   {"dereference-args", no_argument, &opt_dereference_arguments, 1},
  177.   {"kilobytes", no_argument, NULL, 'k'},
  178.   {"one-file-system", no_argument, &opt_one_file_system, 1},
  179.   {"separate-dirs", no_argument, &opt_separate_dirs, 1},
  180.   {"summarize", no_argument, &opt_summarize_only, 1},
  181.   {"total", no_argument, &opt_combined_arguments, 1},
  182.   {"help", no_argument, &show_help, 1},
  183.   {"version", no_argument, &show_version, 1},
  184.   {NULL, 0, NULL, 0}
  185. };
  186.  
  187. static void
  188. usage (status, reason)
  189.      int status;
  190.      char *reason;
  191. {
  192.   if (reason != NULL)
  193.     fprintf (status == 0 ? stdout : stderr, "%s: %s\n",
  194.          program_name, reason);
  195.  
  196.   if (status != 0)
  197.     fprintf (stderr, "Try `%s --help' for more information.\n",
  198.          program_name);
  199.   else
  200.     {
  201.       printf ("Usage: %s [OPTION]... [PATH]...\n", program_name);
  202.       printf ("\
  203. \n\
  204.   -a, --all                write counts for all files, not just directories\n\
  205.   -b, --bytes              print size in bytes\n\
  206.   -c, --total              produce a grand total\n\
  207.   -k, --kilobytes          use 1024 blocks, not 512 despite POSIXLY_CORRECT\n\
  208.   -l, --count-links        count sizes many times if hard linked\n\
  209.   -s, --summarize          display only a total for each argument\n\
  210.   -x, --one-file-system    skip directories on different filesystems\n\
  211.   -D, --dereference-args   dereference PATHs when symbolic link\n\
  212.   -L, --dereference        dereference all symbolic links\n\
  213.   -S, --separate-dirs      do not include size of subdirectories\n\
  214.       --help               display this help and exit\n\
  215.       --version            output version information and exit\n");
  216.     }
  217.   exit (status);
  218. }
  219.  
  220. main (argc, argv)
  221.      int argc;
  222.      char *argv[];
  223. {
  224.   int c;
  225.   char *cwd_only[2];
  226.   
  227.   cwd_only[0] = ".";
  228.   cwd_only[1] = NULL;
  229.  
  230.   program_name = argv[0];
  231.   xstat = safe_lstat;
  232.   output_size = getenv ("POSIXLY_CORRECT") ? size_blocks : size_kilobytes;
  233.  
  234.   while ((c = getopt_long (argc, argv, "abcklsxDLS", long_options, (int *) 0))
  235.      != EOF)
  236.     {
  237.       switch (c)
  238.     {
  239.     case 0:            /* Long option. */
  240.       break;
  241.  
  242.     case 'a':
  243.       opt_all = 1;
  244.       break;
  245.  
  246.     case 'b':
  247.       output_size = size_bytes;
  248.       break;
  249.  
  250.     case 'c':
  251.       opt_combined_arguments = 1;
  252.       break;
  253.  
  254.     case 'k':
  255.       output_size = size_kilobytes;
  256.       break;
  257.  
  258.     case 'l':
  259.       opt_count_all = 1;
  260.       break;
  261.  
  262.     case 's':
  263.       opt_summarize_only = 1;
  264.       break;
  265.  
  266.     case 'x':
  267.       opt_one_file_system = 1;
  268.       break;
  269.  
  270.     case 'D':
  271.       opt_dereference_arguments = 1;
  272.       break;
  273.  
  274.     case 'L':
  275.       xstat = safe_stat;
  276.       break;
  277.  
  278.     case 'S':
  279.       opt_separate_dirs = 1;
  280.       break;
  281.  
  282.     default:
  283.       usage (2, (char *) 0);
  284.     }
  285.     }
  286.  
  287.   if (show_version)
  288.     {
  289.       printf ("%s\n", version_string);
  290.       exit (0);
  291.     }
  292.  
  293.   if (show_help)
  294.     usage (0, NULL);
  295.  
  296.   if (opt_all && opt_summarize_only)
  297.     usage (2, "cannot both summarize and show all entries");
  298.  
  299.   /* Initialize the hash structure for inode numbers.  */
  300.   hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
  301.  
  302.   str_init (&path, INITIAL_PATH_SIZE);
  303.  
  304.   du_files (optind == argc ? cwd_only : argv + optind);
  305.  
  306.   exit (exit_status);
  307. }
  308.  
  309. /* Recursively print the sizes of the directories (and, if selected, files)
  310.    named in FILES, the last entry of which is NULL.  */
  311.  
  312. static void
  313. du_files (files)
  314.      char **files;
  315. {
  316. #ifdef HAVE_FCHDIR
  317.   int wd_desc;
  318. #else
  319.   char *wd;
  320. #endif
  321.   ino_t initial_ino;        /* Initial directory's inode. */
  322.   dev_t initial_dev;        /* Initial directory's device. */
  323.   int i;            /* Index in FILES. */
  324.  
  325. #ifdef HAVE_FCHDIR
  326.   wd_desc = open (".", O_RDONLY);
  327.   if (wd_desc < 0)
  328.     error (1, errno, "cannot open current directory");
  329. #else
  330.   wd = xgetcwd ();
  331.   if (wd == NULL)
  332.     error (1, errno, "cannot get current directory");
  333. #endif
  334.  
  335.   /* Remember the inode and device number of the current directory.  */
  336.   if (SAFE_STAT (".", &stat_buf))
  337.     error (1, errno, "current directory");
  338.   initial_ino = stat_buf.st_ino;
  339.   initial_dev = stat_buf.st_dev;
  340.  
  341.   for (i = 0; files[i]; i++)
  342.     {
  343.       char *arg;
  344.       int s;
  345.  
  346.       arg = files[i];
  347.  
  348.       /* Delete final slash in the argument, unless the slash is alone.  */
  349.       s = strlen (arg) - 1;
  350.       if (s != 0)
  351.     {
  352.       if (arg[s] == '/')
  353.         arg[s] = 0;
  354.  
  355.       str_copyc (path, arg);
  356.     }
  357.       else if (arg[0] == '/')
  358.     str_trunc (path, 0);    /* Null path for root directory.  */
  359.       else
  360.     str_copyc (path, arg);
  361.  
  362.       if (!opt_combined_arguments)
  363.     hash_reset ();
  364.  
  365.       count_entry (arg, 1, 0);
  366.  
  367.       /* chdir if `count_entry' has changed the working directory.  */
  368.       if (SAFE_STAT (".", &stat_buf))
  369.     error (1, errno, ".");
  370.       if ((stat_buf.st_ino != initial_ino || stat_buf.st_dev != initial_dev)
  371. #ifdef HAVE_FCHDIR
  372.       && fchdir (wd_desc) < 0)
  373.     error (1, errno, "cannot return to original directory");
  374. #else
  375.       && chdir (wd) < 0)
  376.     error (1, errno, "cannot change to directory %s", wd);
  377. #endif
  378.     }
  379.  
  380.   if (opt_combined_arguments)
  381.     {
  382.       printf ("%ld\ttotal\n", output_size == size_bytes ? tot_size
  383.           : convert_blocks (tot_size, output_size == size_kilobytes));
  384.       fflush (stdout);
  385.     }
  386.  
  387. #ifndef HAVE_FCHDIR
  388.   free (wd);
  389. #endif
  390. }
  391.  
  392. /* Print (if appropriate) and return the size
  393.    (in units determined by `output_size') of file or directory ENT.
  394.    TOP is one for external calls, zero for recursive calls.
  395.    LAST_DEV is the device that the parent directory of ENT is on.  */
  396.  
  397. static long
  398. count_entry (ent, top, last_dev)
  399.      char *ent;
  400.      int top;
  401.      dev_t last_dev;
  402. {
  403.   long size;
  404.  
  405.   if (((top && opt_dereference_arguments)
  406.        ? SAFE_STAT (ent, &stat_buf)
  407.        : (*xstat) (ent, &stat_buf)) < 0)
  408.     {
  409.       error (0, errno, "%s", path->text);
  410.       exit_status = 1;
  411.       return 0;
  412.     }
  413.  
  414.   if (!opt_count_all
  415.       && stat_buf.st_nlink > 1
  416.       && hash_insert (stat_buf.st_ino, stat_buf.st_dev))
  417.     return 0;            /* Have counted this already.  */
  418.  
  419.   if (output_size == size_bytes)
  420.     size = stat_buf.st_size;
  421.   else
  422.     size = ST_NBLOCKS (stat_buf);
  423.  
  424.   tot_size += size;
  425.  
  426.   if (S_ISDIR (stat_buf.st_mode))
  427.     {
  428.       unsigned pathlen;
  429.       dev_t dir_dev;
  430.       char *name_space;
  431.       char *namep;
  432.  
  433.       dir_dev = stat_buf.st_dev;
  434.  
  435.       if (opt_one_file_system && !top && last_dev != dir_dev)
  436.     return 0;        /* Don't enter a new file system.  */
  437.  
  438.       if (chdir (ent) < 0)
  439.     {
  440.       error (0, errno, "cannot change to directory %s", path->text);
  441.       exit_status = 1;
  442.       return 0;
  443.     }
  444.  
  445.       errno = 0;
  446.       name_space = savedir (".", stat_buf.st_size);
  447.       if (name_space == NULL)
  448.     {
  449.       if (errno)
  450.         {
  451.           error (0, errno, "%s", path->text);
  452.           if (chdir ("..") < 0)    /* Try to return to previous dir.  */
  453.         error (1, errno, "cannot change to `..' from directory %s",
  454.                path->text);
  455.           exit_status = 1;
  456.           return 0;
  457.         }
  458.       else
  459.         error (1, 0, "virtual memory exhausted");
  460.     }
  461.  
  462.       /* Remember the current path.  */
  463.  
  464.       str_concatc (path, "/");
  465.       pathlen = path->length;
  466.  
  467.       namep = name_space;
  468.       while (*namep != 0)
  469.     {
  470.       str_concatc (path, namep);
  471.  
  472.       size += count_entry (namep, 0, dir_dev);
  473.  
  474.       str_trunc (path, pathlen);
  475.       namep += strlen (namep) + 1;
  476.     }
  477.       free (name_space);
  478.       if (chdir ("..") < 0)
  479.         error (1, errno, "cannot change to `..' from directory %s", path->text);
  480.  
  481.       str_trunc (path, pathlen - 1); /* Remove the "/" we added.  */
  482.       if (!opt_summarize_only || top)
  483.     {
  484.       printf ("%ld\t%s\n", output_size == size_bytes ? size
  485.           : convert_blocks (size, output_size == size_kilobytes),
  486.           path->length > 0 ? path->text : "/");
  487.       fflush (stdout);
  488.     }
  489.       return opt_separate_dirs ? 0 : size;
  490.     }
  491.   else if (opt_all || top)
  492.     {
  493.       printf ("%ld\t%s\n", output_size == size_bytes ? size
  494.           : convert_blocks (size, output_size == size_kilobytes),
  495.           path->text);
  496.       fflush (stdout);
  497.     }
  498.  
  499.   return size;
  500. }
  501.  
  502. /* Allocate space for the hash structures, and set the global
  503.    variable `htab' to point to it.  The initial hash module is specified in
  504.    MODULUS, and the number of entries are specified in ENTRY_TAB_SIZE.  (The
  505.    hash structure will be rebuilt when ENTRY_TAB_SIZE entries have been
  506.    inserted, and MODULUS and ENTRY_TAB_SIZE in the global `htab' will be
  507.    doubled.)  */
  508.  
  509. static void
  510. hash_init (modulus, entry_tab_size)
  511.      unsigned modulus;
  512.      unsigned entry_tab_size;
  513. {
  514.   struct htab *htab_r;
  515.  
  516.   htab_r = (struct htab *)
  517.     xmalloc (sizeof (struct htab) + sizeof (struct entry *) * modulus);
  518.  
  519.   htab_r->entry_tab = (struct entry *)
  520.     xmalloc (sizeof (struct entry) * entry_tab_size);
  521.  
  522.   htab_r->modulus = modulus;
  523.   htab_r->entry_tab_size = entry_tab_size;
  524.   htab = htab_r;
  525.  
  526.   hash_reset ();
  527. }
  528.  
  529. /* Reset the hash structure in the global variable `htab' to
  530.    contain no entries.  */
  531.  
  532. static void
  533. hash_reset ()
  534. {
  535.   int i;
  536.   struct entry **p;
  537.  
  538.   htab->first_free_entry = 0;
  539.  
  540.   p = htab->hash;
  541.   for (i = htab->modulus; i > 0; i--)
  542.     *p++ = NULL;
  543. }
  544.  
  545. /* Insert an item (inode INO and device DEV) in the hash
  546.    structure in the global variable `htab', if an entry with the same data
  547.    was not found already.  Return zero if the item was inserted and non-zero
  548.    if it wasn't.  */
  549.  
  550. static int
  551. hash_insert (ino, dev)
  552.      ino_t ino;
  553.      dev_t dev;
  554. {
  555.   struct htab *htab_r = htab;    /* Initially a copy of the global `htab'.  */
  556.  
  557.   if (htab_r->first_free_entry >= htab_r->entry_tab_size)
  558.     {
  559.       int i;
  560.       struct entry *ep;
  561.       unsigned modulus;
  562.       unsigned entry_tab_size;
  563.  
  564.       /* Increase the number of hash entries, and re-hash the data.
  565.      The method of shrimping and increasing is made to compactify
  566.      the heap.  If twice as much data would be allocated
  567.      straightforwardly, we would never re-use a byte of memory.  */
  568.  
  569.       /* Let `htab' shrimp.  Keep only the header, not the pointer vector.  */
  570.  
  571.       htab_r = (struct htab *)
  572.     xrealloc ((char *) htab_r, sizeof (struct htab));
  573.  
  574.       modulus = 2 * htab_r->modulus;
  575.       entry_tab_size = 2 * htab_r->entry_tab_size;
  576.  
  577.       /* Increase the number of possible entries.  */
  578.  
  579.       htab_r->entry_tab = (struct entry *)
  580.     xrealloc ((char *) htab_r->entry_tab,
  581.          sizeof (struct entry) * entry_tab_size);
  582.  
  583.       /* Increase the size of htab again.  */
  584.  
  585.       htab_r = (struct htab *)
  586.     xrealloc ((char *) htab_r,
  587.          sizeof (struct htab) + sizeof (struct entry *) * modulus);
  588.  
  589.       htab_r->modulus = modulus;
  590.       htab_r->entry_tab_size = entry_tab_size;
  591.       htab = htab_r;
  592.  
  593.       i = htab_r->first_free_entry;
  594.  
  595.       /* Make the increased hash table empty.  The entries are still
  596.      available in htab->entry_tab.  */
  597.  
  598.       hash_reset ();
  599.  
  600.       /* Go through the entries and install them in the pointer vector
  601.      htab->hash.  The items are actually inserted in htab->entry_tab at
  602.      the position where they already are.  The htab->coll_link need
  603.      however be updated.  Could be made a little more efficient.  */
  604.  
  605.       for (ep = htab_r->entry_tab; i > 0; i--)
  606.     {
  607.       hash_insert2 (htab_r, ep->ino, ep->dev);
  608.       ep++;
  609.     }
  610.     }
  611.  
  612.   return hash_insert2 (htab_r, ino, dev);
  613. }
  614.  
  615. /* Insert INO and DEV in the hash structure HTAB, if not
  616.    already present.  Return zero if inserted and non-zero if it
  617.    already existed.  */
  618.  
  619. static int
  620. hash_insert2 (htab, ino, dev)
  621.      struct htab *htab;
  622.      ino_t ino;
  623.      dev_t dev;
  624. {
  625.   struct entry **hp, *ep2, *ep;
  626.   hp = &htab->hash[ino % htab->modulus];
  627.   ep2 = *hp;
  628.  
  629.   /* Collision?  */
  630.  
  631.   if (ep2 != NULL)
  632.     {
  633.       ep = ep2;
  634.  
  635.       /* Search for an entry with the same data.  */
  636.  
  637.       do
  638.     {
  639.       if (ep->ino == ino && ep->dev == dev)
  640.         return 1;        /* Found an entry with the same data.  */
  641.       ep = ep->coll_link;
  642.     }
  643.       while (ep != NULL);
  644.  
  645.       /* Did not find it.  */
  646.  
  647.     }
  648.  
  649.   ep = *hp = &htab->entry_tab[htab->first_free_entry++];
  650.   ep->ino = ino;
  651.   ep->dev = dev;
  652.   ep->coll_link = ep2;        /* `ep2' is NULL if no collision.  */
  653.  
  654.   return 0;
  655. }
  656.  
  657. /* Initialize the struct string S1 for holding SIZE characters.  */
  658.  
  659. static void
  660. str_init (s1, size)
  661.      string *s1;
  662.      unsigned size;
  663. {
  664.   string s;
  665.  
  666.   s = (string) xmalloc (sizeof (stringstruct));
  667.   s->text = xmalloc (size + 1);
  668.  
  669.   s->alloc = size;
  670.   *s1 = s;
  671. }
  672.  
  673. static void
  674. ensure_space (s, size)
  675.      string s;
  676.      unsigned size;
  677. {
  678.   if (s->alloc < size)
  679.     {
  680.       s->text = xrealloc (s->text, size + 1);
  681.       s->alloc = size;
  682.     }
  683. }
  684.  
  685. /* Assign the null-terminated C-string CSTR to S1.  */
  686.  
  687. static void
  688. str_copyc (s1, cstr)
  689.      string s1;
  690.      char *cstr;
  691. {
  692.   unsigned l = strlen (cstr);
  693.   ensure_space (s1, l);
  694.   strcpy (s1->text, cstr);
  695.   s1->length = l;
  696. }
  697.  
  698. static void
  699. str_concatc (s1, cstr)
  700.      string s1;
  701.      char *cstr;
  702. {
  703.   unsigned l1 = s1->length;
  704.   unsigned l2 = strlen (cstr);
  705.   unsigned l = l1 + l2;
  706.  
  707.   ensure_space (s1, l);
  708.   strcpy (s1->text + l1, cstr);
  709.   s1->length = l;
  710. }
  711.  
  712. /* Truncate the string S1 to have length LENGTH.  */
  713.  
  714. static void
  715. str_trunc (s1, length)
  716.      string s1;
  717.      unsigned length;
  718. {
  719.   if (s1->length > length)
  720.     {
  721.       s1->text[length] = 0;
  722.       s1->length = length;
  723.     }
  724. }
  725.