home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / dif115as.zip / UTIL.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  20KB  |  840 lines

  1. /* Support routines for GNU DIFF.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21. This port is also distributed under the terms of the GNU General
  22. Public License as published by the Free Software Foundation.
  23.  
  24. Please note that this file is not identical to the original GNU release,
  25. you should have received this code as patch to the official release.
  26.  
  27. $Header: e:/gnu/diff/RCS/util.c 1.15.0.2 91/03/12 17:06:46 tho Exp $  */
  28.  
  29. #include "diff.h"
  30. #include "regex.h"            /* was missing [tho] */
  31.  
  32. #ifdef MSDOS
  33. #include <conio.h>
  34. static char *_pipe_file (void);
  35. static void cleanup_pipe (void);
  36. #endif /* MSDOS */
  37.  
  38. /* Use when a system call returns non-zero status.
  39.    TEXT should normally be the file name.  */
  40.  
  41. void
  42. perror_with_name (text)
  43.      char *text;
  44. {
  45.   fprintf (stderr, "%s: ", program);
  46.   perror (text);
  47. }
  48.  
  49. /* Use when a system call returns non-zero status and that is fatal.  */
  50.  
  51. void
  52. pfatal_with_name (text)
  53.      char *text;
  54. {
  55.   print_message_queue ();
  56.   fprintf (stderr, "%s: ", program);
  57.   perror (text);
  58.   exit (2);
  59. }
  60.  
  61. /* Print an error message from the format-string FORMAT
  62.    with args ARG1 and ARG2.  */
  63.  
  64. void
  65. error (format, arg, arg1)
  66.      char *format;
  67.      char *arg;
  68.      char *arg1;
  69. {
  70.   fprintf (stderr, "%s: ", program);
  71.   fprintf (stderr, format, arg, arg1);
  72.   fprintf (stderr, "\n");
  73. }
  74.  
  75. /* Print an error message containing the string TEXT, then exit.  */
  76.  
  77. void
  78. fatal (message)
  79.      char *message;
  80. {
  81.   print_message_queue ();
  82.   error (message, "");
  83.   exit (2);
  84. }
  85.  
  86. #ifdef MSDOS
  87. void
  88. fatal_with_name (char *message,char *name)
  89. {
  90.   print_message_queue ();
  91.   error ("%s: %s", name, message);
  92.   exit (2);
  93. }
  94. #endif
  95.  
  96. /* Like printf, except if -l in effect then save the message and print later.
  97.    This is used for things like "binary files differ" and "Only in ...".  */
  98.  
  99. void
  100. message (format, arg1, arg2)
  101.      char *format, *arg1, *arg2;
  102. {
  103.   if (paginate_flag)
  104.     {
  105.       struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));
  106.       if (msg_chain_end == 0)
  107.     msg_chain = msg_chain_end = new;
  108.       else
  109.     {
  110.       msg_chain_end->next = new;
  111.       msg_chain_end = new;
  112.     }
  113.       new->format = format;
  114.       new->arg1 = concat (arg1, "", "");
  115.       new->arg2 = concat (arg2, "", "");
  116.       new->next = 0;
  117.     }
  118.   else
  119.     printf (format, arg1, arg2);
  120. }
  121.  
  122. /* Output all the messages that were saved up by calls to `message'.  */
  123.  
  124. void
  125. print_message_queue ()
  126. {
  127.   struct msg *m;
  128.  
  129.   for (m = msg_chain; m; m = m->next)
  130.     printf (m->format, m->arg1, m->arg2);
  131. }
  132.  
  133. /* Call before outputting the results of comparing files NAME0 and NAME1
  134.    to set up OUTFILE, the stdio stream for the output to go to.
  135.  
  136.    Usually, OUTFILE is just stdout.  But when -l was specified
  137.    we fork off a `pr' and make OUTFILE a pipe to it.
  138.    `pr' then outputs to our stdout.  */
  139.  
  140. void
  141. setup_output (name0, name1, depth)
  142.      char *name0, *name1;
  143.      int depth;
  144. {
  145.   char *name;
  146.  
  147.   /* Construct the header of this piece of diff.  */
  148.   name = (char *) xmalloc (strlen (name0) + strlen (name1)
  149.                + strlen (switch_string) + 15);
  150.  
  151.   strcpy (name, "diff");
  152.   strcat (name, switch_string);
  153.   strcat (name, " ");
  154.   strcat (name, name0);
  155.   strcat (name, " ");
  156.   strcat (name, name1);
  157.  
  158.   if (paginate_flag)
  159.     {
  160.  
  161. #ifdef MSDOS
  162.  
  163.       if ( !(outfile = fopen(_pipe_file (), "w")) )
  164.         {
  165.       error ("can't pipe to more, using stdout instead.");
  166.       outfile = stdout;
  167.         }
  168.  
  169. #else
  170.  
  171.       int pipes[2];
  172.       int desc;
  173.  
  174.       /* For a `pr' and make OUTFILE a pipe to it.  */
  175.       if (pipe (pipes) < 0)
  176.     pfatal_with_name ("pipe");
  177.  
  178.       fflush (stdout);
  179.  
  180.       desc = vfork ();
  181.       if (desc < 0)
  182.     pfatal_with_name ("vfork");
  183.  
  184.       if (desc == 0)
  185.     {
  186.       close (pipes[1]);
  187.       if (pipes[0] != fileno (stdin))
  188.         {
  189.           if (dup2 (pipes[0], fileno (stdin)) < 0)
  190.         pfatal_with_name ("dup2");
  191.           close (pipes[0]);
  192.         }
  193.  
  194.       if (execl (PR_FILE_NAME, PR_FILE_NAME, "-f", "-h", name, 0) < 0)
  195.         pfatal_with_name (PR_FILE_NAME);
  196.     }
  197.       else
  198.     {
  199.       close (pipes[0]);
  200.       outfile = fdopen (pipes[1], "w");
  201.     } 
  202.  
  203. #endif /* MSDOS */
  204.  
  205.     }
  206.   else
  207.     {
  208.  
  209.       /* If -l was not specified, output the diff straight to `stdout'.  */
  210.  
  211.       outfile = stdout;
  212.  
  213.       /* If handling multiple files (because scanning a directory),
  214.      print which files the following output is about.  */
  215.       if (depth > 0)
  216.     printf ("%s\n", name);
  217.     }
  218.  
  219.   free (name);
  220. }
  221.  
  222. /* Call after the end of output of diffs for one file.
  223.    Close OUTFILE and get rid of the `pr' subfork.  */
  224.  
  225. void
  226. finish_output ()
  227. {
  228.   if (outfile != stdout)
  229.     {
  230. #ifdef MSDOS
  231.  
  232. #ifndef PAGER
  233. #define PAGER "more"
  234. #endif
  235. #ifndef PAGER_ARGS
  236. #define PAGER_ARGS NULL
  237. #endif
  238.  
  239.       int diff_stdin;
  240.       int pager_stdin;
  241.  
  242.       fclose (outfile);
  243.  
  244.       diff_stdin = dup (0);
  245.       pager_stdin = open (_pipe_file (), O_RDONLY|O_TEXT);
  246.       dup2 (pager_stdin, 0);
  247.  
  248.       spawnlp (P_WAIT, PAGER, PAGER, PAGER_ARGS, NULL);
  249.  
  250.       dup2 (diff_stdin, 0);
  251.       close (diff_stdin);
  252.       close (pager_stdin);
  253.  
  254.       printf ("hit any key to continue...");
  255.       while (!kbhit ())
  256.     ;
  257.  
  258.       printf ("\n");
  259.  
  260. #else /* not MSDOS */
  261.  
  262.       fclose (outfile);
  263.       wait (0);
  264.  
  265. #endif /* not MSDOS */
  266.     }
  267. }
  268.  
  269. /* Compare two lines (typically one from each input file)
  270.    according to the command line options.
  271.    Each line is described by a `struct line_def'.
  272.    Return 1 if the lines differ, like `bcmp'.  */
  273.  
  274. int
  275. line_cmp (s1, s2)
  276.      struct line_def *s1, *s2;
  277. {
  278.   register char _huge *t1, _huge *t2;
  279.   register char end_char = line_end_char;
  280. #ifdef MSDOS
  281.   char savechar;
  282. #else /* not MSDOS */
  283.   int savechar;
  284. #endif /* not MSDOS */
  285.  
  286.   /* Check first for exact identity.
  287.      If that is true, return 0 immediately.
  288.      This detects the common case of exact identity
  289.      faster than complete comparison would.  */
  290.  
  291.   t1 = s1->text;
  292.   t2 = s2->text;
  293.  
  294.   /* Alter the character following line 2 so it doesn't
  295.      match that following line 1.
  296.      (We used to alter the character after line 1,
  297.      but that caused trouble if line 2 directly follows line 1.)  */
  298.   savechar = s2->text[s2->length];
  299.   s2->text[s2->length] = s1->text[s1->length] + 1;
  300.  
  301.   /* Now find the first mismatch; this won't go past the
  302.      character we just changed.  */
  303.   while (*t1++ == *t2++);
  304.  
  305.   /* Undo the alteration.  */
  306.   s2->text[s2->length] = savechar;
  307.  
  308.   /* If the comparison stopped at the alteration,
  309.      the two lines are identical.  */
  310.   if (t2 == s2->text + s2->length + 1)
  311.     return 0;
  312.  
  313.   /* Not exactly identical, but perhaps they match anyway
  314.      when case or whitespace is ignored.  */
  315.  
  316.   if (ignore_case_flag || ignore_space_change_flag || ignore_all_space_flag)
  317.     {
  318.       t1 = s1->text;
  319.       t2 = s2->text;
  320.  
  321.       while (1)
  322.     {
  323.       register char c1 = *t1++;
  324.       register char c2 = *t2++;
  325.  
  326.       /* Ignore horizontal whitespace if -b or -w is specified.  */
  327.  
  328.       if (ignore_all_space_flag)
  329.         {
  330.           /* For -w, just skip past any spaces or tabs.  */
  331.           while (c1 == ' ' || c1 == '\t') c1 = *t1++;
  332.           while (c2 == ' ' || c2 == '\t') c2 = *t2++;
  333.         }
  334.       else if (ignore_space_change_flag)
  335.         {
  336.           /* For -b, advance past any sequence of whitespace in line 1
  337.          and consider it just one Space, or nothing at all
  338.          if it is at the end of the line.  */
  339.           if (c1 == ' ' || c1 == '\t')
  340.         {
  341.           while (1)
  342.             {
  343.               c1 = *t1++;
  344.               if (c1 == end_char)
  345.             break;
  346.               if (c1 != ' ' && c1 != '\t')
  347.             {
  348.               --t1;
  349.               c1 = ' ';
  350.               break;
  351.             }
  352.             }
  353.         }
  354.  
  355.           /* Likewise for line 2.  */
  356.           if (c2 == ' ' || c2 == '\t')
  357.         {
  358.           while (1)
  359.             {
  360.               c2 = *t2++;
  361.               if (c2 == end_char)
  362.             break;
  363.               if (c2 != ' ' && c2 != '\t')
  364.             {
  365.               --t2;
  366.               c2 = ' ';
  367.               break;
  368.             }
  369.             }
  370.         }
  371.         }
  372.  
  373.       /* Upcase all letters if -i is specified.  */
  374.  
  375.       if (ignore_case_flag)
  376.         {
  377.           if (islower (c1))
  378.         c1 = toupper (c1);
  379.           if (islower (c2))
  380.         c2 = toupper (c2);
  381.         }
  382.  
  383.       if (c1 != c2)
  384.         break;
  385.       if (c1 == end_char)
  386.         return 0;
  387.     }
  388.     }
  389.  
  390.   return (1);
  391. }
  392.  
  393. /* Find the consecutive changes at the start of the script START.
  394.    Return the last link before the first gap.  */
  395.  
  396. struct change *
  397. find_change (start)
  398.      struct change *start;
  399. {
  400.   return start;
  401. }
  402.  
  403. struct change *
  404. find_reverse_change (start)
  405.      struct change *start;
  406. {
  407.   return start;
  408. }
  409.  
  410. /* Divide SCRIPT into pieces by calling HUNKFUN and
  411.    print each piece with PRINTFUN.
  412.    Both functions take one arg, an edit script.
  413.  
  414.    HUNKFUN is called with the tail of the script
  415.    and returns the last link that belongs together with the start
  416.    of the tail.
  417.  
  418.    PRINTFUN takes a subscript which belongs together (with a null
  419.    link at the end) and prints it.  */
  420.  
  421. #ifdef MSDOS
  422. void
  423. print_script (    struct change *script,
  424.         struct change * (*hunkfun) (struct change *),
  425.         void (*printfun) (struct change *) )
  426. #else
  427. void
  428. print_script (script, hunkfun, printfun)
  429.      struct change *script;
  430.      struct change * (*hunkfun) ();
  431.      void (*printfun) ();
  432. #endif /* MSDOS */
  433. {
  434.   struct change *next = script;
  435.  
  436.   while (next)
  437.     {
  438.       struct change *this, *end;
  439.  
  440.       /* Find a set of changes that belong together.  */
  441.       this = next;
  442.       end = (*hunkfun) (next);
  443.  
  444.       /* Disconnect them from the rest of the changes,
  445.      making them a hunk, and remember the rest for next iteration.  */
  446.       next = end->link;
  447.       end->link = NULL;
  448. #ifdef DEBUG
  449.       debug_script (this);
  450. #endif
  451.  
  452.       /* Print this hunk.  */
  453.       (*printfun) (this);
  454.  
  455.       /* Reconnect the script so it will all be freed properly.  */
  456.       end->link = next;
  457.     }
  458. }
  459.  
  460. /* Print the text of a single line LINE,
  461.    flagging it with the characters in LINE_FLAG (which say whether
  462.    the line is inserted, deleted, changed, etc.).  */
  463.  
  464. void
  465. print_1_line (line_flag, line)
  466.      char *line_flag;
  467.      struct line_def *line;
  468. {
  469.   int length = line->length; /* must be nonzero */
  470.   const char *text = line->text; /* Help the compiler.  */
  471.   FILE *out = outfile; /* Help the compiler some more.  */
  472.  
  473.   /* If -T was specified, use a Tab between the line-flag and the text.
  474.      Otherwise use a Space (as Unix diff does).
  475.      Print neither space nor tab if line-flags are empty.  */
  476.  
  477.   if (line_flag != NULL && line_flag[0] != 0)
  478.     fprintf (out, tab_align_flag ? "%s\t" : "%s ", line_flag);
  479.  
  480.   /* Now output the contents of the line.
  481.      If -t was specified, expand tabs to spaces.
  482.      Otherwise output verbatim.  */
  483.  
  484.   if (tab_expand_flag)
  485.     {
  486.       register int column = 0;
  487.       register int i;
  488.       for (i = 0; i < line->length; i++)
  489.     {
  490.       register char c = line->text[i];
  491.       switch (c)
  492.         {
  493.         case '\t':
  494.           column++;
  495.           while (column & 7)
  496.         {
  497.           putc (' ', out);
  498.           column++;
  499.         }
  500.           c = ' ';
  501.           break;
  502.         case '\b':
  503.           column--;
  504.           break;
  505.         default:
  506.           column++;
  507.           break;
  508.         }
  509.       putc (c, out);
  510.     }
  511.     }
  512.   else
  513.     fwrite (text, sizeof (char), length, out);
  514.   if ((line_flag == NULL || line_flag[0] != 0) && text[length - 1] != '\n'
  515.       && line_end_char == '\n')
  516.     fprintf (out, "\n\\ No newline at end of file\n");
  517. }
  518.  
  519. change_letter (inserts, deletes)
  520.      int inserts, deletes;
  521. {
  522.   if (!inserts)
  523.     return 'd';
  524.   else if (!deletes)
  525.     return 'a';
  526.   else
  527.     return 'c';
  528. }
  529.  
  530. /* Translate an internal line number (an index into diff's table of lines)
  531.    into an actual line number in the input file.
  532.    The internal line number is LNUM.  FILE points to the data on the file.
  533.  
  534.    Internal line numbers count from 0 within the current chunk.
  535.    Actual line numbers count from 1 within the entire file;
  536.    in addition, they include lines ignored for comparison purposes.
  537.  
  538.    The `ltran' feature is no longer in use.  */
  539.  
  540. int
  541. translate_line_number (file, lnum)
  542.      struct file_data *file;
  543.      int lnum;
  544. {
  545.   return lnum + 1;
  546. }
  547.  
  548. void
  549. translate_range (file, a, b, aptr, bptr)
  550.      struct file_data *file;
  551.      int a, b;
  552.      int *aptr, *bptr;
  553. {
  554.   *aptr = translate_line_number (file, a - 1) + 1;
  555.   *bptr = translate_line_number (file, b + 1) - 1;
  556. }
  557.  
  558. /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
  559.    If the two numbers are identical, print just one number.
  560.  
  561.    Args A and B are internal line numbers.
  562.    We print the translated (real) line numbers.  */
  563.  
  564. void
  565. print_number_range (sepchar, file, a, b)
  566.      char sepchar;
  567.      struct file_data *file;
  568.      int a, b;
  569. {
  570.   int trans_a, trans_b;
  571.   translate_range (file, a, b, &trans_a, &trans_b);
  572.  
  573.   /* Note: we can have B < A in the case of a range of no lines.
  574.      In this case, we should print the line number before the range,
  575.      which is B.  */
  576.   if (trans_b > trans_a)
  577.     fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
  578.   else
  579.     fprintf (outfile, "%d", trans_b);
  580. }
  581.  
  582. /* Look at a hunk of edit script and report the range of lines in each file
  583.    that it applies to.  HUNK is the start of the hunk, which is a chain
  584.    of `struct change'.  The first and last line numbers of file 0 are stored in
  585.    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1. 
  586.    Note that these are internal line numbers that count from 0.
  587.  
  588.    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
  589.  
  590.    Also set *DELETES nonzero if any lines of file 0 are deleted
  591.    and set *INSERTS nonzero if any lines of file 1 are inserted.
  592.    If only ignorable lines are inserted or deleted, both are
  593.    set to 0.  */
  594.  
  595. void
  596. analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
  597.      struct change *hunk;
  598.      int *first0, *last0, *first1, *last1;
  599.      int *deletes, *inserts;
  600. {
  601.   int f0, l0, f1, l1, show_from, show_to;
  602.   int i;
  603.   int nontrivial = !(ignore_blank_lines_flag || ignore_regexp);
  604.   struct change *next;
  605.  
  606.   show_from = show_to = 0;
  607.  
  608.   f0 = hunk->line0;
  609.   f1 = hunk->line1;
  610.  
  611.   for (next = hunk; next; next = next->link)
  612.     {
  613.       l0 = next->line0 + next->deleted - 1;
  614.       l1 = next->line1 + next->inserted - 1;
  615.       show_from += next->deleted;
  616.       show_to += next->inserted;
  617.  
  618.       for (i = next->line0; i <= l0 && ! nontrivial; i++)
  619.     if ((!ignore_blank_lines_flag || files[0].linbuf[i].length > 1)
  620.         && (!ignore_regexp
  621.         || 0 > re_search (&ignore_regexp_compiled,
  622.                   files[0].linbuf[i].text,
  623.                   files[0].linbuf[i].length, 0,
  624.                   files[0].linbuf[i].length, 0)))
  625.       nontrivial = 1;
  626.  
  627.       for (i = next->line1; i <= l1 && ! nontrivial; i++)
  628.     if ((!ignore_blank_lines_flag || files[1].linbuf[i].length > 1)
  629.         && (!ignore_regexp
  630.         || 0 > re_search (&ignore_regexp_compiled,
  631.                   files[1].linbuf[i].text,
  632.                   files[1].linbuf[i].length, 0,
  633.                   files[1].linbuf[i].length, 0)))
  634.       nontrivial = 1;
  635.     }
  636.  
  637.   *first0 = f0;
  638.   *last0 = l0;
  639.   *first1 = f1;
  640.   *last1 = l1;
  641.  
  642.   /* If all inserted or deleted lines are ignorable,
  643.      tell the caller to ignore this hunk.  */
  644.  
  645.   if (!nontrivial)
  646.     show_from = show_to = 0;
  647.  
  648.   *deletes = show_from;
  649.   *inserts = show_to;
  650. }
  651.  
  652. /* malloc a block of memory, with fatal error message if we can't do it. */
  653.  
  654. #ifdef MSDOS
  655. #include <malloc.h>
  656. #include <dos.h>
  657.  
  658. void _huge *
  659. xhalloc (long size)
  660. {
  661.   void _huge *value = (void _huge *) halloc (size, (size_t)1 );
  662.  
  663.   if (!value)
  664.     fatal ("virtual memory exhausted");
  665.   return value;
  666. }
  667.  
  668. /* Here we do a huge "realloc" by allocating a new block and
  669.    moving the old block afterwards.  This is *slow*, but should
  670.    be reliable.  */
  671.  
  672. void _huge *
  673. xhrealloc (void _huge *ptr, long new_size, long old_size)
  674. {
  675.   void _huge *value = (void _huge *) halloc (new_size, (size_t)1 );
  676.  
  677.   if (!value)
  678.     fatal ("virtual memory exhausted");
  679.   else
  680.     {
  681.       char _huge *dest = value;
  682.       char _huge *src = ptr;
  683.  
  684.       while (old_size > 0L)
  685.     {
  686.       unsigned int bytes = (unsigned int) min (0x8000L, old_size);
  687.       memcpy (dest, src, bytes);
  688.       old_size -= (long) bytes;
  689.       dest += (long) bytes;
  690.       src += (long) bytes;
  691.     }
  692.     }
  693.   hfree (ptr);
  694.   return value;
  695. }
  696.  
  697. long        /* doesn't belong here, but is also a 'huge' pointer kludge */
  698. hread (int fd, void _huge *buffer, long bytes)
  699. {
  700.   long bytes_read = 0L;
  701.  
  702.   while (1)
  703.     {
  704.       int n = read (fd, buffer, (unsigned int) min (0x4000L, bytes));
  705.  
  706.       if (n > 0)
  707.     {
  708.       bytes_read += (long) n;
  709.       bytes -= (long) n;
  710.       /* we can't say buffer += n here, because we have to make
  711.          sure that the offset of BUFFER doesn't overflow during
  712.          the read() system call.  Therefore we add what we read
  713.          to the segment of BUFFER.  */
  714.       FP_SEG(buffer) += (n >> 4);
  715.       FP_OFF(buffer) += (n & 0x0F);
  716.     }
  717.       else if (n == 0)
  718.     return bytes_read;    /* done */
  719.       else
  720.     pfatal_with_name ("error while reading input");
  721.     }
  722. }
  723.  
  724. #endif /* MSDOS */
  725.  
  726. VOID *
  727. xmalloc (size)
  728.      unsigned size;
  729. {
  730.   register VOID *value;
  731.  
  732.   if (size == 0)
  733.     size = 1;
  734.  
  735.   value = (VOID *) malloc (size);
  736.  
  737.   if (!value)
  738.     fatal ("virtual memory exhausted");
  739.   return value;
  740. }
  741.  
  742. /* realloc a block of memory, with fatal error message if we can't do it. */
  743.  
  744. VOID *
  745. xrealloc (old, size)
  746.      VOID *old;
  747.      unsigned int size;
  748. {
  749.   register VOID *value;
  750.  
  751.   if (size == 0)
  752.     size = 1;
  753.  
  754.   value = (VOID *) realloc (old, size);
  755.  
  756.   if (!value)
  757.     fatal ("virtual memory exhausted");
  758.   return value;
  759. }
  760.  
  761. /* Concatenate three strings, returning a newly malloc'd string.  */
  762.  
  763. char *
  764. concat (s1, s2, s3)
  765.      char *s1, *s2, *s3;
  766. {
  767.   int len = strlen (s1) + strlen (s2) + strlen (s3);
  768.   char *new = (char *) xmalloc (len + 1);
  769.   strcpy (new, s1);
  770.   strcat (new, s2);
  771.   strcat (new, s3);
  772.   return new;
  773. }
  774.  
  775. #ifdef MSDOS
  776. void
  777. #endif /* MSDOS */
  778. debug_script (sp)
  779.      struct change *sp;
  780. {
  781.   fflush (stdout);
  782.   for (; sp; sp = sp->link)
  783.     fprintf (stderr, "%3d %3d delete %d insert %d\n",
  784.          sp->line0, sp->line1, sp->deleted, sp->inserted);
  785.   fflush (stderr);
  786. }
  787.  
  788. #ifdef MSDOS
  789.  
  790. static char *pipe_file = NULL;
  791. static char *tmpdir = NULL;
  792. static int tmpdirlen;
  793.  
  794. /* Return the name of a temporary file.  */
  795. char *
  796. _pipe_file (void)
  797. {
  798.   if (!pipe_file)
  799.     {
  800.       if (!tmpdir)
  801.     {
  802.       /* Initialize.  */
  803.  
  804.       atexit (cleanup_pipe);
  805.  
  806.       tmpdir = getenv ("TMP");
  807.  
  808.       if (tmpdir)
  809.         {
  810.           tmpdirlen = strlen (tmpdir);
  811.           if (tmpdir[tmpdirlen - 1] == '/'
  812.           || tmpdir[tmpdirlen - 1] == '\\')
  813.         tmpdir[tmpdirlen - 1] = '\0';
  814.         }
  815.       else
  816.         {
  817.           tmpdir = ".";
  818.           tmpdirlen = 1;
  819.         }
  820.     }
  821.  
  822.       pipe_file = (char *) xmalloc (tmpdirlen + 14);
  823.       sprintf (pipe_file, "%s/diff%04x", tmpdir, getpid ());
  824.     }
  825.  
  826.   return pipe_file;
  827. }
  828.  
  829.  
  830. /* Clean up after we are done. */
  831.  
  832. void
  833. cleanup_pipe (void)
  834. {
  835.   unlink (pipe_file);
  836. }
  837.  
  838. #endif /* MSDOS */
  839.  
  840.