home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / gnucdiff / analyze.c next >
Encoding:
C/C++ Source or Header  |  1988-12-16  |  23.7 KB  |  836 lines

  1. /* Analyze file differences for GNU DIFF.
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU DIFF General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU DIFF, but only under the conditions described in the
  15. GNU DIFF General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU DIFF so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21. /* The basic algorithm is described in: 
  22.    "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
  23.    Algorithmica Vol. 1 No. 2, 1986, p 251.  */
  24.  
  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.  
  119.       for (d = bmax; d >= bmin; d -= 2)
  120.     {
  121.       int x, y, oldx, tlo = bd[d - 1], thi = bd[d + 1];
  122.  
  123.       if (tlo < thi)
  124.         x = tlo;
  125.       else
  126.         x = thi - 1;
  127.       oldx = x;
  128.       y = x - d;
  129.       while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1])
  130.         --x, --y;
  131.       if (oldx - x > 20)
  132.         big_snake = 1;
  133.       bd[d] = x;
  134.       if (!odd && fmin <= d && d <= fmax && bd[d] <= fd[d])
  135.         {
  136.           *cost = 2 * c;
  137.           return d;
  138.         }
  139.     }
  140.  
  141.       /* Heuristic: check occasionally for a diagonal that has made
  142.      lots of progress compared with the edit distance.
  143.      If we have any such, find the one that has made the most
  144.      progress and return it as if it had succeeded.
  145.  
  146.      With this heuristic, for files with a constant small density
  147.      of changes, the algorithm is linear in the file size.  */
  148.  
  149.       if (c > 200 && big_snake && heuristic)
  150.     {
  151.       int best;
  152.       int bestpos;
  153.  
  154.       best = 0;
  155.       for (d = fmax; d >= fmin; d -= 2)
  156.         {
  157.           int dd = d - fmid;
  158.           if ((fd[d] - xoff)*2 - dd > 12 * (c + (dd > 0 ? dd : -dd)))
  159.         {
  160.           if (fd[d] * 2 - dd > best
  161.               && fd[d] - xoff > 20
  162.               && fd[d] - d - yoff > 20)
  163.             {
  164.               int k;
  165.               int x = fd[d];
  166.  
  167.               /* We have a good enough best diagonal;
  168.              now insist that it end with a significant snake.  */
  169.               for (k = 1; k <= 20; k++)
  170.             if (xvec[x - k] != yvec[x - d - k])
  171.               break;
  172.  
  173.               if (k == 21)
  174.             {
  175.               best = fd[d] * 2 - dd;
  176.               bestpos = d;
  177.             }
  178.             }
  179.         }
  180.         }
  181.       if (best > 0)
  182.         {
  183.           *cost = 2 * c - 1;
  184.           return bestpos;
  185.         }
  186.  
  187.       best = 0;
  188.       for (d = bmax; d >= bmin; d -= 2)
  189.         {
  190.           int dd = d - bmid;
  191.           if ((xlim - bd[d])*2 + dd > 12 * (c + (dd > 0 ? dd : -dd)))
  192.         {
  193.           if ((xlim - bd[d]) * 2 + dd > best
  194.               && xlim - bd[d] > 20
  195.               && ylim - (bd[d] - d) > 20)
  196.             {
  197.               /* We have a good enough best diagonal;
  198.              now insist that it end with a significant snake.  */
  199.               int k;
  200.               int x = bd[d];
  201.  
  202.               for (k = 0; k < 20; k++)
  203.             if (xvec[x + k] != yvec[x - d + k])
  204.               break;
  205.               if (k == 20)
  206.             {
  207.               best = (xlim - bd[d]) * 2 + dd;
  208.               bestpos = d;
  209.             }
  210.             }
  211.         }
  212.         }
  213.       if (best > 0)
  214.         {
  215.           *cost = 2 * c - 1;
  216.           return bestpos;
  217.         }
  218.     }
  219.     }
  220. }
  221.  
  222. /* Compare in detail contiguous subsequences of the two files
  223.    which are known, as a whole, to match each other.
  224.  
  225.    The results are recorded in the vectors files[N].changed_flag, by
  226.    storing a 1 in the element for each line that is an insertion or deletion.
  227.  
  228.    The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
  229.  
  230.    Note that XLIM, YLIM are exclusive bounds.
  231.    All line numbers are origin-0 and discarded lines are not counted.  */
  232.  
  233. static void
  234. compareseq (xoff, xlim, yoff, ylim)
  235.      int xoff, xlim, yoff, ylim;
  236. {
  237.   /* Slide down the bottom initial diagonal. */
  238.   while (xoff < xlim && yoff < ylim && xvec[xoff] == yvec[yoff])
  239.     ++xoff, ++yoff;
  240.   /* Slide up the top initial diagonal. */
  241.   while (xlim > xoff && ylim > yoff && xvec[xlim - 1] == yvec[ylim - 1])
  242.     --xlim, --ylim;
  243.   
  244.   /* Handle simple cases. */
  245.   if (xoff == xlim)
  246.     while (yoff < ylim)
  247.       files[1].changed_flag[files[1].realindexes[yoff++]] = 1;
  248.   else if (yoff == ylim)
  249.     while (xoff < xlim)
  250.       files[0].changed_flag[files[0].realindexes[xoff++]] = 1;
  251.   else
  252.     {
  253.       int c, d, f, b;
  254.  
  255.       /* Find a point of correspondence in the middle of the files.  */
  256.  
  257.       d = diag (xoff, xlim, yoff, ylim, &c);
  258.       f = fdiag[d];
  259.       b = bdiag[d];
  260.  
  261.       if (c == 1)
  262.     {
  263.       /* This should be impossible, because it implies that
  264.          one of the two subsequences is empty,
  265.          and that case was handled above without calling `diag'.
  266.          Let's verify that this is true.  */
  267.       abort ();
  268. #if 0
  269.       /* The two subsequences differ by a single insert or delete;
  270.          record it and we are done.  */
  271.       if (d < xoff - yoff)
  272.         files[1].changed_flag[files[1].realindexes[b - d - 1]] = 1;
  273.       else
  274.         files[0].changed_flag[files[0].realindexes[b]] = 1;
  275. #endif
  276.     }
  277.       else
  278.     {
  279.       /* Use that point to split this problem into two subproblems.  */
  280.       compareseq (xoff, b, yoff, b - d);
  281.       /* This used to use f instead of b,
  282.          but that is incorrect!
  283.          It is not necessarily the case that diagonal d
  284.          has a snake from b to f.  */
  285.       compareseq (b, xlim, b - d, ylim);
  286.     }
  287.     }
  288. }
  289.  
  290. /* Discard lines from one file that have no matches in the other file.
  291.  
  292.    A line which is discarded will not be considered by the actual
  293.    comparison algorithm; it will be as if that line were not in the file.
  294.    The file's `realindexes' table maps virtual line numbers
  295.    (which don't count the discarded lines) into real line numbers;
  296.    this is how the actual comparison algorithm produces results
  297.    that are comprehensible when the discarded lines are counted.
  298.  
  299.    When we discard a line, we also mark it as a deletion or insertion
  300.    so that it will be printed in the output.  */
  301.  
  302. void
  303. discard_confusing_lines (filevec)
  304.      struct file_data filevec[];
  305. {
  306.   int f, i, j, k;
  307.   char *discarded[2];
  308.   int *equiv_count[2];
  309.  
  310.   /* Allocate our results.  */
  311.   for (f = 0; f < 2; f++)
  312.     {
  313.       filevec[f].undiscarded
  314.     = (int *) xmalloc (filevec[f].buffered_lines * sizeof (int));
  315.       filevec[f].realindexes
  316.     = (int *) xmalloc (filevec[f].buffered_lines * sizeof (int));
  317.     }
  318.  
  319.   /* Set up equiv_count[F][I] as the number of lines in file F
  320.      that fall in equivalence class I.  */
  321.  
  322.   equiv_count[0] = (int *) xmalloc (filevec[0].equiv_max * sizeof (int));
  323.   bzero (equiv_count[0], filevec[0].equiv_max * sizeof (int));
  324.   equiv_count[1] = (int *) xmalloc (filevec[1].equiv_max * sizeof (int));
  325.   bzero (equiv_count[1], filevec[1].equiv_max * sizeof (int));
  326.  
  327.   for (i = 0; i < filevec[0].buffered_lines; ++i)
  328.     ++equiv_count[0][filevec[0].equivs[i]];
  329.   for (i = 0; i < filevec[1].buffered_lines; ++i)
  330.     ++equiv_count[1][filevec[1].equivs[i]];
  331.  
  332.   /* Set up tables of which lines are going to be discarded.  */
  333.  
  334.   discarded[0] = (char *) xmalloc (filevec[0].buffered_lines);
  335.   discarded[1] = (char *) xmalloc (filevec[1].buffered_lines);
  336.   bzero (discarded[0], filevec[0].buffered_lines);
  337.   bzero (discarded[1], filevec[1].buffered_lines);
  338.  
  339.   /* Mark to be discarded each line that matches no line of the other file.
  340.      If a line matches many lines, mark it as provisionally discardable.  */
  341.  
  342.   for (f = 0; f < 2; f++)
  343.     {
  344.       int end = filevec[f].buffered_lines;
  345.       char *discards = discarded[f];
  346.       int *counts = equiv_count[1 - f];
  347.       int *equivs = filevec[f].equivs;
  348.       for (i = 0; i < end; i++)
  349.     {
  350.       int nmatch = counts[equivs[i]];
  351.       if (equivs[i] == 0)
  352.         continue;
  353.       if (nmatch == 0)
  354.         discards[i] = 1;
  355.       else if (nmatch > 5)
  356.         discards[i] = 2;
  357.     }
  358.     }
  359.  
  360.   /* Don't really discard the provisional lines if there are less than ten
  361.      discardable lines in a row.  */
  362.  
  363.   for (f = 0; f < 2; f++)
  364.     {
  365.       int end = filevec[f].buffered_lines;
  366.       char *discards = discarded[f];
  367.  
  368.       for (i = 0; i < end; i++)
  369.     if (discards[i])
  370.       {
  371.         register int j;
  372.         int length;
  373.         int provisional = 0;
  374.  
  375.         /* Cancel provisional discards at the beginning.  */
  376.         while (i < end && discards[i] == 2)
  377.           discards[i] = 0;
  378.  
  379.         /* Find end of this run of discardable lines.
  380.            Count how many are provisionally discardable.  */
  381.         for (j = i; j < end; j++)
  382.           {
  383.         if (discards[j] == 0)
  384.           break;
  385.         if (discards[j] == 2)
  386.           ++provisional;
  387.           }
  388.  
  389.         /* Cancel provisional discards at the end, and shrink the run.  */
  390. #if    defined(MSC_BUG)
  391.         while (j > i && discards[j - 1] == 2)
  392.           discards[j - 1] = 0, --provisional;
  393. #else
  394.         {
  395.             int index = j - 1;
  396.           while (j > i && discards[index] == 2)
  397.             discards[index] = 0, --provisional;
  398.         }
  399. #endif
  400.  
  401.         /* Now we have the length of a run of discardable lines
  402.            whose first and last are not provisional.  */
  403.         length = j - i;
  404.  
  405.         /* If half the lines in the run are provisional,
  406.            cancel discarding of all provisional lines in the run.  */
  407.         if (provisional * 2 > length)
  408.           {
  409.         while (j > i)
  410.           if (discards[--j] == 2)
  411.             discards[j] = 0;
  412.           }
  413.         else
  414.           {
  415.         /* Cancel provisional discards that are near either end.  */
  416.         for (j = 0; j < 5 && j < length; j++)
  417.           if (discards[i + j] == 2)
  418.             discards[i + j] = 0;
  419.         /* Meanwhile, I advances to the last line of the run.  */
  420.         i += length - 1;
  421.         length -= 5;
  422.         for (j = 0; j < 5 && j < length; j++)
  423.           if (discards[i - j] == 2)
  424.             discards[i - j] = 0;
  425.           }
  426.       }
  427.     }
  428.  
  429.   /* Discard from file 0. */
  430.   for (f = 0; f < 2; f++)
  431.     {
  432.       char *discards = discarded[f];
  433.       int end = filevec[f].buffered_lines;
  434.       j = 0;
  435.       for (i = 0; i < end; ++i)
  436.     if (no_discards || discards[i] == 0)
  437.       {
  438.         filevec[f].undiscarded[j] = filevec[f].equivs[i];
  439.         filevec[f].realindexes[j++] = i;
  440.       }
  441.     else
  442.       filevec[f].changed_flag[i] = 1;
  443.       filevec[f].nondiscarded_lines = j;
  444.     }
  445.  
  446.   free (discarded[1]);
  447.   free (discarded[0]);
  448.   free (equiv_count[1]);
  449.   free (equiv_count[0]);
  450. }
  451.  
  452. /* Adjust inserts/deletes of blank lines to join changes
  453.    as much as possible.
  454.  
  455.    We do something when a run of changed lines include a blank
  456.    line at one end and have an excluded blank line at the other.
  457.    We are free to choose which blank line is included.
  458.    `compareseq' always chooses the one at the beginning,
  459.    but usually it is cleaner to consider the following blank line
  460.    to be the "change".  The only exception is if the preceding blank line
  461.    would join this change to other changes.  */
  462.  
  463. int inhibit;
  464.  
  465. static void
  466. shift_boundaries (filevec)
  467.      struct file_data filevec[];
  468. {
  469.   int f;
  470.  
  471.   if (inhibit)
  472.     return;
  473.  
  474.   for (f = 0; f < 2; f++)
  475.     {
  476.       char *changed = filevec[f].changed_flag;
  477.       char *other_changed = filevec[1-f].changed_flag;
  478.       int i = 0;
  479.       int j = 0;
  480.       int i_end = filevec[f].buffered_lines;
  481.       int j_end = filevec[1-f].buffered_lines;
  482.       int preceeding = -1;
  483.       int other_preceeding = -1;
  484.  
  485.       while (1)
  486.     {
  487.       int start, end, other_start;
  488.  
  489.       /* Scan forwards to find beginning of another run of changes.
  490.          Also keep track of the corresponding point in the other file.  */
  491.  
  492.       while (i < i_end && changed[i] == 0)
  493.         {
  494.           while (other_changed[j++])
  495.         /* Non-corresponding lines in the other file
  496.            will count as the preceeding batch of changes.  */
  497.         other_preceeding = j;
  498.           i++;
  499.         }
  500.  
  501.       if (i == i_end)
  502.         break;
  503.  
  504.       start = i;
  505.       other_start = j;
  506.  
  507.       while (1)
  508.         {
  509.           /* Now find the end of this run of changes.  */
  510.  
  511.           while (i < i_end && changed[i] != 0) i++;
  512.           end = i;
  513.  
  514.           /* If the first changed line matches the following unchanged one,
  515.          and this run does not follow right after a previous run,
  516.          and there are no lines deleted from the other file here,
  517.          then classify the first changed line as unchanged
  518.          and the following line as changed in its place.  */
  519.  
  520.           /* You might ask, how could this run follow right after another?
  521.          Only because the previous run was shifted here.  */
  522.  
  523.           if (files[f].equivs[start] == files[f].equivs[end]
  524.           && !other_changed[j]
  525.           && end != i_end
  526.           && !((preceeding >= 0 && start == preceeding)
  527.                || (other_preceeding >= 0
  528.                && other_start == other_preceeding)))
  529.         {
  530.           changed[end++] = 1;
  531.           changed[start++] = 0;
  532.           ++i;
  533.           /* Since one line-that-matches is now before this run
  534.              instead of after, we must advance in the other file
  535.              to keep in synch.  */
  536.           ++j;
  537.         }
  538.           else
  539.         break;
  540.         }
  541.  
  542.       preceeding = i;
  543.       other_preceeding = j;
  544.     }
  545.     }
  546. }
  547.  
  548. /* Cons an additional entry onto the front of an edit script OLD.
  549.    LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  550.    DELETED is the number of lines deleted here from file 0.
  551.    INSERTED is the number of lines inserted here in file 1.
  552.  
  553.    If DELETED is 0 then LINE0 is the number of the line before
  554.    which the insertion was done; vice versa for INSERTED and LINE1.  */
  555.  
  556. static struct change *
  557. add_change (line0, line1, deleted, inserted, old)
  558.      int line0, line1, deleted, inserted;
  559.      struct change *old;
  560. {
  561.   struct change *new = (struct change *) xmalloc (sizeof (struct change));
  562.  
  563.   new->line0 = line0;
  564.   new->line1 = line1;
  565.   new->inserted = inserted;
  566.   new->deleted = deleted;
  567.   new->link = old;
  568.   return new;
  569. }
  570.  
  571. /* Scan the tables of which lines are inserted and deleted,
  572.    producing an edit script in reverse order.  */
  573.  
  574. static struct change *
  575. build_reverse_script (filevec)
  576.      struct file_data filevec[];
  577. {
  578.   struct change *script = 0;
  579.   char *changed0 = filevec[0].changed_flag;
  580.   char *changed1 = filevec[1].changed_flag;
  581.   int len0 = filevec[0].buffered_lines;
  582.   int len1 = filevec[1].buffered_lines;
  583.  
  584.   /* Note that changedN[len0] does exist, and contains 0.  */
  585.  
  586.   int i0 = 0, i1 = 0;
  587.  
  588.   while (i0 < len0 || i1 < len1)
  589.     {
  590.       if (changed0[i0] || changed1[i1])
  591.     {
  592.       int line0 = i0, line1 = i1;
  593.  
  594.       /* Find # lines changed here in each file.  */
  595.       while (changed0[i0]) ++i0;
  596.       while (changed1[i1]) ++i1;
  597.  
  598.       /* Record this change.  */
  599.       script = add_change (line0, line1, i0 - line0, i1 - line1, script);
  600.     }
  601.  
  602.       /* We have reached lines in the two files that match each other.  */
  603.       i0++, i1++;
  604.     }
  605.  
  606.   return script;
  607. }
  608.  
  609. /* Scan the tables of which lines are inserted and deleted,
  610.    producing an edit script in forward order.  */
  611.  
  612. static struct change *
  613. build_script (filevec)
  614.      struct file_data filevec[];
  615. {
  616.   struct change *script = 0;
  617.   char *changed0 = filevec[0].changed_flag;
  618.   char *changed1 = filevec[1].changed_flag;
  619.   int len0 = filevec[0].buffered_lines;
  620.   int len1 = filevec[1].buffered_lines;
  621.  
  622.   /* Note that changedN[-1] does exist, and contains 0.  */
  623.  
  624.   int i0 = len0, i1 = len1;
  625.  
  626.   while (i0 >= 0 || i1 >= 0)
  627.     {
  628.       if (changed0[i0 - 1] || changed1[i1 - 1])
  629.     {
  630.       int line0 = i0, line1 = i1;
  631.  
  632.       /* Find # lines changed here in each file.  */
  633.       while (changed0[i0 - 1]) --i0;
  634.       while (changed1[i1 - 1]) --i1;
  635.  
  636.       /* Record this change.  */
  637.       script = add_change (i0, i1, line0 - i0, line1 - i1, script);
  638.     }
  639.  
  640.       /* We have reached lines in the two files that match each other.  */
  641.       i0--, i1--;
  642.     }
  643.  
  644.   return script;
  645. }
  646.  
  647. /* Report the differences of two files.  DEPTH is the current directory
  648.    depth. */
  649. int
  650. diff_2_files (filevec, depth)
  651.      struct file_data filevec[];
  652.      int depth;
  653. {
  654.   int diags;
  655.   int i;
  656.   struct change *e, *p;
  657.   struct change *script;
  658.   int binary;
  659.  
  660.   /* See if the two named files are actually the same physical file.
  661.      If so, we know they are identical without actually reading them.  */
  662.  
  663. #if !defined(MSDOS)
  664.     /*
  665.     ** since MSC always sets the inode and dev fields to zero under DOS
  666.     ** this test will always think two files are the same.
  667.     */
  668.   if (filevec[0].stat.st_ino == filevec[1].stat.st_ino
  669.       && filevec[0].stat.st_dev == filevec[1].stat.st_dev)
  670.     return 0;
  671. #endif /* MSDOS */
  672.  
  673.   binary = read_files (filevec);
  674.  
  675.   /* If we have detected that file 0 is a binary file,
  676.      compare the two files as binary.  This can happen
  677.      only when the first chunk is read.  */
  678.  
  679.   if (binary)
  680.     {
  681. #if !defined(MSDOS)
  682.       int differs = (filevec[0].buffered_chars != filevec[1].buffered_chars
  683.              || bcmp (filevec[0].buffer, filevec[1].buffer,
  684.                   filevec[1].buffered_chars));
  685. #else
  686.     int differs;
  687.     if(filevec[0].buffered_chars != filevec[1].buffered_chars)
  688.         differs = 1;
  689.     else {
  690.         /*
  691.         ** we've got to do it in chunks because of our
  692.         ** poor 16 bit processor
  693.         */
  694.         char huge *b0 = filevec[0].buffer,
  695.               *b1 = filevec[1].buffer;
  696.         long int count;
  697.         unsigned int delta;
  698.         count = filevec[0].buffered_chars;
  699.         while(count > 0) {
  700.             delta = (unsigned)(count > 65000 ? 65000 : count);
  701.             if(bcmp(b0,b1,delta) != 0)
  702.                 break;
  703.             count -= delta;
  704.             b0 += delta;
  705.             b1 += delta;
  706.         }
  707.         differs = count != 0;
  708.     }
  709. #endif           
  710.       if (differs) 
  711.     message ("Binary files %s and %s differ\n",
  712.          filevec[0].name, filevec[1].name);
  713.  
  714.       for (i = 0; i < 2; ++i)
  715.     free (filevec[i].buffer);
  716.       return differs;
  717.     }
  718.  
  719.   /* Allocate vectors for the results of comparison:
  720.      a flag for each line of each file, saying whether that line
  721.      is an insertion or deletion.
  722.      Allocate an extra element, always zero, at each end of each vector.  */
  723.  
  724.   filevec[0].changed_flag = (char *) xmalloc (filevec[0].buffered_lines + 2);
  725.   filevec[1].changed_flag = (char *) xmalloc (filevec[1].buffered_lines + 2);
  726.   bzero (filevec[0].changed_flag, filevec[0].buffered_lines + 2);
  727.   bzero (filevec[1].changed_flag, filevec[1].buffered_lines + 2);
  728.   filevec[0].changed_flag++;
  729.   filevec[1].changed_flag++;
  730.  
  731.   /* Some lines are obviously insertions or deletions
  732.      because they don't match anything.  Detect them now,
  733.      and avoid even thinking about them in the main comparison algorithm.  */
  734.  
  735.   discard_confusing_lines (filevec);
  736.  
  737.   /* Now do the main comparison algorithm, considering just the
  738.      undiscarded lines.  */
  739.  
  740.   xvec = filevec[0].undiscarded;
  741.   yvec = filevec[1].undiscarded;
  742.   diags = filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines + 3;
  743.   fdiag = (int *) xmalloc (diags * sizeof (int));
  744.   fdiag += filevec[1].nondiscarded_lines + 1;
  745.   bdiag = (int *) xmalloc (diags * sizeof (int));
  746.   bdiag += filevec[1].nondiscarded_lines + 1;
  747.  
  748.   files[0] = filevec[0];
  749.   files[1] = filevec[1];
  750.  
  751.   compareseq (0, filevec[0].nondiscarded_lines,
  752.           0, filevec[1].nondiscarded_lines);
  753.  
  754.   bdiag -= filevec[1].nondiscarded_lines + 1;
  755.   free (bdiag);
  756.   fdiag -= filevec[1].nondiscarded_lines + 1;
  757.   free (fdiag);
  758.  
  759.   /* Modify the results slightly to make them prettier
  760.      in cases where that can validly be done.  */
  761.  
  762.   shift_boundaries (filevec);
  763.  
  764.   /* Get the results of comparison in the form of a chain
  765.      of `struct change's -- an edit script.  */
  766.  
  767.   if (output_style == OUTPUT_ED)
  768.     script = build_reverse_script (filevec);
  769.   else
  770.     script = build_script (filevec);
  771.  
  772.   if (script)
  773.     {
  774.       setup_output (files[0].name, files[1].name, depth);
  775.       if (output_style == OUTPUT_CONTEXT)
  776.     print_context_header (files);
  777.  
  778.       switch (output_style)
  779.     {
  780.     case OUTPUT_CONTEXT:
  781.       print_context_script (script);
  782.       break;
  783.  
  784.     case OUTPUT_ED:
  785.       print_ed_script (script);
  786.       break;
  787.  
  788.     case OUTPUT_FORWARD_ED:
  789.       print_forward_ed_script (script);
  790.       break;
  791.  
  792.     case OUTPUT_RCS:
  793.       print_rcs_script (script);
  794.       break;
  795.  
  796.     case OUTPUT_NORMAL:
  797.       print_normal_script (script);
  798.       break;
  799.     }
  800.  
  801.       finish_output ();
  802.     }
  803.  
  804.   for (i = 1; i >= 0; --i)
  805.     {
  806.       free (filevec[i].realindexes);
  807.       free (filevec[i].undiscarded);
  808.     }
  809.  
  810.   for (i = 1; i >= 0; --i)
  811.     free (--filevec[i].changed_flag);
  812.  
  813.   for (i = 1; i >= 0; --i)
  814.     free (filevec[i].equivs);
  815.  
  816.   for (i = 0; i < 2; ++i)
  817.     {
  818. #if !defined(MSDOS)
  819.       if (filevec[i].buffer != 0)
  820.     free (filevec[i].buffer);
  821. #else    /* MSDOS */
  822.       if (filevec[i].buffer != 0)
  823.     hfree (filevec[i].buffer);
  824. #endif
  825.       free (filevec[i].linbuf);
  826.     }
  827.  
  828.   for (e = script; e; e = p)
  829.     {
  830.       p = e->link;
  831.       free (e);
  832.     }
  833.  
  834.   return script ? 1 : 0;
  835. }
  836.