home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / diff20.zip / diff-2.0 / io.c < prev    next >
C/C++ Source or Header  |  1992-09-15  |  19KB  |  684 lines

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