home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnudiff.zip / io.c < prev    next >
C/C++ Source or Header  |  1994-10-31  |  21KB  |  726 lines

  1. /* File I/O for GNU DIFF.
  2.    Copyright (C) 1988, 1989, 1992, 1993, 1994 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 2, 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. #include "diff.h"
  21.  
  22. /* Rotate an unsigned value to the left.  */
  23. #define ROL(v, n) ((v) << (n) | (v) >> (sizeof (v) * CHAR_BIT - (n)))
  24.  
  25. /* Given a hash value and a new character, return a new hash value.  */
  26. #define HASH(h, c) ((c) + ROL (h, 7))
  27.  
  28. /* Guess remaining number of lines from number N of lines so far,
  29.    size S so far, and total size T.  */
  30. #define GUESS_LINES(n,s,t) (((t) - (s)) / ((n) < 10 ? 32 : (s) / ((n)-1)) + 5)
  31.  
  32. /* Type used for fast prefix comparison in find_identical_ends.  */
  33. #ifndef word
  34. #define word int
  35. #endif
  36.  
  37. /* Lines are put into equivalence classes (of lines that match in line_cmp).
  38.    Each equivalence class is represented by one of these structures,
  39.    but only while the classes are being computed.
  40.    Afterward, each class is represented by a number.  */
  41. struct equivclass
  42. {
  43.   int next;    /* Next item in this bucket.  */
  44.   unsigned hash;    /* Hash of lines in this class.  */
  45.   char const *line;    /* A line that fits this class.  */
  46.   size_t length;    /* That line's length, not counting its newline.  */
  47. };
  48.  
  49. /* Hash-table: array of buckets, each being a chain of equivalence classes.
  50.    buckets[-1] is reserved for incomplete lines.  */
  51. static int *buckets;
  52.  
  53. /* Number of buckets in the hash table array, not counting buckets[-1].  */
  54. static int nbuckets;
  55.  
  56. /* Array in which the equivalence classes are allocated.
  57.    The bucket-chains go through the elements in this array.
  58.    The number of an equivalence class is its index in this array.  */
  59. static struct equivclass *equivs;
  60.  
  61. /* Index of first free element in the array `equivs'.  */
  62. static int equivs_index;
  63.  
  64. /* Number of elements allocated in the array `equivs'.  */
  65. static int equivs_alloc;
  66.  
  67. static void find_and_hash_each_line PARAMS((struct file_data *));
  68. static void find_identical_ends PARAMS((struct file_data[]));
  69. static void prepare_text_end PARAMS((struct file_data *));
  70.  
  71. /* Check for binary files and compare them for exact identity.  */
  72.  
  73. /* Return 1 if BUF contains a non text character.
  74.    SIZE is the number of characters in BUF.  */
  75.  
  76. #define binary_file_p(buf, size) (memchr (buf, '\0', size) != 0)
  77.  
  78. /* Get ready to read the current file.
  79.    Return nonzero if SKIP_TEST is zero,
  80.    and if it appears to be a binary file.  */
  81.  
  82. int
  83. sip (current, skip_test)
  84.      struct file_data *current;
  85.      int skip_test;
  86. {
  87.   /* If we have a nonexistent file at this stage, treat it as empty.  */
  88.   if (current->desc < 0)
  89.     {
  90.       /* Leave room for a sentinel.  */
  91.       current->bufsize = sizeof (word);
  92.       current->buffer = xmalloc (current->bufsize);
  93.     }
  94.   else
  95.     {
  96.       current->bufsize = STAT_BLOCKSIZE (current->stat);
  97.       current->buffer = xmalloc (current->bufsize);
  98.  
  99.       if (! skip_test)
  100.     {
  101.       /* Check first part of file to see if it's a binary file.  */
  102. #if HAVE_SETMODE
  103.       int oldmode = setmode (current->desc, O_BINARY);
  104. #endif
  105.       size_t n = read (current->desc, current->buffer, current->bufsize);
  106.       if (n == -1)
  107.         pfatal_with_name (current->name);
  108.       current->buffered_chars = n;
  109. #if HAVE_SETMODE
  110.       if (oldmode != O_BINARY)
  111.         {
  112.           if (lseek (current->desc, - (off_t) n, SEEK_CUR) == -1)
  113.         pfatal_with_name (current->name);
  114.           setmode (current->desc, oldmode);
  115.           current->buffered_chars = 0;
  116.         }
  117. #endif
  118.       return binary_file_p (current->buffer, n);
  119.     }
  120.     }
  121.  
  122.   current->buffered_chars = 0;
  123.   return 0;
  124. }
  125.  
  126. /* Slurp the rest of the current file completely into memory.  */
  127.  
  128. void
  129. slurp (current)
  130.      struct file_data *current;
  131. {
  132.   size_t cc;
  133.  
  134.   if (current->desc < 0)
  135.     /* The file is nonexistent.  */
  136.     ;
  137.   else if (S_ISREG (current->stat.st_mode))
  138.     {
  139.       /* It's a regular file; slurp in the rest all at once.  */
  140.  
  141.       /* Get the size out of the stat block.
  142.      Allocate just enough room for appended newline plus word sentinel,
  143.      plus word-alignment since we want the buffer word-aligned.  */
  144.       cc = current->stat.st_size + 2 * sizeof (word);
  145.       cc -= cc % sizeof (word);
  146.       if (current->bufsize < cc)
  147.     {
  148.       current->bufsize = cc;
  149.       current->buffer = xrealloc (current->buffer, cc);
  150.     }
  151.  
  152.       if (current->buffered_chars < current->stat.st_size)
  153.     {
  154.       cc = read (current->desc,
  155.              current->buffer + current->buffered_chars,
  156.              current->stat.st_size - current->buffered_chars);
  157.       if (cc == -1)
  158.         pfatal_with_name (current->name);
  159.       current->buffered_chars += cc;
  160.     }
  161.     }
  162.   /* It's not a regular file; read it, growing the buffer as needed.  */
  163.   else if (always_text_flag || current->buffered_chars != 0)
  164.     {
  165.       for (;;)
  166.     {
  167.       if (current->buffered_chars == current->bufsize)
  168.         {
  169.           current->bufsize = current->bufsize * 2;
  170.           current->buffer = xrealloc (current->buffer, current->bufsize);
  171.         }
  172.       cc = read (current->desc,
  173.              current->buffer + current->buffered_chars,
  174.              current->bufsize - current->buffered_chars);
  175.       if (cc == 0)
  176.         break;
  177.       if (cc == -1)
  178.         pfatal_with_name (current->name);
  179.       current->buffered_chars += cc;
  180.     }
  181.       /* Allocate just enough room for appended newline plus word sentinel,
  182.      plus word-alignment since we want the buffer word-aligned.  */
  183.       cc = current->buffered_chars + 2 * sizeof (word);
  184.       current->bufsize = cc - cc % sizeof (word);
  185.       current->buffer = xrealloc (current->buffer, current->bufsize);
  186.     }
  187. }
  188.  
  189. /* Split the file into lines, simultaneously computing the equivalence class for
  190.    each line.  */
  191.  
  192. static void
  193. find_and_hash_each_line (current)
  194.      struct file_data *current;
  195. {
  196.   unsigned h;
  197.   unsigned char const *p = (unsigned char const *) current->prefix_end;
  198.   unsigned char c;
  199.   int i, *bucket;
  200.   size_t length;
  201.  
  202.   /* Cache often-used quantities in local variables to help the compiler.  */
  203.   char const **linbuf = current->linbuf;
  204.   int alloc_lines = current->alloc_lines;
  205.   int line = 0;
  206.   int linbuf_base = current->linbuf_base;
  207.   int *cureqs = (int *) xmalloc (alloc_lines * sizeof (int));
  208.   struct equivclass *eqs = equivs;
  209.   int eqs_index = equivs_index;
  210.   int eqs_alloc = equivs_alloc;
  211.   char const *suffix_begin = current->suffix_begin;
  212.   char const *bufend = current->buffer + current->buffered_chars;
  213.   int (*diff_length_line_cmp) PARAMS((char const *, char const *))
  214.     = (ignore_all_space_flag | ignore_space_change_flag
  215.        ? line_cmp : 0);
  216.   int (*same_length_diff_contents_cmp) PARAMS((char const *, char const *))
  217.     = (ignore_all_space_flag | ignore_space_change_flag | ignore_case_flag
  218.        ? line_cmp : 0);
  219.  
  220.   while ((char const *) p < suffix_begin)
  221.     {
  222.       char const *ip = (char const *) p;
  223.  
  224.       /* Compute the equivalence class for this line.  */
  225.  
  226.       h = 0;
  227.  
  228.       /* Hash this line until we find a newline.  */
  229.       if (ignore_case_flag)
  230.     {
  231.       if (ignore_all_space_flag)
  232.         while ((c = *p++) != '\n')
  233.           {
  234.         if (! ISSPACE (c))
  235.           h = HASH (h, ISUPPER (c) ? _tolower (c) : c);
  236.           }
  237.       else if (ignore_space_change_flag)
  238.         while ((c = *p++) != '\n')
  239.           {
  240.         if (ISSPACE (c))
  241.           {
  242.             do
  243.               {
  244.             if ((c = *p++) == '\n')
  245.               goto hashing_done;
  246.               }
  247.             while (ISSPACE (c));
  248.             h = HASH (h, ' ');
  249.           }
  250.         /* C is now the first non-space.  */
  251.         h = HASH (h, ISUPPER (c) ? _tolower (c) : c);
  252.           }
  253.       else
  254.         while ((c = *p++) != '\n')
  255.           h = HASH (h, ISUPPER (c) ? _tolower (c) : c);
  256.     }
  257.       else
  258.     {
  259.       if (ignore_all_space_flag)
  260.         while ((c = *p++) != '\n')
  261.           {
  262.         if (! ISSPACE (c))
  263.           h = HASH (h, c);
  264.           }
  265.       else if (ignore_space_change_flag)
  266.         while ((c = *p++) != '\n')
  267.           {
  268.         if (ISSPACE (c))
  269.           {
  270.             do
  271.               {
  272.             if ((c = *p++) == '\n')
  273.               goto hashing_done;
  274.               }
  275.             while (ISSPACE (c));
  276.             h = HASH (h, ' ');
  277.           }
  278.         /* C is now the first non-space.  */
  279.         h = HASH (h, c);
  280.           }
  281.       else
  282.         while ((c = *p++) != '\n')
  283.           h = HASH (h, c);
  284.     }
  285.    hashing_done:;
  286.  
  287.       bucket = &buckets[h % nbuckets];
  288.       length = (char const *) p - ip - 1;
  289.  
  290.       if ((char const *) p == bufend
  291.       && current->missing_newline
  292.       && ROBUST_OUTPUT_STYLE (output_style))
  293.     {
  294.       /* This line is incomplete.  If this is significant,
  295.          put the line into bucket[-1].  */
  296.       if (! (ignore_space_change_flag | ignore_all_space_flag))
  297.         bucket = &buckets[-1];
  298.  
  299.       /* Omit the inserted newline when computing linbuf later.  */
  300.       p--;
  301.       bufend = suffix_begin = (char const *) p;
  302.     }
  303.  
  304.       for (i = *bucket;  ;  i = eqs[i].next)
  305.     if (!i)
  306.       {
  307.         /* Create a new equivalence class in this bucket.  */
  308.         i = eqs_index++;
  309.         if (i == eqs_alloc)
  310.           eqs = (struct equivclass *)
  311.               xrealloc (eqs, (eqs_alloc*=2) * sizeof (*eqs));
  312.         eqs[i].next = *bucket;
  313.         eqs[i].hash = h;
  314.         eqs[i].line = ip;
  315.         eqs[i].length = length;
  316.         *bucket = i;
  317.         break;
  318.       }
  319.     else if (eqs[i].hash == h)
  320.       {
  321.         char const *eqline = eqs[i].line;
  322.         int (*compare_lines) PARAMS((char const *, char const *));
  323.  
  324.         /* Reuse existing class if line_cmp reports the lines equal.  */
  325.         if (eqs[i].length == length)
  326.           {
  327.         /* Reuse existing equivalence class if the lines are identical.
  328.            This detects the common case of exact identity
  329.            faster than line_cmp would.  */
  330.         if (memcmp (eqline, ip, length) == 0)
  331.           break;
  332.         compare_lines = same_length_diff_contents_cmp;
  333.           }
  334.         else
  335.           compare_lines = diff_length_line_cmp;
  336.  
  337.         if (compare_lines && (*compare_lines) (eqline, ip) == 0)
  338.           break;
  339.       }
  340.  
  341.       /* Maybe increase the size of the line table.  */
  342.       if (line == alloc_lines)
  343.     {
  344.       /* Double (alloc_lines - linbuf_base) by adding to alloc_lines.  */
  345.       alloc_lines = 2 * alloc_lines - linbuf_base;
  346.       cureqs = (int *) xrealloc (cureqs, alloc_lines * sizeof (*cureqs));
  347.       linbuf = ((char const **) xrealloc (linbuf + linbuf_base,
  348.                           (alloc_lines - linbuf_base)
  349.                           * sizeof (*linbuf))
  350.             - linbuf_base);
  351.     }
  352.       linbuf[line] = ip;
  353.       cureqs[line] = i;
  354.       ++line;
  355.     }
  356.  
  357.   current->buffered_lines = line;
  358.  
  359.   for (i = 0;  ;  i++)
  360.     {
  361.       /* Record the line start for lines in the suffix that we care about.
  362.      Record one more line start than lines,
  363.      so that we can compute the length of any buffered line.  */
  364.       if (line == alloc_lines)
  365.     {
  366.       /* Double (alloc_lines - linbuf_base) by adding to alloc_lines.  */
  367.       alloc_lines = 2 * alloc_lines - linbuf_base;
  368.       linbuf = ((char const **) xrealloc (linbuf + linbuf_base,
  369.                           (alloc_lines - linbuf_base)
  370.                           * sizeof (*linbuf))
  371.             - linbuf_base);
  372.     }
  373.       linbuf[line] = (char const *) p;
  374.  
  375.       if ((char const *) p == bufend)
  376.     break;
  377.  
  378.       if (context <= i && no_diff_means_no_output)
  379.     break;
  380.  
  381.       line++;
  382.  
  383.       while (*p++ != '\n')
  384.     ;
  385.     }
  386.  
  387.   /* Done with cache in local variables.  */
  388.   current->linbuf = linbuf;
  389.   current->valid_lines = line;
  390.   current->alloc_lines = alloc_lines;
  391.   current->equivs = cureqs;
  392.   equivs = eqs;
  393.   equivs_alloc = eqs_alloc;
  394.   equivs_index = eqs_index;
  395. }
  396.  
  397. /* Prepare the end of the text.  Make sure it's initialized.
  398.    Make sure text ends in a newline,
  399.    but remember that we had to add one.  */
  400.  
  401. static void
  402. prepare_text_end (current)
  403.      struct file_data *current;
  404. {
  405.   size_t buffered_chars = current->buffered_chars;
  406.   char *p = current->buffer;
  407.  
  408.   if (buffered_chars == 0 || p[buffered_chars - 1] == '\n')
  409.     current->missing_newline = 0;
  410.   else
  411.     {
  412.       p[buffered_chars++] = '\n';
  413.       current->buffered_chars = buffered_chars;
  414.       current->missing_newline = 1;
  415.     }
  416.  
  417.   /* Don't use uninitialized storage when planting or using sentinels.  */
  418.   if (p)
  419.     bzero (p + buffered_chars, sizeof (word));
  420. }
  421.  
  422. /* Given a vector of two file_data objects, find the identical
  423.    prefixes and suffixes of each object.  */
  424.  
  425. static void
  426. find_identical_ends (filevec)
  427.      struct file_data filevec[];
  428. {
  429.   word *w0, *w1;
  430.   char *p0, *p1, *buffer0, *buffer1;
  431.   char const *end0, *beg0;
  432.   char const **linbuf0, **linbuf1;
  433.   int i, lines;
  434.   size_t n0, n1, tem;
  435.   int alloc_lines0, alloc_lines1;
  436.   int buffered_prefix, prefix_count, prefix_mask;
  437.  
  438.   slurp (&filevec[0]);
  439.   if (filevec[0].desc != filevec[1].desc)
  440.     slurp (&filevec[1]);
  441.   else
  442.     {
  443.       filevec[1].buffer = filevec[0].buffer;
  444.       filevec[1].bufsize = filevec[0].bufsize;
  445.       filevec[1].buffered_chars = filevec[0].buffered_chars;
  446.     }
  447.   for (i = 0; i < 2; i++)
  448.     prepare_text_end (&filevec[i]);
  449.  
  450.   /* Find identical prefix.  */
  451.  
  452.   p0 = buffer0 = filevec[0].buffer;
  453.   p1 = buffer1 = filevec[1].buffer;
  454.  
  455.   n0 = filevec[0].buffered_chars;
  456.   n1 = filevec[1].buffered_chars;
  457.  
  458.   if (p0 == p1)
  459.     /* The buffers are the same; sentinels won't work.  */
  460.     p0 = p1 += n1;
  461.   else
  462.     {
  463.       /* Insert end sentinels, in this case characters that are guaranteed
  464.      to make the equality test false, and thus terminate the loop.  */
  465.  
  466.       if (n0 < n1)
  467.     p0[n0] = ~p1[n0];
  468.       else
  469.     p1[n1] = ~p0[n1];
  470.  
  471.       /* Loop until first mismatch, or to the sentinel characters.  */
  472.  
  473.       /* Compare a word at a time for speed.  */
  474.       w0 = (word *) p0;
  475.       w1 = (word *) p1;
  476.       while (*w0++ == *w1++)
  477.     ;
  478.       --w0, --w1;
  479.  
  480.       /* Do the last few bytes of comparison a byte at a time.  */
  481.       p0 = (char *) w0;
  482.       p1 = (char *) w1;
  483.       while (*p0++ == *p1++)
  484.     ;
  485.       --p0, --p1;
  486.  
  487.       /* Don't mistakenly count missing newline as part of prefix.  */
  488.       if (ROBUST_OUTPUT_STYLE (output_style)
  489.       && (buffer0 + n0 - filevec[0].missing_newline < p0)
  490.          !=
  491.          (buffer1 + n1 - filevec[1].missing_newline < p1))
  492.     --p0, --p1;
  493.     }
  494.  
  495.   /* Now P0 and P1 point at the first nonmatching characters.  */
  496.  
  497.   /* Skip back to last line-beginning in the prefix,
  498.      and then discard up to HORIZON_LINES lines from the prefix.  */
  499.   i = horizon_lines;
  500.   while (p0 != buffer0 && (p0[-1] != '\n' || i--))
  501.     --p0, --p1;
  502.  
  503.   /* Record the prefix.  */
  504.   filevec[0].prefix_end = p0;
  505.   filevec[1].prefix_end = p1;
  506.  
  507.   /* Find identical suffix.  */
  508.  
  509.   /* P0 and P1 point beyond the last chars not yet compared.  */
  510.   p0 = buffer0 + n0;
  511.   p1 = buffer1 + n1;
  512.  
  513.   if (! ROBUST_OUTPUT_STYLE (output_style)
  514.       || filevec[0].missing_newline == filevec[1].missing_newline)
  515.     {
  516.       end0 = p0;    /* Addr of last char in file 0.  */
  517.  
  518.       /* Get value of P0 at which we should stop scanning backward:
  519.      this is when either P0 or P1 points just past the last char
  520.      of the identical prefix.  */
  521.       beg0 = filevec[0].prefix_end + (n0 < n1 ? 0 : n0 - n1);
  522.  
  523.       /* Scan back until chars don't match or we reach that point.  */
  524.       while (p0 != beg0)
  525.     if (*--p0 != *--p1)
  526.       {
  527.         /* Point at the first char of the matching suffix.  */
  528.         ++p0, ++p1;
  529.         beg0 = p0;
  530.         break;
  531.       }
  532.  
  533.       /* Are we at a line-beginning in both files?  If not, add the rest of
  534.      this line to the main body.  Discard up to HORIZON_LINES lines from
  535.      the identical suffix.  Also, discard one extra line,
  536.      because shift_boundaries may need it.  */
  537.       i = horizon_lines + !((buffer0 == p0 || p0[-1] == '\n')
  538.                 &&
  539.                 (buffer1 == p1 || p1[-1] == '\n'));
  540.       while (i-- && p0 != end0)
  541.     while (*p0++ != '\n')
  542.       ;
  543.  
  544.       p1 += p0 - beg0;
  545.     }
  546.  
  547.   /* Record the suffix.  */
  548.   filevec[0].suffix_begin = p0;
  549.   filevec[1].suffix_begin = p1;
  550.  
  551.   /* Calculate number of lines of prefix to save.
  552.  
  553.      prefix_count == 0 means save the whole prefix;
  554.      we need this with for options like -D that output the whole file.
  555.      We also need it for options like -F that output some preceding line;
  556.      at least we will need to find the last few lines,
  557.      but since we don't know how many, it's easiest to find them all.
  558.  
  559.      Otherwise, prefix_count != 0.  Save just prefix_count lines at start
  560.      of the line buffer; they'll be moved to the proper location later.
  561.      Handle 1 more line than the context says (because we count 1 too many),
  562.      rounded up to the next power of 2 to speed index computation.  */
  563.  
  564.   if (no_diff_means_no_output && ! function_regexp.fastmap)
  565.     {
  566.       for (prefix_count = 1;  prefix_count < context + 1;  prefix_count *= 2)
  567.     ;
  568.       prefix_mask = prefix_count - 1;
  569.       alloc_lines0
  570.     = prefix_count
  571.       + GUESS_LINES (0, 0, p0 - filevec[0].prefix_end)
  572.       + context;
  573.     }
  574.   else
  575.     {
  576.       prefix_count = 0;
  577.       prefix_mask = ~0;
  578.       alloc_lines0 = GUESS_LINES (0, 0, n0);
  579.     }
  580.  
  581.   lines = 0;
  582.   linbuf0 = (char const **) xmalloc (alloc_lines0 * sizeof (*linbuf0));
  583.  
  584.   /* If the prefix is needed, find the prefix lines.  */
  585.   if (! (no_diff_means_no_output
  586.      && filevec[0].prefix_end == p0
  587.      && filevec[1].prefix_end == p1))
  588.     {
  589.       p0 = buffer0;
  590.       end0 = filevec[0].prefix_end;
  591.       while (p0 != end0)
  592.     {
  593.       int l = lines++ & prefix_mask;
  594.       if (l == alloc_lines0)
  595.         linbuf0 = (char const **)
  596.           xrealloc (linbuf0, (alloc_lines0 *= 2) * sizeof (*linbuf0));
  597.       linbuf0[l] = p0;
  598.       while (*p0++ != '\n')
  599.         ;
  600.     }
  601.     }
  602.   buffered_prefix = prefix_count && context < lines ? context : lines;
  603.  
  604.   /* Allocate line buffer 1.  */
  605.   tem = prefix_count ? filevec[1].suffix_begin - buffer1 : n1;
  606.  
  607.   alloc_lines1
  608.     = (buffered_prefix
  609.        + GUESS_LINES (lines, filevec[1].prefix_end - buffer1, tem)
  610.        + context);
  611.   linbuf1 = (char const **) xmalloc (alloc_lines1 * sizeof (*linbuf1));
  612.  
  613.   if (buffered_prefix != lines)
  614.     {
  615.       /* Rotate prefix lines to proper location.  */
  616.       for (i = 0;  i < buffered_prefix;  i++)
  617.     linbuf1[i] = linbuf0[(lines - context + i) & prefix_mask];
  618.       for (i = 0;  i < buffered_prefix;  i++)
  619.     linbuf0[i] = linbuf1[i];
  620.     }
  621.  
  622.   /* Initialize line buffer 1 from line buffer 0.  */
  623.   for (i = 0; i < buffered_prefix; i++)
  624.     linbuf1[i] = linbuf0[i] - buffer0 + buffer1;
  625.  
  626.   /* Record the line buffer, adjusted so that
  627.      linbuf*[0] points at the first differing line.  */
  628.   filevec[0].linbuf = linbuf0 + buffered_prefix;
  629.   filevec[1].linbuf = linbuf1 + buffered_prefix;
  630.   filevec[0].linbuf_base = filevec[1].linbuf_base = - buffered_prefix;
  631.   filevec[0].alloc_lines = alloc_lines0 - buffered_prefix;
  632.   filevec[1].alloc_lines = alloc_lines1 - buffered_prefix;
  633.   filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
  634. }
  635.  
  636. /* Largest primes less than some power of two, for nbuckets.  Values range
  637.    from useful to preposterous.  If one of these numbers isn't prime
  638.    after all, don't blame it on me, blame it on primes (6).  */
  639. static int const primes[] =
  640. {
  641.   509,
  642.   1021,
  643.   2039,
  644.   4093,
  645.   8191,
  646.   16381,
  647.   32749,
  648. #if 32767 < INT_MAX
  649.   65521,
  650.   131071,
  651.   262139,
  652.   524287,
  653.   1048573,
  654.   2097143,
  655.   4194301,
  656.   8388593,
  657.   16777213,
  658.   33554393,
  659.   67108859,            /* Preposterously large . . . */
  660.   134217689,
  661.   268435399,
  662.   536870909,
  663.   1073741789,
  664.   2147483647,
  665. #endif
  666.   0
  667. };
  668.  
  669. /* Given a vector of two file_data objects, read the file associated
  670.    with each one, and build the table of equivalence classes.
  671.    Return 1 if either file appears to be a binary file.
  672.    If PRETEND_BINARY is nonzero, pretend they are binary regardless.  */
  673.  
  674. int
  675. read_files (filevec, pretend_binary)
  676.      struct file_data filevec[];
  677.      int pretend_binary;
  678. {
  679.   int i;
  680.   int skip_test = always_text_flag | pretend_binary;
  681.   int appears_binary = pretend_binary | sip (&filevec[0], skip_test);
  682.  
  683.   if (filevec[0].desc != filevec[1].desc)
  684.     appears_binary |= sip (&filevec[1], skip_test | appears_binary);
  685.   else
  686.     {
  687.       filevec[1].buffer = filevec[0].buffer;
  688.       filevec[1].bufsize = filevec[0].bufsize;
  689.       filevec[1].buffered_chars = filevec[0].buffered_chars;
  690.     }
  691.   if (appears_binary)
  692.     {
  693. #if HAVE_SETMODE
  694.       setmode (filevec[0].desc, O_BINARY);
  695.       setmode (filevec[1].desc, O_BINARY);
  696. #endif
  697.       return 1;
  698.     }
  699.  
  700.   find_identical_ends (filevec);
  701.  
  702.   equivs_alloc = filevec[0].alloc_lines + filevec[1].alloc_lines + 1;
  703.   equivs = (struct equivclass *) xmalloc (equivs_alloc * sizeof (struct equivclass));
  704.   /* Equivalence class 0 is permanently safe for lines that were not
  705.      hashed.  Real equivalence classes start at 1.  */
  706.   equivs_index = 1;
  707.  
  708.   for (i = 0;  primes[i] < equivs_alloc / 3;  i++)
  709.     if (! primes[i])
  710.       abort ();
  711.   nbuckets = primes[i];
  712.  
  713.   buckets = (int *) xmalloc ((nbuckets + 1) * sizeof (*buckets));
  714.   bzero (buckets++, (nbuckets + 1) * sizeof (*buckets));
  715.  
  716.   for (i = 0; i < 2; i++)
  717.     find_and_hash_each_line (&filevec[i]);
  718.  
  719.   filevec[0].equiv_max = filevec[1].equiv_max = equivs_index;
  720.  
  721.   free (equivs);
  722.   free (buckets - 1);
  723.  
  724.   return 0;
  725. }
  726.