home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / diff.lzh / diff / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-04-24  |  18.0 KB  |  776 lines

  1. static char RCSid[]="$Id: util.c_v 1.1 96/04/23 02:57:01 hiro Exp $";
  2. /* Support routines for GNU DIFF.
  3.    Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU DIFF.
  6.  
  7. GNU DIFF is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU DIFF is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU DIFF; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "diff.h"
  22. #include <stdio.h>
  23. #include <ctype.h>
  24.  
  25. #ifndef pid_t
  26. #define pid_t    int
  27. #endif
  28.  
  29. /* Queue up one-line messages to be printed at the end,
  30.    when -l is specified.  Each message is recorded with a `struct msg'.  */
  31.  
  32. struct msg
  33. {
  34.   struct msg *next;
  35.   char const *format;
  36.   char const *arg1;
  37.   char const *arg2;
  38.   char const *arg3;
  39.   char const *arg4;
  40. };
  41.  
  42. /* Head of the chain of queues messages.  */
  43.  
  44. static struct msg *msg_chain;
  45.  
  46. /* Tail of the chain of queues messages.  */
  47.  
  48. static struct msg **msg_chain_end = &msg_chain;
  49.  
  50. /* Use when a system call returns non-zero status.
  51.    TEXT should normally be the file name.  */
  52.  
  53. void
  54. perror_with_name (text)
  55.      char const *text;
  56. {
  57.   int e = errno;
  58.   fprintf (stderr, "%s: ", program);
  59.   errno = e;
  60.   perror (text);
  61. }
  62.  
  63. /* Use when a system call returns non-zero status and that is fatal.  */
  64.  
  65. void
  66. pfatal_with_name (text)
  67.      char const *text;
  68. {
  69.   int e = errno;
  70.   print_message_queue ();
  71.   fprintf (stderr, "%s: ", program);
  72.   errno = e;
  73.   perror (text);
  74.   exit (2);
  75. }
  76.  
  77. /* Print an error message from the format-string FORMAT
  78.    with args ARG1 and ARG2.  */
  79.  
  80. void
  81. error (format, arg, arg1)
  82.      char const *format, *arg, *arg1;
  83. {
  84.   fprintf (stderr, "%s: ", program);
  85.   fprintf (stderr, format, arg, arg1);
  86.   fprintf (stderr, "\n");
  87. }
  88.  
  89. /* Print an error message containing the string TEXT, then exit.  */
  90.  
  91. void
  92. fatal (m)
  93.      char const *m;
  94. {
  95.   print_message_queue ();
  96.   error ("%s", m, 0);
  97.   exit (2);
  98. }
  99.  
  100. /* Like printf, except if -l in effect then save the message and print later.
  101.    This is used for things like "binary files differ" and "Only in ...".  */
  102.  
  103. void
  104. message (format, arg1, arg2)
  105.      char const *format, *arg1, *arg2;
  106. {
  107.   message5 (format, arg1, arg2, 0, 0);
  108. }
  109.  
  110. void
  111. message5 (format, arg1, arg2, arg3, arg4)
  112.      char const *format, *arg1, *arg2, *arg3, *arg4;
  113. {
  114.   if (paginate_flag)
  115.     {
  116.       struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));
  117.       new->format = format;
  118.       new->arg1 = concat (arg1, "", "");
  119.       new->arg2 = concat (arg2, "", "");
  120.       new->arg3 = arg3 ? concat (arg3, "", "") : 0;
  121.       new->arg4 = arg4 ? concat (arg4, "", "") : 0;
  122.       new->next = 0;
  123.       *msg_chain_end = new;
  124.       msg_chain_end = &new->next;
  125.     }
  126.   else
  127.     {
  128.       if (sdiff_help_sdiff)
  129.     putchar (' ');
  130.       printf (format, arg1, arg2, arg3, arg4);
  131.     }
  132. }
  133.  
  134. /* Output all the messages that were saved up by calls to `message'.  */
  135.  
  136. void
  137. print_message_queue ()
  138. {
  139.   struct msg *m;
  140.  
  141.   for (m = msg_chain; m; m = m->next)
  142.     printf (m->format, m->arg1, m->arg2, m->arg3, m->arg4);
  143. }
  144.  
  145. /* Call before outputting the results of comparing files NAME0 and NAME1
  146.    to set up OUTFILE, the stdio stream for the output to go to.
  147.  
  148.    Usually, OUTFILE is just stdout.  But when -l was specified
  149.    we fork off a `pr' and make OUTFILE a pipe to it.
  150.    `pr' then outputs to our stdout.  */
  151.  
  152. static char const *current_name0;
  153. static char const *current_name1;
  154. static int current_depth;
  155.  
  156. void
  157. setup_output (name0, name1, depth)
  158.      char const *name0, *name1;
  159.      int depth;
  160. {
  161.   current_name0 = name0;
  162.   current_name1 = name1;
  163.   current_depth = depth;
  164.   outfile = 0;
  165. }
  166.  
  167. static pid_t pr_pid;
  168.  
  169. void
  170. begin_output ()
  171. {
  172.   char *name;
  173. #ifndef __STDC__
  174. extern FILE *fdopen();
  175. #endif
  176.  
  177.   if (outfile != 0)
  178.     return;
  179.  
  180.   /* Construct the header of this piece of diff.  */
  181.   name = xmalloc (strlen (current_name0) + strlen (current_name1)
  182.           + strlen (switch_string) + 7);
  183.   /* Posix.2 section 4.17.6.1.1 specifies this format.  But there are some
  184.      bugs in the first printing (IEEE Std 1003.2-1992 p 251 l 3304):
  185.      it says that we must print only the last component of the pathnames,
  186.      and it requires two spaces after "diff" if there are no options.
  187.      These requirements are silly and do not match historical practice.  */
  188.   sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);
  189.  
  190.   if (paginate_flag)
  191.     {
  192. #ifdef _OS9000
  193.       extern int os9forkc();
  194.       int savestdin;
  195.       static char *args[] = {PR_FILE_NAME, "-f", "-h", 0, 0};
  196.  
  197.       args[3] = name;
  198.       savestdin = dup(0);
  199.       close(0);
  200.       open("/pipe", 3);
  201.       if ((pr_pid = os9exec(os9forkc, args[0], args, 0, 0, 3)) < 0)
  202.     pfatal_with_name (PR_FILE_NAME);
  203.       outfile = fdopen (dup(0), "w");
  204.       close(0);
  205.       dup(savestdin);
  206.       close(savestdin);
  207. #else
  208.       int pipes[2];
  209.  
  210.       /* Fork a `pr' and make OUTFILE a pipe to it.  */
  211.       if (pipe (pipes) < 0)
  212.     pfatal_with_name ("pipe");
  213.  
  214.       fflush (stdout);
  215.  
  216.       pr_pid = vfork ();
  217.       if (pr_pid < 0)
  218.     pfatal_with_name ("vfork");
  219.  
  220.       if (pr_pid == 0)
  221.     {
  222.       close (pipes[1]);
  223.       if (pipes[0] != STDIN_FILENO)
  224.         {
  225.           if (dup2 (pipes[0], STDIN_FILENO) < 0)
  226.         pfatal_with_name ("dup2");
  227.           close (pipes[0]);
  228.         }
  229.  
  230.       execl (PR_FILE_NAME, PR_FILE_NAME, "-f", "-h", name, 0);
  231.       pfatal_with_name (PR_FILE_NAME);
  232.     }
  233.       else
  234.     {
  235.       close (pipes[0]);
  236.       outfile = fdopen (pipes[1], "w");
  237.     }
  238. #endif
  239.     }
  240.   else
  241.     {
  242.  
  243.       /* If -l was not specified, output the diff straight to `stdout'.  */
  244.  
  245.       outfile = stdout;
  246.  
  247.       /* If handling multiple files (because scanning a directory),
  248.      print which files the following output is about.  */
  249.       if (current_depth > 0)
  250.     printf ("%s\n", name);
  251.     }
  252.  
  253.   free (name);
  254.  
  255.   /* A special header is needed at the beginning of context output.  */
  256.   switch (output_style)
  257.     {
  258.     case OUTPUT_CONTEXT:
  259.       print_context_header (files, 0);
  260.       break;
  261.  
  262.     case OUTPUT_UNIFIED:
  263.       print_context_header (files, 1);
  264.       break;
  265.  
  266.     default:
  267.       break;
  268.     }
  269. }
  270.  
  271. /* Call after the end of output of diffs for one file.
  272.    Close OUTFILE and get rid of the `pr' subfork.  */
  273.  
  274. void
  275. finish_output ()
  276. {
  277.   if (outfile != 0 && outfile != stdout)
  278.     {
  279.       int wstatus;
  280.       if (ferror (outfile))
  281.     fatal ("write error");
  282.       if (fclose (outfile) != 0)
  283.     pfatal_with_name ("write error");
  284. #if defined(HAVE_WAITPID)
  285.       if (waitpid (pr_pid, &wstatus, 0) < 0)
  286.     pfatal_with_name ("waitpid");
  287. #else
  288.       for (;;) {
  289.     pid_t w = wait (&wstatus);
  290.     if (w < 0)
  291.       pfatal_with_name ("wait");
  292.     if (w == pr_pid)
  293.       break;
  294.       }
  295. #endif
  296.       if (! WIFEXITED (wstatus) || WEXITSTATUS (wstatus) != 0)
  297.     fatal ("subsidiary pr failed");
  298.     }
  299.  
  300.   outfile = 0;
  301. }
  302.  
  303. /* Compare two lines (typically one from each input file)
  304.    according to the command line options.
  305.    Return 1 if the lines differ, like `memcmp'.  */
  306.  
  307. int
  308. line_cmp (s1, len1, s2, len2)
  309.      char const *s1, *s2;
  310.      size_t len1, len2;
  311. {
  312.   register unsigned char const *t1, *t2;
  313.   register unsigned char end_char = line_end_char;
  314.  
  315.   /* Check first for exact identity.
  316.      If that is true, return 0 immediately.
  317.      This detects the common case of exact identity
  318.      faster than complete comparison would.  */
  319.  
  320.   if (len1 == len2 && memcmp (s1, s2, len1) == 0)
  321.     return 0;
  322.  
  323.   /* Not exactly identical, but perhaps they match anyway
  324.      when case or white space is ignored.  */
  325.  
  326.   if (ignore_case_flag | ignore_space_change_flag | ignore_all_space_flag)
  327.     {
  328.       t1 = (unsigned char *) s1;
  329.       t2 = (unsigned char *) s2;
  330.  
  331.       while (1)
  332.     {
  333.       register unsigned char c1 = *t1++;
  334.       register unsigned char c2 = *t2++;
  335.  
  336.       /* Ignore horizontal white space if -b or -w is specified.  */
  337.  
  338.       if (ignore_all_space_flag)
  339.         {
  340.           /* For -w, just skip past any white space.  */
  341.           while (isspace (c1) && c1 != end_char) c1 = *t1++;
  342.           while (isspace (c2) && c2 != end_char) c2 = *t2++;
  343.         }
  344.       else if (ignore_space_change_flag)
  345.         {
  346.           /* For -b, advance past any sequence of white space in line 1
  347.          and consider it just one Space, or nothing at all
  348.          if it is at the end of the line.  */
  349.           if (isspace (c1))
  350.         {
  351.           while (c1 != end_char)
  352.             {
  353.               c1 = *t1++;
  354.               if (! isspace (c1))
  355.             {
  356.               --t1;
  357.               c1 = ' ';
  358.               break;
  359.             }
  360.             }
  361.         }
  362.  
  363.           /* Likewise for line 2.  */
  364.           if (isspace (c2))
  365.         {
  366.           while (c2 != end_char)
  367.             {
  368.               c2 = *t2++;
  369.               if (! isspace (c2))
  370.             {
  371.               --t2;
  372.               c2 = ' ';
  373.               break;
  374.             }
  375.             }
  376.         }
  377.         }
  378.  
  379.       /* Upcase all letters if -i is specified.  */
  380.  
  381.       if (ignore_case_flag)
  382.         {
  383.           if (islower (c1))
  384.         c1 = toupper (c1);
  385.           if (islower (c2))
  386.         c2 = toupper (c2);
  387.         }
  388.  
  389.       if (c1 != c2)
  390.         break;
  391.       if (c1 == end_char)
  392.         return 0;
  393.     }
  394.     }
  395.  
  396.   return (1);
  397. }
  398.  
  399. /* Find the consecutive changes at the start of the script START.
  400.    Return the last link before the first gap.  */
  401.  
  402. struct change *
  403. find_change (start)
  404.      struct change *start;
  405. {
  406.   return start;
  407. }
  408.  
  409. struct change *
  410. find_reverse_change (start)
  411.      struct change *start;
  412. {
  413.   return start;
  414. }
  415.  
  416. /* Divide SCRIPT into pieces by calling HUNKFUN and
  417.    print each piece with PRINTFUN.
  418.    Both functions take one arg, an edit script.
  419.  
  420.    HUNKFUN is called with the tail of the script
  421.    and returns the last link that belongs together with the start
  422.    of the tail.
  423.  
  424.    PRINTFUN takes a subscript which belongs together (with a null
  425.    link at the end) and prints it.  */
  426.  
  427. void
  428. print_script (script, hunkfun, printfun)
  429.      struct change *script;
  430.      struct change * (*hunkfun) PARAMS((struct change *));
  431.      void (*printfun) PARAMS((struct change *));
  432. {
  433.   struct change *next = script;
  434.  
  435.   while (next)
  436.     {
  437.       struct change *this, *end;
  438.  
  439.       /* Find a set of changes that belong together.  */
  440.       this = next;
  441.       end = (*hunkfun) (next);
  442.  
  443.       /* Disconnect them from the rest of the changes,
  444.      making them a hunk, and remember the rest for next iteration.  */
  445.       next = end->link;
  446.       end->link = 0;
  447. /*#ifdef DEBUG
  448.       debug_script (this);
  449. #endif*/
  450.  
  451.       /* Print this hunk.  */
  452.       (*printfun) (this);
  453.  
  454.       /* Reconnect the script so it will all be freed properly.  */
  455.       end->link = next;
  456.     }
  457. }
  458.  
  459. /* Print the text of a single line LINE,
  460.    flagging it with the characters in LINE_FLAG (which say whether
  461.    the line is inserted, deleted, changed, etc.).  */
  462.  
  463. void
  464. print_1_line (line_flag, line)
  465.      char *line_flag;
  466.      char **line;
  467. {
  468.   char *text = line[0], *limit = line[1]; /* Help the compiler.  */
  469.   FILE *out = outfile; /* Help the compiler some more.  */
  470.   char *flag_format = 0;
  471.  
  472.   /* If -T was specified, use a Tab between the line-flag and the text.
  473.      Otherwise use a Space (as Unix diff does).
  474.      Print neither space nor tab if line-flags are empty.  */
  475.  
  476.   if (line_flag && *line_flag)
  477.     {
  478.       flag_format = tab_align_flag ? "%s\t" : "%s ";
  479.       fprintf (out, flag_format, line_flag);
  480.     }
  481.  
  482.   output_1_line (text, limit, flag_format, line_flag);
  483.  
  484.   if ((!line_flag || line_flag[0]) && limit[-1] != '\n'
  485.       && line_end_char == '\n')
  486.     fprintf (out, "\n\\ No newline at end of file\n");
  487. }
  488.  
  489. /* Output a line from TEXT up to LIMIT.  Without -t, output verbatim.
  490.    With -t, expand white space characters to spaces, and if FLAG_FORMAT
  491.    is nonzero, output it with argument LINE_FLAG after every
  492.    internal carriage return, so that tab stops continue to line up.  */
  493.  
  494. void
  495. output_1_line (text, limit, flag_format, line_flag)
  496.      char *text, *limit, *flag_format, *line_flag;
  497. {
  498.   if (!tab_expand_flag)
  499.     fwrite (text, sizeof (char), limit - text, outfile);
  500.   else
  501.     {
  502.       register FILE *out = outfile;
  503.       register unsigned char c;
  504.       register char *t = text;
  505.       register unsigned column = 0;
  506.  
  507.       while (t < limit)
  508.     switch ((c = *t++))
  509.       {
  510.       case '\t':
  511.         {
  512.           unsigned spaces = TAB_WIDTH - column % TAB_WIDTH;
  513.           column += spaces;
  514.           do
  515.         putc (' ', out);
  516.           while (--spaces);
  517.         }
  518.         break;
  519.  
  520. #ifdef _OS9000
  521.       case '\012':
  522. #else
  523.       case '\r':
  524. #endif
  525.         putc (c, out);
  526.         if (flag_format && t < limit && *t != '\n')
  527.           fprintf (out, flag_format, line_flag);
  528.         column = 0;
  529.         break;
  530.  
  531.       case '\b':
  532.         if (column == 0)
  533.           continue;
  534.         column--;
  535.         putc (c, out);
  536.         break;
  537.  
  538.       default:
  539.         if (isprint (c))
  540.           column++;
  541.         putc (c, out);
  542.         break;
  543.       }
  544.     }
  545. }
  546.  
  547. int
  548. change_letter (inserts, deletes)
  549.      int inserts, deletes;
  550. {
  551.   if (!inserts)
  552.     return 'd';
  553.   else if (!deletes)
  554.     return 'a';
  555.   else
  556.     return 'c';
  557. }
  558.  
  559. /* Translate an internal line number (an index into diff's table of lines)
  560.    into an actual line number in the input file.
  561.    The internal line number is LNUM.  FILE points to the data on the file.
  562.  
  563.    Internal line numbers count from 0 starting after the prefix.
  564.    Actual line numbers count from 1 within the entire file.  */
  565.  
  566. int
  567. translate_line_number (file, lnum)
  568.      struct file_data *file;
  569.      int lnum;
  570. {
  571.   return lnum + file->prefix_lines + 1;
  572. }
  573.  
  574. void
  575. translate_range (file, a, b, aptr, bptr)
  576.      struct file_data *file;
  577.      int a, b;
  578.      int *aptr, *bptr;
  579. {
  580.   *aptr = translate_line_number (file, a - 1) + 1;
  581.   *bptr = translate_line_number (file, b + 1) - 1;
  582. }
  583.  
  584. /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
  585.    If the two numbers are identical, print just one number.
  586.  
  587.    Args A and B are internal line numbers.
  588.    We print the translated (real) line numbers.  */
  589.  
  590. void
  591. print_number_range (sepchar, file, a, b)
  592.      char sepchar;
  593.      struct file_data *file;
  594.      int a, b;
  595. {
  596.   int trans_a, trans_b;
  597.   translate_range (file, a, b, &trans_a, &trans_b);
  598.  
  599.   /* Note: we can have B < A in the case of a range of no lines.
  600.      In this case, we should print the line number before the range,
  601.      which is B.  */
  602.   if (trans_b > trans_a)
  603.     fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
  604.   else
  605.     fprintf (outfile, "%d", trans_b);
  606. }
  607.  
  608. /* Look at a hunk of edit script and report the range of lines in each file
  609.    that it applies to.  HUNK is the start of the hunk, which is a chain
  610.    of `struct change'.  The first and last line numbers of file 0 are stored in
  611.    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
  612.    Note that these are internal line numbers that count from 0.
  613.  
  614.    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
  615.  
  616.    Also set *DELETES nonzero if any lines of file 0 are deleted
  617.    and set *INSERTS nonzero if any lines of file 1 are inserted.
  618.    If only ignorable lines are inserted or deleted, both are
  619.    set to 0.  */
  620.  
  621. void
  622. analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
  623.      struct change *hunk;
  624.      int *first0, *last0, *first1, *last1;
  625.      int *deletes, *inserts;
  626. {
  627.   int l0, l1, show_from, show_to;
  628.   int i;
  629.   int trivial = ignore_blank_lines_flag || ignore_regexp_list;
  630.   struct change *next;
  631.  
  632.   show_from = show_to = 0;
  633.  
  634.   *first0 = hunk->line0;
  635.   *first1 = hunk->line1;
  636.  
  637.   next = hunk;
  638.   do
  639.     {
  640.       l0 = next->line0 + next->deleted - 1;
  641.       l1 = next->line1 + next->inserted - 1;
  642.       show_from += next->deleted;
  643.       show_to += next->inserted;
  644.  
  645.       for (i = next->line0; i <= l0 && trivial; i++)
  646.     if (!ignore_blank_lines_flag || files[0].linbuf[i][0] != '\n')
  647.       {
  648.         struct regexp_list *r;
  649.         char *line = files[0].linbuf[i];
  650.         int len = files[0].linbuf[i + 1] - line;
  651.  
  652.         for (r = ignore_regexp_list; r; r = r->next)
  653.           if (0 <= re_search (&r->buf, line, len, 0, len, 0))
  654.         break;    /* Found a match.  Ignore this line.  */
  655.         /* If we got all the way through the regexp list without
  656.            finding a match, then it's nontrivial.  */
  657.         if (!r)
  658.           trivial = 0;
  659.       }
  660.  
  661.       for (i = next->line1; i <= l1 && trivial; i++)
  662.     if (!ignore_blank_lines_flag || files[1].linbuf[i][0] != '\n')
  663.       {
  664.         struct regexp_list *r;
  665.         char *line = files[1].linbuf[i];
  666.         int len = files[1].linbuf[i + 1] - line;
  667.  
  668.         for (r = ignore_regexp_list; r; r = r->next)
  669.           if (0 <= re_search (&r->buf, line, len, 0, len, 0))
  670.         break;    /* Found a match.  Ignore this line.  */
  671.         /* If we got all the way through the regexp list without
  672.            finding a match, then it's nontrivial.  */
  673.         if (!r)
  674.           trivial = 0;
  675.       }
  676.     }
  677.   while ((next = next->link) != 0);
  678.  
  679.   *last0 = l0;
  680.   *last1 = l1;
  681.  
  682.   /* If all inserted or deleted lines are ignorable,
  683.      tell the caller to ignore this hunk.  */
  684.  
  685.   if (trivial)
  686.     show_from = show_to = 0;
  687.  
  688.   *deletes = show_from;
  689.   *inserts = show_to;
  690. }
  691.  
  692. /* malloc a block of memory, with fatal error message if we can't do it. */
  693.  
  694. VOID *
  695. xmalloc (size)
  696.      size_t size;
  697. {
  698.   register VOID *value;
  699.  
  700.   if (size == 0)
  701.     size = 1;
  702.  
  703.   value = (VOID *) malloc (size);
  704.  
  705.   if (!value)
  706.     fatal ("memory exhausted");
  707.   return value;
  708. }
  709.  
  710. /* realloc a block of memory, with fatal error message if we can't do it. */
  711.  
  712. VOID *
  713. xrealloc (old, size)
  714.      VOID *old;
  715.      size_t size;
  716. {
  717.   register VOID *value;
  718.  
  719.   if (size == 0)
  720.     size = 1;
  721.  
  722.   value = (VOID *) realloc (old, size);
  723.  
  724.   if (!value)
  725.     fatal ("memory exhausted");
  726.   return value;
  727. }
  728.  
  729. /* Concatenate three strings, returning a newly malloc'd string.  */
  730.  
  731. char *
  732. concat (s1, s2, s3)
  733.      char *s1, *s2, *s3;
  734. {
  735.   size_t len = strlen (s1) + strlen (s2) + strlen (s3);
  736.   char *new = xmalloc (len + 1);
  737.   sprintf (new, "%s%s%s", s1, s2, s3);
  738.   return new;
  739. }
  740.  
  741. /* Yield the newly malloc'd pathname
  742.    of the file in DIR whose filename is FILE.  */
  743.  
  744. char *
  745. dir_file_pathname (dir, file)
  746.      char *dir, *file;
  747. {
  748.   return concat (dir, "/" + (*dir && dir[strlen (dir) - 1] == '/'), file);
  749. }
  750.  
  751. void
  752. debug_script (sp)
  753.      struct change *sp;
  754. {
  755.   fflush (stdout);
  756.   for (; sp; sp = sp->link)
  757.     fprintf (stderr, "%3d %3d delete %d insert %d\n",
  758.          sp->line0, sp->line1, sp->deleted, sp->inserted);
  759.   fflush (stderr);
  760. }
  761.  
  762. #if !defined(HAVE_MEMCHR)
  763. char *
  764. memchr (s, c, n)
  765.      char *s;
  766.      int c;
  767.      size_t n;
  768. {
  769.   unsigned char *p = (unsigned char *) s, *lim = p + n;
  770.   for (;  p < lim;  p++)
  771.     if (*p == c)
  772.       return (char *) p;
  773.   return 0;
  774. }
  775. #endif
  776.