home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff281.lzh / Diff / analyze.c < prev    next >
C/C++ Source or Header  |  1989-11-20  |  23KB  |  800 lines

  1. /* Analyze file differences 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. /* The basic algorithm is described in: 
  21.    "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
  22.    Algorithmica Vol. 1 No. 2, 1986, p 251.  */
  23.  
  24. #include "regex.h"
  25. #include "diff.h"
  26.  
  27. extern int no_discards;
  28.  
  29. static int *xvec, *yvec;    /* Vectors being compared. */
  30. static int *fdiag;        /* Vector, indexed by diagonal, containing
  31.                    the X coordinate of the point furthest
  32.                    along the given diagonal in the forward
  33.                    search of the edit matrix. */
  34. static int *bdiag;        /* Vector, indexed by diagonal, containing
  35.                    the X coordinate of the point furthest
  36.                    along the given diagonal in the backward
  37.                    search of the edit matrix. */
  38.  
  39. /* Find the midpoint of the shortest edit script for a specified
  40.    portion of the two files.
  41.  
  42.    We scan from the beginnings of the files, and simultaneously from the ends,
  43.    doing a breadth-first search through the space of edit-sequence.
  44.    When the two searches meet, we have found the midpoint of the shortest
  45.    edit sequence.
  46.  
  47.    The value returned is the number of the diagonal on which the midpoint lies.
  48.    The diagonal number equals the number of inserted lines minus the number
  49.    of deleted lines (counting only lines before the midpoint).
  50.    The edit cost is stored into *COST; this is the total number of
  51.    lines inserted or deleted (counting only lines before the midpoint).
  52.  
  53.    This function assumes that the first lines of the specified portions
  54.    of the two files do not match, and likewise that the last lines do not
  55.    match.  The caller must trim matching lines from the beginning and end
  56.    of the portions it is going to specify.
  57.  
  58.    Note that if we return the "wrong" diagonal value, or if
  59.    the value of bdiag at that diagonal is "wrong",
  60.    the worst this can do is cause suboptimal diff output.
  61.    It cannot cause incorrect diff output.  */
  62.  
  63. static int
  64. diag (xoff, xlim, yoff, ylim, cost)
  65.      int xoff, xlim, yoff, ylim;
  66.      int *cost;
  67. {
  68.   int *const fd = fdiag;    /* Give the compiler a chance. */
  69.   int *const bd = bdiag;    /* Additional help for the compiler. */
  70.   int *const xv = xvec;        /* Still more help for the comiler. */
  71.   int *const yv = yvec;        /* And more and more . . . */
  72.   const int dmin = xoff - ylim;    /* Minimum valid diagonal. */
  73.   const int dmax = xlim - yoff;    /* Maximum valid diagonal. */
  74.   const int fmid = xoff - yoff;    /* Center diagonal of top-down search. */
  75.   const int bmid = xlim - ylim;    /* Center diagonal of bottom-up search. */
  76.   int fmin = fmid, fmax = fmid;    /* Limits of top-down search. */
  77.   int bmin = bmid, bmax = bmid;    /* Limits of bottom-up search. */
  78.   int c;            /* Cost. */
  79.   int odd = fmid - bmid & 1;    /* True if southeast corner is on an odd
  80.                    diagonal with respect to the northwest. */
  81.  
  82.   fd[fmid] = xoff;
  83.   bd[bmid] = xlim;
  84.  
  85.   for (c = 1;; ++c)
  86.     {
  87.       int d;            /* Active diagonal. */
  88.       int big_snake = 0;
  89.  
  90.       /* Extend the top-down search by an edit step in each diagonal. */
  91.       fmin > dmin ? fd[--fmin - 1] = -1 : ++fmin;
  92.       fmax < dmax ? fd[++fmax + 1] = -1 : --fmax;
  93.       for (d = fmax; d >= fmin; d -= 2)
  94.     {
  95.       int x, y, oldx, tlo = fd[d - 1], thi = fd[d + 1];
  96.  
  97.       if (tlo >= thi)
  98.         x = tlo + 1;
  99.       else
  100.         x = thi;
  101.       oldx = x;
  102.       y = x - d;
  103.       while (x < xlim && y < ylim && xv[x] == yv[y])
  104.         ++x, ++y;
  105.       if (x - oldx > 20)
  106.         big_snake = 1;
  107.       fd[d] = x;
  108.       if (odd && bmin <= d && d <= bmax && bd[d] <= fd[d])
  109.         {
  110.           *cost = 2 * c - 1;
  111.           return d;
  112.         }
  113.     }
  114.  
  115.       /* Similar extend the bottom-up search. */
  116.       bmin > dmin ? bd[--bmin - 1] = INT_MAX : ++bmin;
  117.       bmax < dmax ? bd[++bmax + 1] = INT_MAX : --bmax;
  118.       for (d = bmax; d >= bmin; d -= 2)
  119.     {
  120.       int x, y, oldx, tlo = bd[d - 1], thi = bd[d + 1];
  121.  
  122.       if (tlo < thi)
  123.         x = tlo;
  124.       else
  125.         x = thi - 1;
  126.       oldx = x;
  127.       y = x - d;
  128.       while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1])
  129.         --x, --y;
  130.       if (oldx - x > 20)
  131.         big_snake = 1;
  132.       bd[d] = x;
  133.       if (!odd && fmin <= d && d <= fmax && bd[d] <= fd[d])
  134.         {
  135.           *cost = 2 * c;
  136.           return d;
  137.         }
  138.     }
  139.  
  140.       /* Heuristic: check occasionally for a diagonal that has made
  141.      lots of progress compared with the edit distance.
  142.      If we have any such, find the one that has made the most
  143.      progress and return it as if it had succeeded.
  144.  
  145.      With this heuristic, for files with a constant small density
  146.      of changes, the algorithm is linear in the file size.  */
  147.  
  148.       if (c > 200 && big_snake && heuristic)
  149.     {
  150.       int best;
  151.       int bestpos;
  152.  
  153.       best = 0;
  154.       for (d = fmax; d >= fmin; d -= 2)
  155.         {
  156.           int dd = d - fmid;
  157.           if ((fd[d] - xoff)*2 - dd > 12 * (c + (dd > 0 ? dd : -dd)))
  158.         {
  159.           if (fd[d] * 2 - dd > best
  160.               && fd[d] - xoff > 20
  161.               && fd[d] - d - yoff > 20)
  162.             {
  163.               int k;
  164.               int x = fd[d];
  165.  
  166.               /* We have a good enough best diagonal;
  167.              now insist that it end with a significant snake.  */
  168.               for (k = 1; k <= 20; k++)
  169.             if (xvec[x - k] != yvec[x - d - k])
  170.               break;
  171.  
  172.               if (k == 21)
  173.             {
  174.               best = fd[d] * 2 - dd;
  175.               bestpos = d;
  176.             }
  177.             }
  178.         }
  179.         }
  180.       if (best > 0)
  181.         {
  182.           *cost = 2 * c - 1;
  183.           return bestpos;
  184.         }
  185.  
  186.       best = 0;
  187.       for (d = bmax; d >= bmin; d -= 2)
  188.         {
  189.           int dd = d - bmid;
  190.           if ((xlim - bd[d])*2 + dd > 12 * (c + (dd > 0 ? dd : -dd)))
  191.         {
  192.           if ((xlim - bd[d]) * 2 + dd > best
  193.               && xlim - bd[d] > 20
  194.               && ylim - (bd[d] - d) > 20)
  195.             {
  196.               /* We have a good enough best diagonal;
  197.              now insist that it end with a significant snake.  */
  198.               int k;
  199.               int x = bd[d];
  200.  
  201.               for (k = 0; k < 20; k++)
  202.             if (xvec[x + k] != yvec[x - d + k])
  203.               break;
  204.               if (k == 20)
  205.             {
  206.               best = (xlim - bd[d]) * 2 + dd;
  207.               bestpos = d;
  208.             }
  209.             }
  210.         }
  211.         }
  212.       if (best > 0)
  213.         {
  214.           *cost = 2 * c - 1;
  215.           return bestpos;
  216.         }
  217.     }
  218.     }
  219. }
  220.  
  221. /* Compare in detail contiguous subsequences of the two files
  222.    which are known, as a whole, to match each other.
  223.  
  224.    The results are recorded in the vectors files[N].changed_flag, by
  225.    storing a 1 in the element for each line that is an insertion or deletion.
  226.  
  227.    The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
  228.  
  229.    Note that XLIM, YLIM are exclusive bounds.
  230.    All line numbers are origin-0 and discarded lines are not counted.  */
  231.  
  232. static void
  233. compareseq (xoff, xlim, yoff, ylim)
  234.      int xoff, xlim, yoff, ylim;
  235. {
  236.   /* Slide down the bottom initial diagonal. */
  237.   while (xoff < xlim && yoff < ylim && xvec[xoff] == yvec[yoff])
  238.     ++xoff, ++yoff;
  239.   /* Slide up the top initial diagonal. */
  240.   while (xlim > xoff && ylim > yoff && xvec[xlim - 1] == yvec[ylim - 1])
  241.     --xlim, --ylim;
  242.   
  243.   /* Handle simple cases. */
  244.   if (xoff == xlim)
  245.     while (yoff < ylim)
  246.       files[1].changed_flag[files[1].realindexes[yoff++]] = 1;
  247.   else if (yoff == ylim)
  248.     while (xoff < xlim)
  249.       files[0].changed_flag[files[0].realindexes[xoff++]] = 1;
  250.   else
  251.     {
  252.       int c, d, f, b;
  253.  
  254.       /* Find a point of correspondence in the middle of the files.  */
  255.  
  256.       d = diag (xoff, xlim, yoff, ylim, &c);
  257.       f = fdiag[d];
  258.       b = bdiag[d];
  259.  
  260.       if (c == 1)
  261.     {
  262.       /* This should be impossible, because it implies that
  263.          one of the two subsequences is empty,
  264.          and that case was handled above without calling `diag'.
  265.          Let's verify that this is true.  */
  266.       abort ();
  267. #if 0
  268.       /* The two subsequences differ by a single insert or delete;
  269.          record it and we are done.  */
  270.       if (d < xoff - yoff)
  271.         files[1].changed_flag[files[1].realindexes[b - d - 1]] = 1;
  272.       else
  273.         files[0].changed_flag[files[0].realindexes[b]] = 1;
  274. #endif
  275.     }
  276.       else
  277.     {
  278.       /* Use that point to split this problem into two subproblems.  */
  279.       compareseq (xoff, b, yoff, b - d);
  280.       /* This used to use f instead of b,
  281.          but that is incorrect!
  282.          It is not necessarily the case that diagonal d
  283.          has a snake from b to f.  */
  284.       compareseq (b, xlim, b - d, ylim);
  285.     }
  286.     }
  287. }
  288.  
  289. /* Discard lines from one file that have no matches in the other file.
  290.  
  291.    A line which is discarded will not be considered by the actual
  292.    comparison algorithm; it will be as if that line were not in the file.
  293.    The file's `realindexes' table maps virtual line numbers
  294.    (which don't count the discarded lines) into real line numbers;
  295.    this is how the actual comparison algorithm produces results
  296.    that are comprehensible when the discarded lines are counted.
  297.  
  298.    When we discard a line, we also mark it as a deletion or insertion
  299.    so that it will be printed in the output.  */
  300.  
  301. void
  302. discard_confusing_lines (filevec)
  303.      struct file_data filevec[];
  304. {
  305.   int f, i, j;
  306.   char *discarded[2];
  307.   int *equiv_count[2];
  308.  
  309.   /* Allocate our results.  */
  310.   for (f = 0; f < 2; f++)
  311.     {
  312.       filevec[f].undiscarded
  313.     = (int *) xmalloc (filevec[f].buffered_lines * sizeof (int));
  314.       filevec[f].realindexes
  315.     = (int *) xmalloc (filevec[f].buffered_lines * sizeof (int));
  316.     }
  317.  
  318.   /* Set up equiv_count[F][I] as the number of lines in file F
  319.      that fall in equivalence class I.  */
  320.  
  321.   equiv_count[0] = (int *) xmalloc (filevec[0].equiv_max * sizeof (int));
  322.   bzero (equiv_count[0], filevec[0].equiv_max * sizeof (int));
  323.   equiv_count[1] = (int *) xmalloc (filevec[1].equiv_max * sizeof (int));
  324.   bzero (equiv_count[1], filevec[1].equiv_max * sizeof (int));
  325.  
  326.   for (i = 0; i < filevec[0].buffered_lines; ++i)
  327.     ++equiv_count[0][filevec[0].equivs[i]];
  328.   for (i = 0; i < filevec[1].buffered_lines; ++i)
  329.     ++equiv_count[1][filevec[1].equivs[i]];
  330.  
  331.   /* Set up tables of which lines are going to be discarded.  */
  332.  
  333.   discarded[0] = (char *) xmalloc (filevec[0].buffered_lines);
  334.   discarded[1] = (char *) xmalloc (filevec[1].buffered_lines);
  335.   bzero (discarded[0], filevec[0].buffered_lines);
  336.   bzero (discarded[1], filevec[1].buffered_lines);
  337.  
  338.   /* Mark to be discarded each line that matches no line of the other file.
  339.      If a line matches many lines, mark it as provisionally discardable.  */
  340.  
  341.   for (f = 0; f < 2; f++)
  342.     {
  343.       int end = filevec[f].buffered_lines;
  344.       char *discards = discarded[f];
  345.       int *counts = equiv_count[1 - f];
  346.       int *equivs = filevec[f].equivs;
  347.       for (i = 0; i < end; i++)
  348.     {
  349.       int nmatch = counts[equivs[i]];
  350.       if (equivs[i] == 0)
  351.         continue;
  352.       if (nmatch == 0)
  353.         discards[i] = 1;
  354.       else if (nmatch > 5)
  355.         discards[i] = 2;
  356.     }
  357.     }
  358.  
  359.   /* Don't really discard the provisional lines if there are less than ten
  360.      discardable lines in a row.  */
  361.  
  362.   for (f = 0; f < 2; f++)
  363.     {
  364.       int end = filevec[f].buffered_lines;
  365.       char *discards = discarded[f];
  366.  
  367.       for (i = 0; i < end; i++)
  368.     if (discards[i])
  369.       {
  370.         register int j;
  371.         int length;
  372.         int provisional = 0;
  373.  
  374.         /* Cancel provisional discards at the beginning.  */
  375.         while (i < end && discards[i] == 2)
  376.           discards[i] = 0;
  377.  
  378.         /* Find end of this run of discardable lines.
  379.            Count how many are provisionally discardable.  */
  380.         for (j = i; j < end; j++)
  381.           {
  382.         if (discards[j] == 0)
  383.           break;
  384.         if (discards[j] == 2)
  385.           ++provisional;
  386.           }
  387.  
  388.         /* Cancel provisional discards at the end, and shrink the run.  */
  389.         while (j > i && discards[j - 1] == 2)
  390.           discards[j - 1] = 0, --provisional;
  391.  
  392.         /* Now we have the length of a run of discardable lines
  393.            whose first and last are not provisional.  */
  394.         length = j - i;
  395.  
  396.         /* If half the lines in the run are provisional,
  397.            cancel discarding of all provisional lines in the run.  */
  398.         if (provisional * 2 > length)
  399.           {
  400.         while (j > i)
  401.           if (discards[--j] == 2)
  402.             discards[j] = 0;
  403.           }
  404.         else
  405.           {
  406.         /* Cancel provisional discards that are near either end.  */
  407.         for (j = 0; j < 5 && j < length; j++)
  408.           if (discards[i + j] == 2)
  409.             discards[i + j] = 0;
  410.         /* Meanwhile, I advances to the last line of the run.  */
  411.         i += length - 1;
  412.         length -= 5;
  413.         for (j = 0; j < 5 && j < length; j++)
  414.           if (discards[i - j] == 2)
  415.             discards[i - j] = 0;
  416.           }
  417.       }
  418.     }
  419.  
  420.   /* Discard from file 0. */
  421.   for (f = 0; f < 2; f++)
  422.     {
  423.       char *discards = discarded[f];
  424.       int end = filevec[f].buffered_lines;
  425.       j = 0;
  426.       for (i = 0; i < end; ++i)
  427.     if (no_discards || discards[i] == 0)
  428.       {
  429.         filevec[f].undiscarded[j] = filevec[f].equivs[i];
  430.         filevec[f].realindexes[j++] = i;
  431.       }
  432.     else
  433.       filevec[f].changed_flag[i] = 1;
  434.       filevec[f].nondiscarded_lines = j;
  435.     }
  436.  
  437.   free (discarded[1]);
  438.   free (discarded[0]);
  439.   free (equiv_count[1]);
  440.   free (equiv_count[0]);
  441. }
  442.  
  443. /* Adjust inserts/deletes of blank lines to join changes
  444.    as much as possible.
  445.  
  446.    We do something when a run of changed lines include a blank
  447.    line at one end and have an excluded blank line at the other.
  448.    We are free to choose which blank line is included.
  449.    `compareseq' always chooses the one at the beginning,
  450.    but usually it is cleaner to consider the following blank line
  451.    to be the "change".  The only exception is if the preceding blank line
  452.    would join this change to other changes.  */
  453.  
  454. int inhibit;
  455.  
  456. static void
  457. shift_boundaries (filevec)
  458.      struct file_data filevec[];
  459. {
  460.   int f;
  461.  
  462.   if (inhibit)
  463.     return;
  464.  
  465.   for (f = 0; f < 2; f++)
  466.     {
  467.       char *changed = filevec[f].changed_flag;
  468.       char *other_changed = filevec[1-f].changed_flag;
  469.       int i = 0;
  470.       int j = 0;
  471.       int i_end = filevec[f].buffered_lines;
  472.       int preceeding = -1;
  473.       int other_preceeding = -1;
  474.  
  475.       while (1)
  476.     {
  477.       int start, end, other_start;
  478.  
  479.       /* Scan forwards to find beginning of another run of changes.
  480.          Also keep track of the corresponding point in the other file.  */
  481.  
  482.       while (i < i_end && changed[i] == 0)
  483.         {
  484.           while (other_changed[j++])
  485.         /* Non-corresponding lines in the other file
  486.            will count as the preceeding batch of changes.  */
  487.         other_preceeding = j;
  488.           i++;
  489.         }
  490.  
  491.       if (i == i_end)
  492.         break;
  493.  
  494.       start = i;
  495.       other_start = j;
  496.  
  497.       while (1)
  498.         {
  499.           /* Now find the end of this run of changes.  */
  500.  
  501.           while (i < i_end && changed[i] != 0) i++;
  502.           end = i;
  503.  
  504.           /* If the first changed line matches the following unchanged one,
  505.          and this run does not follow right after a previous run,
  506.          and there are no lines deleted from the other file here,
  507.          then classify the first changed line as unchanged
  508.          and the following line as changed in its place.  */
  509.  
  510.           /* You might ask, how could this run follow right after another?
  511.          Only because the previous run was shifted here.  */
  512.  
  513.           if (files[f].equivs[start] == files[f].equivs[end]
  514.           && !other_changed[j]
  515.           && end != i_end
  516.           && !((preceeding >= 0 && start == preceeding)
  517.                || (other_preceeding >= 0
  518.                && other_start == other_preceeding)))
  519.         {
  520.           changed[end++] = 1;
  521.           changed[start++] = 0;
  522.           ++i;
  523.           /* Since one line-that-matches is now before this run
  524.              instead of after, we must advance in the other file
  525.              to keep in synch.  */
  526.           ++j;
  527.         }
  528.           else
  529.         break;
  530.         }
  531.  
  532.       preceeding = i;
  533.       other_preceeding = j;
  534.     }
  535.     }
  536. }
  537.  
  538. /* Cons an additional entry onto the front of an edit script OLD.
  539.    LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  540.    DELETED is the number of lines deleted here from file 0.
  541.    INSERTED is the number of lines inserted here in file 1.
  542.  
  543.    If DELETED is 0 then LINE0 is the number of the line before
  544.    which the insertion was done; vice versa for INSERTED and LINE1.  */
  545.  
  546. static struct change *
  547. add_change (line0, line1, deleted, inserted, old)
  548.      int line0, line1, deleted, inserted;
  549.      struct change *old;
  550. {
  551.   struct change *new = (struct change *) xmalloc (sizeof (struct change));
  552.  
  553.   new->line0 = line0;
  554.   new->line1 = line1;
  555.   new->inserted = inserted;
  556.   new->deleted = deleted;
  557.   new->link = old;
  558.   return new;
  559. }
  560.  
  561. /* Scan the tables of which lines are inserted and deleted,
  562.    producing an edit script in reverse order.  */
  563.  
  564. static struct change *
  565. build_reverse_script (filevec)
  566.      struct file_data filevec[];
  567. {
  568.   struct change *script = 0;
  569.   char *changed0 = filevec[0].changed_flag;
  570.   char *changed1 = filevec[1].changed_flag;
  571.   int len0 = filevec[0].buffered_lines;
  572.   int len1 = filevec[1].buffered_lines;
  573.  
  574.   /* Note that changedN[len0] does exist, and contains 0.  */
  575.  
  576.   int i0 = 0, i1 = 0;
  577.  
  578.   while (i0 < len0 || i1 < len1)
  579.     {
  580.       if (changed0[i0] || changed1[i1])
  581.     {
  582.       int line0 = i0, line1 = i1;
  583.  
  584.       /* Find # lines changed here in each file.  */
  585.       while (changed0[i0]) ++i0;
  586.       while (changed1[i1]) ++i1;
  587.  
  588.       /* Record this change.  */
  589.       script = add_change (line0, line1, i0 - line0, i1 - line1, script);
  590.     }
  591.  
  592.       /* We have reached lines in the two files that match each other.  */
  593.       i0++, i1++;
  594.     }
  595.  
  596.   return script;
  597. }
  598.  
  599. /* Scan the tables of which lines are inserted and deleted,
  600.    producing an edit script in forward order.  */
  601.  
  602. static struct change *
  603. build_script (filevec)
  604.      struct file_data filevec[];
  605. {
  606.   struct change *script = 0;
  607.   char *changed0 = filevec[0].changed_flag;
  608.   char *changed1 = filevec[1].changed_flag;
  609.   int len0 = filevec[0].buffered_lines;
  610.   int len1 = filevec[1].buffered_lines;
  611.  
  612.   /* Note that changedN[-1] does exist, and contains 0.  */
  613.  
  614.   int i0 = len0, i1 = len1;
  615.  
  616.   while (i0 >= 0 || i1 >= 0)
  617.     {
  618.       if (changed0[i0 - 1] || changed1[i1 - 1])
  619.     {
  620.       int line0 = i0, line1 = i1;
  621.  
  622.       /* Find # lines changed here in each file.  */
  623.       while (changed0[i0 - 1]) --i0;
  624.       while (changed1[i1 - 1]) --i1;
  625.  
  626.       /* Record this change.  */
  627.       script = add_change (i0, i1, line0 - i0, line1 - i1, script);
  628.     }
  629.  
  630.       /* We have reached lines in the two files that match each other.  */
  631.       i0--, i1--;
  632.     }
  633.  
  634.   return script;
  635. }
  636.  
  637. /* Report the differences of two files.  DEPTH is the current directory
  638.    depth. */
  639. int
  640. diff_2_files (filevec, depth)
  641.      struct file_data filevec[];
  642.      int depth;
  643. {
  644.   int diags;
  645.   int i;
  646.   struct change *e, *p;
  647.   struct change *script;
  648.   int binary;
  649.  
  650.   /* See if the two named files are actually the same physical file.
  651.      If so, we know they are identical without actually reading them.  */
  652.  
  653. #ifndef AMIGA
  654.   if (filevec[0].stat.st_ino == filevec[1].stat.st_ino
  655.       && filevec[0].stat.st_dev == filevec[1].stat.st_dev)
  656.     return 0;
  657. #endif
  658.  
  659.   binary = read_files (filevec);
  660.  
  661.   /* If we have detected that file 0 is a binary file,
  662.      compare the two files as binary.  This can happen
  663.      only when the first chunk is read.  */
  664.  
  665.   if (binary)
  666.     {
  667.       int differs = (filevec[0].buffered_chars != filevec[1].buffered_chars
  668.              || bcmp (filevec[0].buffer, filevec[1].buffer,
  669.                   filevec[1].buffered_chars));
  670.       if (differs) 
  671.     message ("Binary files %s and %s differ\n",
  672.          filevec[0].name, filevec[1].name);
  673.  
  674.       for (i = 0; i < 2; ++i)
  675.     if (filevec[i].buffer)
  676.       free (filevec[i].buffer);
  677.       return differs;
  678.     }
  679.  
  680.   if (filevec[0].missing_newline != filevec[1].missing_newline)
  681.     {
  682.       if (filevec[0].missing_newline)
  683.     error ("No newline at end of file %s\n", filevec[0].name);
  684.       if (filevec[1].missing_newline)
  685.     error ("No newline at end of file %s\n", filevec[1].name);
  686.     }
  687.  
  688.   /* Allocate vectors for the results of comparison:
  689.      a flag for each line of each file, saying whether that line
  690.      is an insertion or deletion.
  691.      Allocate an extra element, always zero, at each end of each vector.  */
  692.  
  693.   filevec[0].changed_flag = (char *) xmalloc (filevec[0].buffered_lines + 2);
  694.   filevec[1].changed_flag = (char *) xmalloc (filevec[1].buffered_lines + 2);
  695.   bzero (filevec[0].changed_flag, filevec[0].buffered_lines + 2);
  696.   bzero (filevec[1].changed_flag, filevec[1].buffered_lines + 2);
  697.   filevec[0].changed_flag++;
  698.   filevec[1].changed_flag++;
  699.  
  700.   /* Some lines are obviously insertions or deletions
  701.      because they don't match anything.  Detect them now,
  702.      and avoid even thinking about them in the main comparison algorithm.  */
  703.  
  704.   discard_confusing_lines (filevec);
  705.  
  706.   /* Now do the main comparison algorithm, considering just the
  707.      undiscarded lines.  */
  708.  
  709.   xvec = filevec[0].undiscarded;
  710.   yvec = filevec[1].undiscarded;
  711.   diags = filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines + 3;
  712.   fdiag = (int *) xmalloc (diags * sizeof (int));
  713.   fdiag += filevec[1].nondiscarded_lines + 1;
  714.   bdiag = (int *) xmalloc (diags * sizeof (int));
  715.   bdiag += filevec[1].nondiscarded_lines + 1;
  716.  
  717.   files[0] = filevec[0];
  718.   files[1] = filevec[1];
  719.  
  720.   compareseq (0, filevec[0].nondiscarded_lines,
  721.           0, filevec[1].nondiscarded_lines);
  722.  
  723.   bdiag -= filevec[1].nondiscarded_lines + 1;
  724.   free (bdiag);
  725.   fdiag -= filevec[1].nondiscarded_lines + 1;
  726.   free (fdiag);
  727.  
  728.   /* Modify the results slightly to make them prettier
  729.      in cases where that can validly be done.  */
  730.  
  731.   shift_boundaries (filevec);
  732.  
  733.   /* Get the results of comparison in the form of a chain
  734.      of `struct change's -- an edit script.  */
  735.  
  736.   if (output_style == OUTPUT_ED)
  737.     script = build_reverse_script (filevec);
  738.   else
  739.     script = build_script (filevec);
  740.  
  741.   if (script)
  742.     {
  743.       setup_output (files[0].name, files[1].name, depth);
  744.       if (output_style == OUTPUT_CONTEXT)
  745.     print_context_header (files);
  746.  
  747.       switch (output_style)
  748.     {
  749.     case OUTPUT_CONTEXT:
  750.       print_context_script (script);
  751.       break;
  752.  
  753.     case OUTPUT_ED:
  754.       print_ed_script (script);
  755.       break;
  756.  
  757.     case OUTPUT_FORWARD_ED:
  758.       pr_forward_ed_script (script);
  759.       break;
  760.  
  761.     case OUTPUT_RCS:
  762.       print_rcs_script (script);
  763.       break;
  764.  
  765.     case OUTPUT_NORMAL:
  766.       print_normal_script (script);
  767.       break;
  768.     }
  769.  
  770.       finish_output ();
  771.     }
  772.  
  773.   for (i = 1; i >= 0; --i)
  774.     {
  775.       free (filevec[i].realindexes);
  776.       free (filevec[i].undiscarded);
  777.     }
  778.  
  779.   for (i = 1; i >= 0; --i)
  780.     free (--filevec[i].changed_flag);
  781.  
  782.   for (i = 1; i >= 0; --i)
  783.     free (filevec[i].equivs);
  784.  
  785.   for (i = 0; i < 2; ++i)
  786.     {
  787.       if (filevec[i].buffer != 0)
  788.     free (filevec[i].buffer);
  789.       free (filevec[i].linbuf);
  790.     }
  791.  
  792.   for (e = script; e; e = p)
  793.     {
  794.       p = e->link;
  795.       free (e);
  796.     }
  797.  
  798.   return script ? 1 : 0;
  799. }
  800.