home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / TAC.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  19KB  |  704 lines

  1. /* tac - concatenate and print files in reverse
  2.    Copyright (C) 1988, 1989, 1990 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. /* Written by Jay Lepreau (lepreau@cs.utah.edu).
  19.    GNU enhancements by David MacKenzie (djm@ai.mit.edu). */
  20.  
  21. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  22.    This port is also distributed under the terms of the
  23.    GNU General Public License as published by the
  24.    Free Software Foundation.
  25.  
  26.    Please note that this file is not identical to the
  27.    original GNU release, you should have received this
  28.    code as patch to the official release.  */
  29.  
  30. #ifdef MSDOS
  31. static char RCS_Id[] =
  32.   "$Header: e:/gnu/fileutil/RCS/tac.c 1.4.0.3 90/09/19 12:09:16 tho Exp $";
  33.  
  34. static char Program_Id[] = "tac";
  35. static char RCS_Revision[] = "$Revision: 1.4.0.3 $";
  36.  
  37. #define VERSION \
  38.   "GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
  39.   (sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
  40.  
  41. #define COPYING \
  42.   "This is free software, distributed under the terms of the\n" \
  43.   "GNU General Public License.  For details, see the file COPYING.\n"
  44. #endif /* MSDOS */
  45.  
  46. /* Usage: tac [-br] [-s separator] [+before] [+regex] [+separator separator]
  47.           [file...]
  48.  
  49.    Copy each FILE, or the standard input if none are given or when a
  50.    FILE name of "-" is encountered, to the standard output with the
  51.    order of the records reversed.  The records are separated by
  52.    instances of a string, or a newline if none is given.  By default, the
  53.    separator string is attached to the end of the record that it
  54.    follows in the file.
  55.  
  56.    Options:
  57.    -b, +before            The separator is attached to the beginning
  58.                 of the record that it precedes in the file.
  59.    -r, +regex            The separator is a regular expression.
  60.    -s, +separator separator    Use SEPARATOR as the record separator.
  61.  
  62.    To reverse a file byte by byte, use (in bash, ksh, or sh):
  63. tac -r -s '.\|
  64. ' file */
  65.  
  66. #include <stdio.h>
  67. #include <getopt.h>
  68. #include <sys/types.h>
  69. #include <signal.h>
  70. #include "system.h"
  71. #include "regex.h"
  72.  
  73. #ifdef STDC_HEADERS
  74. #include <stdlib.h>
  75. #include <errno.h>
  76. #else
  77. char *malloc ();
  78. char *realloc ();
  79.  
  80. extern int errno;
  81. #endif
  82.  
  83. /* The number of bytes per atomic read. */
  84. #define INITIAL_READSIZE 8192
  85.  
  86. /* The number of bytes per atomic write. */
  87. #define WRITESIZE 8192
  88.  
  89. char *mktemp ();
  90.  
  91. #ifndef _POSIX_SOURCE
  92. off_t lseek ();
  93. #endif
  94.  
  95. #ifdef MSDOS
  96.  
  97. #include <gnulib.h>
  98. #include <io.h>
  99.  
  100. /* We need a static malloc (with cleanup) here, so mask off
  101.    the one from gnulib   */
  102. #define xmalloc _xmalloc
  103. #define xrealloc _xrealloc
  104.  
  105. static char *_xmalloc (unsigned int n);
  106. static char *_xrealloc (char *p, unsigned int n);
  107.  
  108. extern void main (int argc, char **argv);
  109. static int tac (int fd, char *file);
  110. static int tac_file (char *file);
  111. static int tac_stdin (void );
  112. static void output (char *start, char *past_end);
  113. static void save_stdin (void );
  114. static void xwrite (int desc, char *buffer, int size);
  115. static SIGTYPE cleanup (void );
  116.  
  117. #else /* not MSDOS */
  118.  
  119. SIGTYPE cleanup ();
  120. int tac ();
  121. int tac_file ();
  122. int tac_stdin ();
  123. char *xmalloc ();
  124. char *xrealloc ();
  125. void output ();
  126. void error ();
  127. void save_stdin ();
  128. void xwrite ();
  129. #endif /* not MSDOS */
  130.  
  131. /* The name this program was run with. */
  132. char *program_name;
  133.  
  134. /* The string that separates the records of the file. */
  135. char *separator;
  136.  
  137. /* If nonzero, print `separator' along with the record preceding it
  138.    in the file; otherwise with the record following it. */
  139. int separator_ends_record;
  140.  
  141. /* 0 if `separator' is to be matched as a regular expression;
  142.    otherwise, the length of `separator', used as a sentinel to
  143.    stop the search. */
  144. int sentinel_length;
  145.  
  146. /* The length of a match with `separator'.  If `sentinel_length' is 0,
  147.    `match_length' is computed every time a match succeeds;
  148.    otherwise, it is simply the length of `separator'. */
  149. int match_length;
  150.  
  151. /* The input buffer. */
  152. char *buffer;
  153.  
  154. /* The number of bytes to read at once into `buffer'. */
  155. unsigned read_size;
  156.  
  157. /* The size of `buffer'.  This is read_size * 2 + sentinel_length + 2.
  158.    The extra 2 bytes allow `past_end' to have a value beyond the
  159.    end of `buffer' and `match_start' to run off the front of `buffer'. */
  160. unsigned buffer_size;
  161.  
  162. /* The compiled regular expression representing `separator'. */
  163. static struct re_pattern_buffer compiled_separator;
  164.  
  165. struct option longopts[] =
  166. {
  167. #ifdef MSDOS
  168.   {"copying", 0, NULL, 30},
  169.   {"version", 0, NULL, 31},
  170. #endif
  171.   {"before", 0, &separator_ends_record, 0},
  172.   {"regex", 0, &sentinel_length, 0},
  173.   {"separator", 1, NULL, 's'},
  174.   {NULL, 0, NULL, 0}
  175. };
  176.  
  177. void
  178. main (argc, argv)
  179.      int argc;
  180.      char **argv;
  181. {
  182.   char *error_message;        /* Return value from re_compile_pattern. */
  183.   int optc, longind, errors;
  184.  
  185.   program_name = argv[0];
  186. #ifdef MSDOS
  187.   setmode (0, O_BINARY);
  188.   setmode (1, O_BINARY);
  189. #endif /* MSDOS */
  190.  
  191.   errors = 0;
  192.   separator = "\n";
  193.   sentinel_length = 1;
  194.   separator_ends_record = 1;
  195.  
  196.   while ((optc = getopt_long (argc, argv, "brs:", longopts, &longind))
  197.      != EOF)
  198.     {
  199.       switch (optc)
  200.     {
  201.     case 0:
  202.       break;
  203.     case 'b':
  204.       separator_ends_record = 0;
  205.       break;
  206.     case 'r':
  207.       sentinel_length = 0;
  208.       break;
  209.     case 's':
  210.       separator = optarg;
  211.       if (*separator == 0)
  212.         error (1, 0, "separator cannot be empty");
  213.       break;
  214. #ifdef MSDOS
  215.     case 30:
  216.       fprintf (stderr, COPYING);
  217.       exit (0);
  218.       break;
  219.     case 31:
  220.       fprintf (stderr, VERSION);
  221.       exit (0);
  222.       break;
  223. #endif
  224.     default:
  225. #ifdef MSDOS
  226.       fprintf (stderr, "\
  227. Usage: %s [-br] [-s separator] [+before] [+regex] [+separator separator]\n\
  228.        [+copying] [+version] [file...]\n",
  229. #else /* not MSDOS */
  230.       fprintf (stderr, "\
  231. Usage: %s [-br] [-s separator] [+before] [+regex] [+separator separator]\n\
  232.        [file...]\n",
  233. #endif /* not MSDOS */
  234.            program_name);
  235.       exit (1);
  236.     }
  237.     }
  238.  
  239.   if (sentinel_length == 0)
  240.     {
  241.       compiled_separator.allocated = 100;
  242. #ifdef MSDOS
  243.       compiled_separator.buffer
  244.     = xmalloc ((size_t) compiled_separator.allocated);
  245. #else /* not MSDOS */
  246.       compiled_separator.buffer = xmalloc (compiled_separator.allocated);
  247. #endif /* not MSDOS */
  248.       compiled_separator.fastmap = xmalloc (256);
  249.       compiled_separator.translate = 0;
  250.       error_message = re_compile_pattern (separator, strlen (separator),
  251.                       &compiled_separator);
  252.       if (error_message)
  253.     error (1, 0, "%s", error_message);
  254.     }
  255.   else
  256.     match_length = sentinel_length = strlen (separator);
  257.  
  258.   read_size = INITIAL_READSIZE;
  259.   /* A precaution that will probably never be needed. */
  260.   while (sentinel_length * 2 >= read_size)
  261.     read_size *= 2;
  262.   buffer_size = read_size * 2 + sentinel_length + 2;
  263.   buffer = xmalloc (buffer_size);
  264.   if (sentinel_length)
  265.     {
  266.       strcpy (buffer, separator);
  267.       buffer += sentinel_length;
  268.     }
  269.   else
  270.     ++buffer;
  271.  
  272.   if (optind == argc)
  273.     errors = tac_stdin ();
  274.   else
  275.     for (; optind < argc; ++optind)
  276.       {
  277.     if (strcmp (argv[optind], "-") == 0)
  278.       errors |= tac_stdin ();
  279.     else
  280.       errors |= tac_file (argv[optind]);
  281.       }
  282.  
  283.   /* Flush the output buffer. */
  284.   output ((char *) NULL, (char *) NULL);
  285.   exit (errors);
  286. }
  287.  
  288. /* The name of a temporary file containing a copy of pipe input. */
  289. char *tempfile;
  290.  
  291. /* Print the standard input in reverse, saving it to temporary
  292.    file `tempfile' first if it is a pipe.
  293.    Return 0 if ok, 1 if an error occurs. */
  294.  
  295. int
  296. tac_stdin ()
  297. {
  298.   /* Previous values of signal handlers. */
  299.   SIGTYPE (*sigint) (), (*sighup) (), (*sigterm) ();
  300.   int errors;
  301.   struct stat stats;
  302.  
  303.   /* No tempfile is needed for "tac < file".
  304.      Use fstat instead of checking for errno == ESPIPE because
  305.      lseek doesn't work on some special files but doesn't return an
  306.      error, either. */
  307.   if (fstat (0, &stats))
  308.     {
  309.       error (0, errno, "standard input");
  310.       return 1;
  311.     }
  312.   if ((stats.st_mode & S_IFMT) == S_IFREG)
  313.     return tac (0, "standard input");
  314.  
  315.   sigint = signal (SIGINT, SIG_IGN);
  316.   if (sigint != SIG_IGN)
  317.     signal (SIGINT, cleanup);
  318. #ifdef SIGHUP
  319.   sighup = signal (SIGHUP, SIG_IGN);
  320.   if (sighup != SIG_IGN)
  321.     signal (SIGHUP, cleanup);
  322. #endif
  323.   sigterm = signal (SIGTERM, SIG_IGN);
  324.   if (sigterm != SIG_IGN)
  325.     signal (SIGTERM, cleanup);
  326.   save_stdin ();
  327.  
  328.   errors = tac_file (tempfile);
  329.  
  330.   unlink (tempfile);
  331.   signal (SIGINT, sigint);
  332. #ifdef SIGHUP
  333.   signal (SIGHUP, sighup);
  334. #endif
  335.   signal (SIGTERM, sigterm);
  336.   return errors;
  337. }
  338.  
  339. #ifdef MSDOS
  340. char template[] = "/tacXXXXXX";
  341. char *workplate;
  342. #else /* not MSDOS */
  343. char template[] = "/tmp/tacXXXXXX";
  344. char workplate[sizeof template];
  345. #endif /* not MSDOS */
  346.  
  347. /* Make a copy of the standard input in `tempfile'. */
  348.  
  349. void
  350. save_stdin ()
  351. {
  352.   int fd;
  353.   int bytes_read;
  354.  
  355. #ifdef MSDOS
  356.   char *p;
  357.  
  358.   if ((p = getenv ("TMP")) || (p = getenv ("TEMP")))
  359.     {
  360.       int len = strlen (p);
  361.       workplate = (char *) xmalloc (sizeof (template) + len + 1);
  362.       strcpy (workplate, p);
  363.       p = workplate + len - 1;
  364.       if (*p == '/' || *p == '\\')    /*  strip trailing slash */
  365.     *p = '\0';
  366.     }
  367.   else
  368.     {
  369.       workplate = (char *) xmalloc (sizeof (template) + 2);
  370.       strcpy (workplate, ".");
  371.     }
  372.   strcat (workplate, template);
  373. #else /* not MSDOS */
  374.   strcpy (workplate, template);
  375. #endif /* not MSDOS */
  376.   tempfile = mktemp (workplate);
  377.  
  378. #ifdef MSDOS
  379.   fd = open (tempfile, O_CREAT|O_TRUNC|O_RDWR|O_BINARY, S_IWRITE|S_IREAD);
  380. #else /* not MSDOS */
  381.   fd = creat (tempfile, 0600);
  382. #endif /* not MSDOS */
  383.   if (fd == -1)
  384.     {
  385.       error (0, errno, "%s", tempfile);
  386.       cleanup ();
  387.     }
  388.  
  389.   while ((bytes_read = read (0, buffer, read_size)) > 0)
  390.     if (write (fd, buffer, bytes_read) != bytes_read)
  391.       {
  392.     error (0, errno, "%s", tempfile);
  393.     cleanup ();
  394.       }
  395.   close (fd);
  396.   if (bytes_read == -1)
  397.     {
  398.       error (0, errno, "read error");
  399.       cleanup ();
  400.     }
  401. }
  402.  
  403. /* Print FILE in reverse.
  404.    Return 0 if ok, 1 if an error occurs. */
  405.  
  406. int
  407. tac_file (file)
  408.      char *file;
  409. {
  410.   int fd, errors;
  411.  
  412. #ifdef MSDOS
  413.   fd = open (file, O_RDONLY|O_BINARY);
  414. #else /* not MSDOS */
  415.   fd = open (file, 0);
  416. #endif /* not MSDOS */
  417.   if (fd == -1)
  418.     {
  419.       error (0, errno, "%s", file);
  420.       return 1;
  421.     }
  422.   errors = tac (fd, file);
  423.   close (fd);
  424.   return errors;
  425. }
  426.  
  427. /* Print in reverse the file open on descriptor FD for reading FILE.
  428.    Return 0 if ok, 1 if an error occurs. */
  429.  
  430. int
  431. tac (fd, file)
  432.      int fd;
  433.      char *file;
  434. {
  435.   /* Pointer to the location in `buffer' where the search for
  436.      the next separator will begin. */
  437.   char *match_start;
  438.   /* Pointer to one past the rightmost character in `buffer' that
  439.      has not been printed yet. */
  440.   char *past_end;
  441.   unsigned saved_record_size;    /* Length of the record growing in `buffer'. */
  442.   off_t file_pos;        /* Offset in the file of the next read. */
  443.   /* Nonzero if `output' has not been called yet for any file.
  444.      Only used when the separator is attached to the preceding record. */
  445.   int first_time = 1;
  446.   char first_char = *separator;    /* Speed optimization, non-regexp. */
  447.   char *separator1 = separator + 1; /* Speed optimization, non-regexp. */
  448.   int match_length1 = match_length - 1; /* Speed optimization, non-regexp. */
  449.   struct re_registers regs;
  450.   int errors = 0;
  451.  
  452.   /* Find the size of the input file. */
  453.   file_pos = lseek (fd, (off_t) 0, L_XTND);
  454.   if (file_pos < 1)
  455.     return 0;            /* It's an empty file. */
  456.  
  457.   /* Arrange for the first read to lop off enough to leave the rest of the
  458.      file a multiple of `read_size'.  Since `read_size' can change, this may
  459.      not always hold during the program run, but since it usually will, leave
  460.      it here for i/o efficiency (page/sector boundaries and all that).
  461.      Note: the efficiency gain has not been verified. */
  462. #ifdef MSDOS
  463.   saved_record_size = (unsigned int) (file_pos % read_size);
  464. #else /* not MSDOS */
  465.   saved_record_size = file_pos % read_size;
  466. #endif /* not MSDOS */
  467.   if (saved_record_size == 0)
  468.     saved_record_size = read_size;
  469.   file_pos -= saved_record_size;
  470.   /* `file_pos' now points to the start of the last (probably partial) block
  471.      in the input file. */
  472.  
  473.   lseek (fd, file_pos, L_SET);
  474.   if (read (fd, buffer, saved_record_size) != saved_record_size)
  475.     {
  476.       error (0, 1, "%s", file);
  477.       return 1;
  478.     }
  479.  
  480. #ifdef MSDOS
  481.   /* Some loosing editors add one or more ^Z to the end of a text file and
  482.      loosing MS-DOS devices interpret this as End Of File.  We turn them
  483.      into spaces and print a warning message.  */
  484.     {
  485.       char *ctrl_z = buffer + saved_record_size - 1;
  486.       if (*ctrl_z == '\032')
  487.     {
  488.       *ctrl_z = ' ';
  489.       error (0, 0, "%s: trailing ^Z translated to space.", file);
  490.       while (*--ctrl_z == '\032')
  491.         *ctrl_z = ' ';
  492.     }
  493.     }
  494. #endif /* MSDOS */
  495.  
  496.   match_start = past_end = buffer + saved_record_size;
  497.   /* For non-regexp search, move past impossible positions for a match. */
  498.   if (sentinel_length)
  499.     match_start -= match_length1;
  500.  
  501.   for (;;)
  502.     {
  503.       /* Search backward from `match_start' - 1 to `buffer' for a match
  504.      with `separator'; for speed, use strncmp if `separator' contains no
  505.      metacharacters.
  506.      If the match succeeds, set `match_start' to point to the start of
  507.      the match and `match_length' to the length of the match.
  508.      Otherwise, make `match_start' < `buffer'. */
  509.       if (sentinel_length == 0)
  510.     {
  511.       int i = match_start - buffer;
  512.       int ret;
  513.  
  514.       ret = re_search (&compiled_separator, buffer, i, i - 1, -i, ®s);
  515.       if (ret == -1)
  516.         match_start = buffer - 1;
  517.       else if (ret == -2)
  518.         {
  519.           error (0, 0, "error in regular expression search");
  520.           cleanup ();
  521.         }
  522.       else
  523.         {
  524.           match_start = buffer + regs.start[0];
  525.           match_length = regs.end[0] - regs.start[0];
  526.         }
  527.     }
  528.       else
  529.     {
  530.       /* `match_length' is constant for non-regexp boundaries. */
  531.       while (*--match_start != first_char
  532.          || (match_length1 && strncmp (match_start + 1, separator1,
  533.                            match_length1)))
  534.         /* Do nothing. */ ;
  535.     }
  536.  
  537.       /* Check whether we backed off the front of `buffer' without finding
  538.          a match for `separator'. */
  539.       if (match_start < buffer)
  540.     {
  541.       if (file_pos == 0)
  542.         {
  543.           /* Hit the beginning of the file; print the remaining record. */
  544.           output (buffer, past_end);
  545.           return errors;
  546.         }
  547.  
  548.       saved_record_size = past_end - buffer;
  549.       if (saved_record_size > read_size)
  550.         {
  551.           /* `buffer_size' is about twice `read_size', so since
  552.          we want to read in another `read_size' bytes before
  553.          the data already in `buffer', we need to increase
  554.          `buffer_size'. */
  555.           char *newbuffer;
  556.           int offset = sentinel_length ? sentinel_length : 1;
  557.  
  558.           read_size *= 2;
  559.           buffer_size = read_size * 2 + sentinel_length + 2;
  560.           newbuffer = xrealloc (buffer - offset, buffer_size) + offset;
  561.           /* Adjust the pointers for the new buffer location.  */
  562.           match_start += newbuffer - buffer;
  563.           past_end += newbuffer - buffer;
  564.           buffer = newbuffer;
  565.         }
  566.  
  567.       /* Back up to the start of the next bufferfull of the file.  */
  568.       if (file_pos >= read_size)
  569.         file_pos -= read_size;
  570.       else
  571.         {
  572.           read_size = (unsigned) file_pos;
  573.           file_pos = 0;
  574.         }
  575.       lseek (fd, file_pos, L_SET);
  576.  
  577.       /* Shift the pending record data right to make room for the new. */
  578.       bcopy (buffer, buffer + read_size, saved_record_size);
  579.       past_end = buffer + read_size + saved_record_size;
  580.       /* For non-regexp searches, avoid unneccessary scanning. */
  581.       if (sentinel_length)
  582.         match_start = buffer + read_size;
  583.       else
  584.         match_start = past_end;
  585.  
  586.       if (read (fd, buffer, read_size) != read_size)
  587.         {
  588.           error (0, errno, "%s", file);
  589.           return 1;
  590.         }
  591.     }
  592.       else
  593.     {
  594.       /* Found a match of `separator'. */
  595.       if (separator_ends_record)
  596.         {
  597.           char *match_end = match_start + match_length;
  598.  
  599.           /* If this match of `separator' isn't at the end of the
  600.              file, print the record. */
  601.           if (first_time == 0 || match_end != past_end)
  602.         output (match_end, past_end);
  603.           past_end = match_end;
  604.           first_time = 0;
  605.         }
  606.       else
  607.         {
  608.           output (match_start, past_end);
  609.           past_end = match_start;
  610.         }
  611.       match_start -= match_length - 1;
  612.     }
  613.     }
  614. }
  615.  
  616. /* Print the characters from START to PAST_END - 1.
  617.    If START is NULL, just flush the buffer. */
  618.  
  619. void
  620. output (start, past_end)
  621.      char *start;
  622.      char *past_end;
  623. {
  624.   static char buffer[WRITESIZE];
  625.   static int bytes_in_buffer = 0;
  626.   int bytes_to_add = past_end - start;
  627.   int bytes_available = WRITESIZE - bytes_in_buffer;
  628.  
  629.   if (start == 0)
  630.     {
  631.       xwrite (1, buffer, bytes_in_buffer);
  632.       bytes_in_buffer = 0;
  633.       return;
  634.     }
  635.   
  636.   /* Write out as many full buffers as possible. */
  637.   while (bytes_to_add >= bytes_available)
  638.     {
  639.       bcopy (start, buffer + bytes_in_buffer, bytes_available);
  640.       bytes_to_add -= bytes_available;
  641.       start += bytes_available;
  642.       xwrite (1, buffer, WRITESIZE);
  643.       bytes_in_buffer = 0;
  644.       bytes_available = WRITESIZE;
  645.     }
  646.  
  647.   bcopy (start, buffer + bytes_in_buffer, bytes_to_add);
  648.   bytes_in_buffer += bytes_to_add;
  649. }
  650.  
  651. SIGTYPE
  652. cleanup ()
  653. {
  654.   unlink (tempfile);
  655.   exit (1);
  656. }
  657.  
  658. void
  659. xwrite (desc, buffer, size)
  660.      int desc;
  661.      char *buffer;
  662.      int size;
  663. {
  664.   if (write (desc, buffer, size) != size)
  665.     {
  666.       error (0, errno, "write error");
  667.       cleanup ();
  668.     }
  669. }
  670.  
  671.  
  672. /* Allocate N bytes of memory dynamically, with error checking.  */
  673.  
  674. char *
  675. xmalloc (n)
  676.      unsigned n;
  677. {
  678.   char *p;
  679.  
  680.   p = malloc (n);
  681.   if (p == 0)
  682.     {
  683.       error (0, 0, "virtual memory exhausted");
  684.       cleanup ();
  685.     }
  686.   return p;
  687. }
  688.  
  689. /* Change the size of memory area P to N bytes, with error checking. */
  690.  
  691. char *
  692. xrealloc (p, n)
  693.      char *p;
  694.      unsigned n;
  695. {
  696.   p = realloc (p, n);
  697.   if (p == 0)
  698.     {
  699.       error (0, 0, "virtual memory exhausted");
  700.       cleanup ();
  701.     }
  702.   return p;
  703. }
  704.