ref: 33d0b194f3b0df7b830e643c5a9847b6f9a1f74c
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Feb 11 18:01:57 EST 2023
diff: refactor to use struct
--- /dev/null
+++ b/diff.c
@@ -1,0 +1,261 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include "diff.h"
+
+#define DIRECTORY(s) ((s)->qid.type&QTDIR)
+#define REGULAR_FILE(s) ((s)->type == 'M' && !DIRECTORY(s))
+
+Biobuf stdout;
+char mode; /* '\0', 'e', 'f', 'h' */
+char bflag; /* ignore multiple and trailing blanks */
+char rflag; /* recurse down directory trees */
+char mflag; /* pseudo flag: doing multiple files, one dir */
+int anychange;
+
+static char *tmp[] = {"/tmp/diff1XXXXXXXXXXX", "/tmp/diff2XXXXXXXXXXX"};
+static int whichtmp;
+
+static void
+rmtmpfiles(void)
+{
+ while (whichtmp > 0) {
+ whichtmp--;
+ remove(tmp[whichtmp]);
+ }
+}
+
+void
+done(int status)
+{
+ rmtmpfiles();
+ switch(status)
+ {
+ case 0:
+ exits("");
+ case 1:
+ exits("some");
+ default:
+ exits("error");
+ }
+ /*NOTREACHED*/
+}
+
+void
+panic(int status, char *fmt, ...)
+{
+ va_list arg;
+
+ Bflush(&stdout);
+
+ fprint(2, "%s: ", argv0);
+ va_start(arg, fmt);
+ vfprint(2, fmt, arg);
+ va_end(arg);
+ if (status)
+ done(status);
+ /*NOTREACHED*/
+}
+
+static int
+catch(void *a, char *msg)
+{
+ USED(a);
+ panic(2, msg);
+ return 1;
+}
+
+int
+mkpathname(char *pathname, char *path, char *name)
+{
+ if (strlen(path) + strlen(name) > MAXPATHLEN) {
+ panic(0, "pathname %s/%s too long\n", path, name);
+ return 1;
+ }
+ sprint(pathname, "%s/%s", path, name);
+ return 0;
+}
+
+static char *
+mktmpfile(int input, Dir **sb)
+{
+ int fd, i;
+ char *p;
+ char buf[8192];
+
+ atnotify(catch, 1);
+ p = mktemp(tmp[whichtmp++]);
+ fd = create(p, OWRITE, 0600);
+ if (fd < 0) {
+ panic(mflag ? 0: 2, "cannot create %s: %r\n", p);
+ return 0;
+ }
+ while ((i = read(input, buf, sizeof(buf))) > 0) {
+ if ((i = write(fd, buf, i)) < 0)
+ break;
+ }
+ *sb = dirfstat(fd);
+ close(fd);
+ if (i < 0) {
+ panic(mflag ? 0: 2, "cannot read/write %s: %r\n", p);
+ return 0;
+ }
+ return p;
+}
+
+static char *
+statfile(char *file, Dir **sb)
+{
+ Dir *dir;
+ int input;
+
+ dir = dirstat(file);
+ if(dir == nil) {
+ if (strcmp(file, "-") || (dir = dirfstat(0)) == nil) {
+ panic(mflag ? 0: 2, "cannot stat %s: %r\n", file);
+ return 0;
+ }
+ free(dir);
+ return mktmpfile(0, sb);
+ } else if (!REGULAR_FILE(dir) && !DIRECTORY(dir)) {
+ free(dir);
+ if ((input = open(file, OREAD)) == -1) {
+ panic(mflag ? 0: 2, "cannot open %s: %r\n", file);
+ return 0;
+ }
+ file = mktmpfile(input, sb);
+ close(input);
+ } else
+ *sb = dir;
+ return file;
+}
+
+void
+diff(char *f, char *t, int level)
+{
+ char *fp, *tp, *p, fb[MAXPATHLEN+1], tb[MAXPATHLEN+1];
+ Dir *fsb, *tsb;
+
+ if ((fp = statfile(f, &fsb)) == 0)
+ goto Return;
+ if ((tp = statfile(t, &tsb)) == 0){
+ free(fsb);
+ goto Return;
+ }
+ if (DIRECTORY(fsb) && DIRECTORY(tsb)) {
+ if (rflag || level == 0)
+ diffdir(fp, tp, level);
+ else
+ Bprint(&stdout, "Common subdirectories: %s and %s\n", fp, tp);
+ }
+ else if (REGULAR_FILE(fsb) && REGULAR_FILE(tsb))
+ diffreg(fp, f, tp, t);
+ else {
+ if (REGULAR_FILE(fsb)) {
+ if ((p = utfrrune(f, '/')) == 0)
+ p = f;
+ else
+ p++;
+ if (mkpathname(tb, tp, p) == 0)
+ diffreg(fp, f, tb, t);
+ } else {
+ if ((p = utfrrune(t, '/')) == 0)
+ p = t;
+ else
+ p++;
+ if (mkpathname(fb, fp, p) == 0)
+ diffreg(fb, f, tp, t);
+ }
+ }
+ free(fsb);
+ free(tsb);
+Return:
+ rmtmpfiles();
+}
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s [-abcefmnrw] file1 ... file2\n", argv0);
+ exits("usage");
+}
+
+void
+main(int argc, char *argv[])
+{
+ int i;
+ Dir *fsb, *tsb;
+
+ Binit(&stdout, 1, OWRITE);
+ ARGBEGIN{
+ case 'e':
+ case 'f':
+ case 'n':
+ case 'c':
+ case 'a':
+ case 'u':
+ mode = ARGC();
+ break;
+ case 'w':
+ bflag = 2;
+ break;
+
+ case 'b':
+ bflag = 1;
+ break;
+
+ case 'r':
+ rflag = 1;
+ break;
+
+ case 'm':
+ mflag = 1;
+ break;
+
+ case 'h':
+ default:
+ usage();
+ }ARGEND;
+ if (argc < 2)
+ usage();
+ if ((tsb = dirstat(argv[argc-1])) == nil)
+ panic(2, "can't stat %s\n", argv[argc-1]);
+ if (argc > 2) {
+ if (!DIRECTORY(tsb))
+ panic(2, "not directory: %s", argv[argc-1]);
+ mflag = 1;
+ } else {
+ if ((fsb = dirstat(argv[0])) == nil)
+ panic(2, "can't stat %s\n", argv[0]);
+ if (DIRECTORY(fsb) && DIRECTORY(tsb))
+ mflag = 1;
+ free(fsb);
+ }
+ free(tsb);
+ for (i = 0; i < argc-1; i++)
+ diff(argv[i], argv[argc-1], 0);
+ done(anychange);
+ /*NOTREACHED*/
+}
+
+static char noroom[] = "out of memory - try diff -h\n";
+
+void *
+emalloc(unsigned n)
+{
+ register void *p;
+
+ if ((p = malloc(n)) == 0)
+ panic(2, noroom);
+ return p;
+}
+
+void *
+erealloc(void *p, unsigned n)
+{
+ void *rp;
+
+ if ((rp = realloc(p, n)) == 0)
+ panic(2, noroom);
+ return rp;
+}
--- /dev/null
+++ b/diff.h
@@ -1,0 +1,69 @@
+typedef struct Line Line;
+typedef struct Cand Cand;
+typedef struct Diff Diff;
+typedef struct Change Change;
+
+struct Line {
+ int serial;
+ int value;
+};
+
+struct Cand {
+ int x;
+ int y;
+ int pred;
+};
+
+struct Change
+{
+ int a;
+ int b;
+ int c;
+ int d;
+};
+
+struct Diff {
+ Cand cand;
+ Line *file[2], line;
+ int len[2];
+ int binary;
+ Line *sfile[2]; /*shortened by pruning common prefix and suffix*/
+ int slen[2];
+ int pref, suff; /*length of prefix and suffix*/
+ int *class; /*will be overlaid on file[0]*/
+ int *member; /*will be overlaid on file[1]*/
+ int *klist; /*will be overlaid on file[0] after class*/
+ Cand *clist; /* merely a free storage pot for candidates */
+ int clen;
+ int *J; /*will be overlaid on class*/
+ long *ixold; /*will be overlaid on klist*/
+ long *ixnew; /*will be overlaid on file[1]*/
+ char *file1;
+ char *file2;
+ Biobuf *input[2];
+ int firstchange;
+ Change *changes;
+ int nchanges;
+};
+
+extern char mode;
+extern char bflag;
+extern char rflag;
+extern char mflag;
+extern int anychange;
+extern Biobuf stdout;
+
+#define MAXPATHLEN 1024
+
+int mkpathname(char *, char *, char *);
+void *emalloc(unsigned);
+void *erealloc(void *, unsigned);
+void diff(char *, char *, int);
+void diffdir(char *, char *, int);
+void diffreg(char *, char *, char *, char *);
+Biobuf *prepare(Diff*, int, char *, char *);
+void panic(int, char *, ...);
+void check(Diff *, Biobuf *, Biobuf *);
+void change(Diff *, int, int, int, int);
+void flushchanges(Diff *d);
+
--- /dev/null
+++ b/diffdir.c
@@ -1,0 +1,113 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include "diff.h"
+
+static int
+itemcmp(void *v1, void *v2)
+{
+ char **d1 = v1, **d2 = v2;
+
+ return strcmp(*d1, *d2);
+}
+
+static char **
+scandir(char *name)
+{
+ char **cp;
+ Dir *db;
+ int nitems;
+ int fd, n;
+
+ if ((fd = open(name, OREAD)) < 0) {
+ fprint(2, "%s: can't open %s: %r\n", argv0, name);
+ /* fake an empty directory */
+ cp = emalloc(sizeof(char*));
+ cp[0] = 0;
+ return cp;
+ }
+ cp = 0;
+ nitems = 0;
+ if((n = dirreadall(fd, &db)) > 0){
+ while (n--) {
+ cp = erealloc(cp, (nitems+1)*sizeof(char*));
+ cp[nitems] = emalloc(strlen((db+n)->name)+1);
+ strcpy(cp[nitems], (db+n)->name);
+ nitems++;
+ }
+ free(db);
+ }
+ cp = erealloc(cp, (nitems+1)*sizeof(char*));
+ cp[nitems] = 0;
+ close(fd);
+ qsort((char *)cp, nitems, sizeof(char*), itemcmp);
+ return cp;
+}
+
+static int
+isdotordotdot(char *p)
+{
+ if (*p == '.') {
+ if (!p[1])
+ return 1;
+ if (p[1] == '.' && !p[2])
+ return 1;
+ }
+ return 0;
+}
+
+void
+diffdir(char *f, char *t, int level)
+{
+ char **df, **dt, **dirf, **dirt;
+ char *from, *to;
+ int res;
+ char fb[MAXPATHLEN+1], tb[MAXPATHLEN+1];
+
+ df = scandir(f);
+ dt = scandir(t);
+ dirf = df;
+ dirt = dt;
+ while (*df || *dt) {
+ from = *df;
+ to = *dt;
+ if (from && isdotordotdot(from)) {
+ df++;
+ continue;
+ }
+ if (to && isdotordotdot(to)) {
+ dt++;
+ continue;
+ }
+ if (!from)
+ res = 1;
+ else if (!to)
+ res = -1;
+ else
+ res = strcmp(from, to);
+ if (res < 0) {
+ if (mode == 0 || mode == 'n')
+ Bprint(&stdout, "Only in %s: %s\n", f, from);
+ df++;
+ continue;
+ }
+ if (res > 0) {
+ if (mode == 0 || mode == 'n')
+ Bprint(&stdout, "Only in %s: %s\n", t, to);
+ dt++;
+ continue;
+ }
+ if (mkpathname(fb, f, from))
+ continue;
+ if (mkpathname(tb, t, to))
+ continue;
+ diff(fb, tb, level+1);
+ df++; dt++;
+ }
+ for (df = dirf; *df; df++)
+ free(*df);
+ for (dt = dirt; *dt; dt++)
+ free(*dt);
+ free(dirf);
+ free(dirt);
+}
--- /dev/null
+++ b/diffio.c
@@ -1,0 +1,373 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <ctype.h>
+#include "diff.h"
+
+#define MAXLINELEN 4096
+#define MIN(x, y) ((x) < (y) ? (x): (y))
+
+static int
+readline(Biobuf *bp, char *buf)
+{
+ int c;
+ char *p, *e;
+
+ p = buf;
+ e = p + MAXLINELEN-1;
+ do {
+ c = Bgetc(bp);
+ if (c < 0) {
+ if (p == buf)
+ return -1;
+ break;
+ }
+ if (c == '\n')
+ break;
+ *p++ = c;
+ } while (p < e);
+ *p = 0;
+ if (c != '\n' && c >= 0) {
+ do c = Bgetc(bp);
+ while (c >= 0 && c != '\n');
+ }
+ return p - buf;
+}
+
+#define HALFLONG 16
+#define low(x) (x&((1L<<HALFLONG)-1))
+#define high(x) (x>>HALFLONG)
+
+/*
+ * hashing has the effect of
+ * arranging line in 7-bit bytes and then
+ * summing 1-s complement in 16-bit hunks
+ */
+static int
+readhash(Biobuf *bp, char *buf)
+{
+ long sum;
+ unsigned shift;
+ char *p;
+ int len, space;
+
+ sum = 1;
+ shift = 0;
+ if ((len = readline(bp, buf)) == -1)
+ return 0;
+ p = buf;
+ switch(bflag) /* various types of white space handling */
+ {
+ case 0:
+ while (len--) {
+ sum += (long)*p++ << (shift &= (HALFLONG-1));
+ shift += 7;
+ }
+ break;
+ case 1:
+ /*
+ * coalesce multiple white-space
+ */
+ for (space = 0; len--; p++) {
+ if (isspace(*p)) {
+ space++;
+ continue;
+ }
+ if (space) {
+ shift += 7;
+ space = 0;
+ }
+ sum += (long)*p << (shift &= (HALFLONG-1));
+ shift += 7;
+ }
+ break;
+ default:
+ /*
+ * strip all white-space
+ */
+ while (len--) {
+ if (isspace(*p)) {
+ p++;
+ continue;
+ }
+ sum += (long)*p++ << (shift &= (HALFLONG-1));
+ shift += 7;
+ }
+ break;
+ }
+ sum = low(sum) + high(sum);
+ return ((short)low(sum) + (short)high(sum));
+}
+
+Biobuf *
+prepare(Diff *d, int i, char *arg, char *orig)
+{
+ Line *p;
+ int j, h;
+ Biobuf *bp;
+ char *cp, buf[MAXLINELEN];
+ int nbytes;
+ Rune r;
+
+ bp = Bopen(arg, OREAD);
+ if (!bp) {
+ panic(mflag ? 0: 2, "cannot open %s: %r\n", arg);
+ return 0;
+ }
+ if (d->binary)
+ return bp;
+ nbytes = Bread(bp, buf, MIN(1024, MAXLINELEN));
+ if (nbytes > 0) {
+ cp = buf;
+ while (cp < buf+nbytes-UTFmax) {
+ /*
+ * heuristic for a binary file in the
+ * brave new UNICODE world
+ */
+ cp += chartorune(&r, cp);
+ if (r == 0 || (r > 0x7f && r <= 0xa0)) {
+ d->binary++;
+ return bp;
+ }
+ }
+ Bseek(bp, 0, 0);
+ }
+ p = emalloc(3*sizeof(Line));
+ for (j = 0; h = readhash(bp, buf); p[j].value = h)
+ p = erealloc(p, (++j+3)*sizeof(Line));
+ d->len[i] = j;
+ d->file[i] = p;
+ d->input[i] = bp;
+ if (i == 0) {
+ d->file1 = orig;
+ d->firstchange = 0;
+ } else
+ d->file2 = orig;
+ return bp;
+}
+
+static int
+squishspace(char *buf)
+{
+ char *p, *q;
+ int space;
+
+ for (space = 0, q = p = buf; *q; q++) {
+ if (isspace(*q)) {
+ space++;
+ continue;
+ }
+ if (space && bflag == 1) {
+ *p++ = ' ';
+ space = 0;
+ }
+ *p++ = *q;
+ }
+ *p = 0;
+ return p - buf;
+}
+
+/*
+ * need to fix up for unexpected EOF's
+ */
+void
+check(Diff *d, Biobuf *bf, Biobuf *bt)
+{
+ int f, t, flen, tlen;
+ char fbuf[MAXLINELEN], tbuf[MAXLINELEN];
+
+ d->ixold[0] = 0;
+ d->ixnew[0] = 0;
+ for (f = t = 1; f < d->len[0]; f++) {
+ flen = readline(bf, fbuf);
+ d->ixold[f] = d->ixold[f-1] + flen + 1; /* ftell(bf) */
+ if (d->J[f] == 0)
+ continue;
+ do {
+ tlen = readline(bt, tbuf);
+ d->ixnew[t] = d->ixnew[t-1] + tlen + 1; /* ftell(bt) */
+ } while (t++ < d->J[f]);
+ if (bflag) {
+ flen = squishspace(fbuf);
+ tlen = squishspace(tbuf);
+ }
+ if (flen != tlen || strcmp(fbuf, tbuf))
+ d->J[f] = 0;
+ }
+ while (t < d->len[1]) {
+ tlen = readline(bt, tbuf);
+ d->ixnew[t] = d->ixnew[t-1] + tlen + 1; /* fseek(bt) */
+ t++;
+ }
+}
+
+static void
+range(int a, int b, char *separator)
+{
+ Bprint(&stdout, "%d", a > b ? b: a);
+ if (a < b)
+ Bprint(&stdout, "%s%d", separator, b);
+}
+
+static void
+fetch(Diff *d, long *f, int a, int b, Biobuf *bp, char *s)
+{
+ char buf[MAXLINELEN];
+ int maxb;
+
+ if(a <= 1)
+ a = 1;
+ if(bp == d->input[0])
+ maxb = d->len[0];
+ else
+ maxb = d->len[1];
+ if(b > maxb)
+ b = maxb;
+ if(a > maxb)
+ return;
+ Bseek(bp, f[a-1], 0);
+ while (a++ <= b) {
+ readline(bp, buf);
+ Bprint(&stdout, "%s%s\n", s, buf);
+ }
+}
+
+void
+change(Diff *df, int a, int b, int c, int d)
+{
+ char verb;
+ char buf[4];
+ Change *ch;
+
+ if (a > b && c > d)
+ return;
+ anychange = 1;
+ if (mflag && df->firstchange == 0) {
+ if(mode) {
+ buf[0] = '-';
+ buf[1] = mode;
+ buf[2] = ' ';
+ buf[3] = '\0';
+ } else {
+ buf[0] = '\0';
+ }
+ Bprint(&stdout, "diff %s%s %s\n", buf, df->file1, df->file2);
+ df->firstchange = 1;
+ }
+ verb = a > b ? 'a': c > d ? 'd': 'c';
+ switch(mode) {
+ case 'e':
+ range(a, b, ",");
+ Bputc(&stdout, verb);
+ break;
+ case 0:
+ range(a, b, ",");
+ Bputc(&stdout, verb);
+ range(c, d, ",");
+ break;
+ case 'n':
+ Bprint(&stdout, "%s:", df->file1);
+ range(a, b, ",");
+ Bprint(&stdout, " %c ", verb);
+ Bprint(&stdout, "%s:", df->file2);
+ range(c, d, ",");
+ break;
+ case 'f':
+ Bputc(&stdout, verb);
+ range(a, b, " ");
+ break;
+ case 'c':
+ case 'a':
+ case 'u':
+ if(df->nchanges%1024 == 0)
+ df->changes = erealloc(df->changes, (df->nchanges+1024)*sizeof(df->changes[0]));
+ ch = &df->changes[df->nchanges++];
+ ch->a = a;
+ ch->b = b;
+ ch->c = c;
+ ch->d = d;
+ return;
+ }
+ Bputc(&stdout, '\n');
+ if (mode == 0 || mode == 'n') {
+ fetch(df, df->ixold, a, b, df->input[0], "< ");
+ if (a <= b && c <= d)
+ Bprint(&stdout, "---\n");
+ }
+ fetch(df, df->ixnew, c, d, df->input[1], mode == 0 || mode == 'n' ? "> ": "");
+ if (mode != 0 && mode != 'n' && c <= d)
+ Bprint(&stdout, ".\n");
+}
+
+enum
+{
+ Lines = 3, /* number of lines of context shown */
+};
+
+int
+changeset(Diff *d, int i)
+{
+ while(i < d->nchanges && d->changes[i].b + 1 + 2*Lines > d->changes[i+1].a)
+ i++;
+ if(i < d->nchanges)
+ return i+1;
+ return d->nchanges;
+}
+
+void
+flushchanges(Diff *df)
+{
+ vlong a, b, c, d, at, hdr;
+ vlong i, j;
+
+ if(df->nchanges == 0)
+ return;
+
+ hdr = 0;
+ for(i=0; i < df->nchanges; ){
+ j = changeset(df, i);
+ a = df->changes[i].a - Lines;
+ b = df->changes[j-1].b + Lines;
+ c = df->changes[i].c - Lines;
+ d = df->changes[j-1].d + Lines;
+ if(a < 1)
+ a = 1;
+ if(c < 1)
+ c = 1;
+ if(b > df->len[0])
+ b = df->len[0];
+ if(d > df->len[1])
+ d = df->len[1];
+ if(mode == 'a'){
+ a = 1;
+ b = df->len[0];
+ c = 1;
+ d = df->len[1];
+ j = df->nchanges;
+ }
+ if(mode == 'u'){
+ if(!hdr){
+ Bprint(&stdout, "--- %s\n", df->file1);
+ Bprint(&stdout, "+++ %s\n", df->file2);
+ hdr = 1;
+ }
+ Bprint(&stdout, "@@ -%lld,%lld +%lld,%lld @@\n", a, b-a+1, c, d-c+1);
+ }else{
+ Bprint(&stdout, "%s:", df->file1);
+ range(a, b, ",");
+ Bprint(&stdout, " - ");
+ Bprint(&stdout, "%s:", df->file2);
+ range(c, d, ",");
+ Bputc(&stdout, '\n');
+ }
+ at = a;
+ for(; i<j; i++){
+ fetch(df, df->ixold, at, df->changes[i].a-1, df->input[0], mode == 'u' ? " " : " ");
+ fetch(df, df->ixold, df->changes[i].a, df->changes[i].b, df->input[0], mode == 'u' ? "-" : "- ");
+ fetch(df, df->ixnew, df->changes[i].c, df->changes[i].d, df->input[1], mode == 'u' ? "+" : "+ ");
+ at = df->changes[i].b+1;
+ }
+ fetch(df, df->ixold, at, b, df->input[0], mode == 'u' ? " " : " ");
+ }
+ df->nchanges = 0;
+}
--- /dev/null
+++ b/diffreg.c
@@ -1,0 +1,399 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include "diff.h"
+
+/* diff - differential file comparison
+*
+* Uses an algorithm due to Harold Stone, which finds
+* a pair of longest identical subsequences in the two
+* files.
+*
+* The major goal is to generate the match vector J.
+* J[i] is the index of the line in file1 corresponding
+* to line i file0. J[i] = 0 if there is no
+* such line in file1.
+*
+* Lines are hashed so as to work in core. All potential
+* matches are located by sorting the lines of each file
+* on the hash (called value). In particular, this
+* collects the equivalence classes in file1 together.
+* Subroutine equiv replaces the value of each line in
+* file0 by the index of the first element of its
+* matching equivalence in (the reordered) file1.
+* To save space equiv squeezes file1 into a single
+* array member in which the equivalence classes
+* are simply concatenated, except that their first
+* members are flagged by changing sign.
+*
+* Next the indices that point into member are unsorted into
+* array class according to the original order of file0.
+*
+* The cleverness lies in routine stone. This marches
+* through the lines of file0, developing a vector klist
+* of "k-candidates". At step i a k-candidate is a matched
+* pair of lines x,y (x in file0 y in file1) such that
+* there is a common subsequence of lenght k
+* between the first i lines of file0 and the first y
+* lines of file1, but there is no such subsequence for
+* any smaller y. x is the earliest possible mate to y
+* that occurs in such a subsequence.
+*
+* Whenever any of the members of the equivalence class of
+* lines in file1 matable to a line in file0 has serial number
+* less than the y of some k-candidate, that k-candidate
+* with the smallest such y is replaced. The new
+* k-candidate is chained (via pred) to the current
+* k-1 candidate so that the actual subsequence can
+* be recovered. When a member has serial number greater
+* that the y of all k-candidates, the klist is extended.
+* At the end, the longest subsequence is pulled out
+* and placed in the array J by unravel.
+*
+* With J in hand, the matches there recorded are
+* check'ed against reality to assure that no spurious
+* matches have crept in due to hashing. If they have,
+* they are broken, and "jackpot " is recorded--a harmless
+* matter except that a true match for a spuriously
+* mated line may now be unnecessarily reported as a change.
+*
+* Much of the complexity of the program comes simply
+* from trying to minimize core utilization and
+* maximize the range of doable problems by dynamically
+* allocating what is needed and reusing what is not.
+* The core requirements for problems larger than somewhat
+* are (in words) 2*length(file0) + length(file1) +
+* 3*(number of k-candidates installed), typically about
+* 6n words for files of length n.
+*/
+
+static void
+sort(Line *a, int n) /*shellsort CACM #201*/
+{
+ int m;
+ Line *ai, *aim, *j, *k;
+ Line w;
+ int i;
+
+ m = 0;
+ for (i = 1; i <= n; i *= 2)
+ m = 2*i - 1;
+ for (m /= 2; m != 0; m /= 2) {
+ k = a+(n-m);
+ for (j = a+1; j <= k; j++) {
+ ai = j;
+ aim = ai+m;
+ do {
+ if (aim->value > ai->value ||
+ aim->value == ai->value &&
+ aim->serial > ai->serial)
+ break;
+ w = *ai;
+ *ai = *aim;
+ *aim = w;
+
+ aim = ai;
+ ai -= m;
+ } while (ai > a && aim >= ai);
+ }
+ }
+}
+
+static void
+unsort(Line *f, int l, int *b)
+{
+ int *a;
+ int i;
+
+ a = malloc((l+1)*sizeof(int));
+ for(i=1;i<=l;i++)
+ a[f[i].serial] = f[i].value;
+ for(i=1;i<=l;i++)
+ b[i] = a[i];
+ free(a);
+}
+
+static void
+prune(Diff *d)
+{
+ int i,j;
+
+ for(d->pref = 0; d->pref < d->len[0] && d->pref < d->len[1] &&
+ d->file[0][d->pref+1].value == d->file[1][d->pref+1].value;
+ d->pref++) ;
+ for(d->suff=0; d->suff < d->len[0] - d->pref && d->suff < d->len[1] - d->pref &&
+ d->file[0][d->len[0] - d->suff].value == d->file[1][d->len[1] - d->suff].value;
+ d->suff++) ;
+ for(j=0;j<2;j++) {
+ d->sfile[j] = d->file[j]+d->pref;
+ d->slen[j] = d->len[j]-d->pref-d->suff;
+ for(i=0;i<=d->slen[j];i++)
+ d->sfile[j][i].serial = i;
+ }
+}
+
+static void
+equiv(Line *a, int n, Line *b, int m, int *c)
+{
+ int i, j;
+
+ i = j = 1;
+ while(i<=n && j<=m) {
+ if(a[i].value < b[j].value)
+ a[i++].value = 0;
+ else if(a[i].value == b[j].value)
+ a[i++].value = j;
+ else
+ j++;
+ }
+ while(i <= n)
+ a[i++].value = 0;
+ b[m+1].value = 0;
+ j = 0;
+ while(++j <= m) {
+ c[j] = -b[j].serial;
+ while(b[j+1].value == b[j].value) {
+ j++;
+ c[j] = b[j].serial;
+ }
+ }
+ c[j] = -1;
+}
+
+static int
+newcand(Diff *d, int x, int y, int pred)
+{
+ Cand *q;
+
+ d->clist = erealloc(d->clist, (d->clen+1)*sizeof(Cand));
+ q = d->clist + d->clen;
+ q->x = x;
+ q->y = y;
+ q->pred = pred;
+ return d->clen++;
+}
+
+static int
+search(Diff *d, int *c, int k, int y)
+{
+ int i, j, l;
+ int t;
+
+ if(d->clist[c[k]].y < y) /*quick look for typical case*/
+ return k+1;
+ i = 0;
+ j = k+1;
+ while((l=(i+j)/2) > i) {
+ t = d->clist[c[l]].y;
+ if(t > y)
+ j = l;
+ else if(t < y)
+ i = l;
+ else
+ return l;
+ }
+ return l+1;
+}
+
+static int
+stone(Diff *d, int *a, int n, int *b, int *c)
+{
+ int i, k,y;
+ int j, l;
+ int oldc, tc;
+ int oldl;
+
+ k = 0;
+ c[0] = newcand(d, 0, 0, 0);
+ for(i=1; i<=n; i++) {
+ j = a[i];
+ if(j==0)
+ continue;
+ y = -b[j];
+ oldl = 0;
+ oldc = c[0];
+ do {
+ if(y <= d->clist[oldc].y)
+ continue;
+ l = search(d, c, k, y);
+ if(l!=oldl+1)
+ oldc = c[l-1];
+ if(l<=k) {
+ if(d->clist[c[l]].y <= y)
+ continue;
+ tc = c[l];
+ c[l] = newcand(d, i, y, oldc);
+ oldc = tc;
+ oldl = l;
+ } else {
+ c[l] = newcand(d, i,y,oldc);
+ k++;
+ break;
+ }
+ } while((y=b[++j]) > 0);
+ }
+ return k;
+}
+
+static void
+unravel(Diff *d, int p)
+{
+ int i;
+ Cand *q;
+
+ for(i=0; i<=d->len[0]; i++) {
+ if (i <= d->pref)
+ d->J[i] = i;
+ else if (i > d->len[0]-d->suff)
+ d->J[i] = i+d->len[1] - d->len[0];
+ else
+ d->J[i] = 0;
+ }
+ for(q=d->clist+p; q->y != 0; q= d->clist + q->pred)
+ d->J[q->x+d->pref] = q->y+d->pref;
+}
+
+static void
+output(Diff *d)
+{
+ int m, i0, i1, j0, j1;
+
+ m = d->len[0];
+ d->J[0] = 0;
+ d->J[m+1] = d->len[1]+1;
+ if (mode != 'e') {
+ for (i0 = 1; i0 <= m; i0 = i1+1) {
+ while (i0 <= m && d->J[i0] == d->J[i0-1]+1)
+ i0++;
+ j0 = d->J[i0-1]+1;
+ i1 = i0-1;
+ while (i1 < m && d->J[i1+1] == 0)
+ i1++;
+ j1 = d->J[i1+1]-1;
+ d->J[i1] = j1;
+ change(d, i0, i1, j0, j1);
+ }
+ } else {
+ for (i0 = m; i0 >= 1; i0 = i1-1) {
+ while (i0 >= 1 && d->J[i0] == d->J[i0+1]-1 && d->J[i0])
+ i0--;
+ j0 = d->J[i0+1]-1;
+ i1 = i0+1;
+ while (i1 > 1 && d->J[i1-1] == 0)
+ i1--;
+ j1 = d->J[i1-1]+1;
+ d->J[i1] = j1;
+ change(d, i1 , i0, j1, j0);
+ }
+ }
+ if (m == 0)
+ change(d, 1, 0, 1, d->len[1]);
+ flushchanges(d);
+}
+
+#define BUF 4096
+static int
+cmp(Biobuf* b1, Biobuf* b2)
+{
+ int n;
+ uchar buf1[BUF], buf2[BUF];
+ int f1, f2;
+ vlong nc = 1;
+ uchar *b1s, *b1e, *b2s, *b2e;
+
+ f1 = Bfildes(b1);
+ f2 = Bfildes(b2);
+ seek(f1, 0, 0);
+ seek(f2, 0, 0);
+ b1s = b1e = buf1;
+ b2s = b2e = buf2;
+ for(;;){
+ if(b1s >= b1e){
+ if(b1s >= &buf1[BUF])
+ b1s = buf1;
+ n = read(f1, b1s, &buf1[BUF] - b1s);
+ b1e = b1s + n;
+ }
+ if(b2s >= b2e){
+ if(b2s >= &buf2[BUF])
+ b2s = buf2;
+ n = read(f2, b2s, &buf2[BUF] - b2s);
+ b2e = b2s + n;
+ }
+ n = b2e - b2s;
+ if(n > b1e - b1s)
+ n = b1e - b1s;
+ if(n <= 0)
+ break;
+ if(memcmp((void *)b1s, (void *)b2s, n) != 0){
+ return 1;
+ }
+ nc += n;
+ b1s += n;
+ b2s += n;
+ }
+ if(b1e - b1s == b2e - b2s)
+ return 0;
+ return 1;
+}
+
+void
+diffreg(char *f, char *fo, char *t, char *to)
+{
+ Biobuf *b0, *b1;
+ Diff d;
+ int k;
+
+ d.binary = 0;
+ memset(&d, 0, sizeof(d));
+ b0 = prepare(&d, 0, f, fo);
+ if (!b0)
+ return;
+ b1 = prepare(&d, 1, t, to);
+ if (!b1) {
+ Bterm(b0);
+ return;
+ }
+ if (d.binary){
+ // could use b0 and b1 but this is simpler.
+ if (cmp(b0, b1))
+ print("binary files %s %s differ\n", f, t);
+ Bterm(b0);
+ Bterm(b1);
+ return;
+ }
+ d.clen = 0;
+ prune(&d);
+ sort(d.sfile[0], d.slen[0]);
+ sort(d.sfile[1], d.slen[1]);
+
+ d.member = (int *)d.file[1];
+ equiv(d.sfile[0], d.slen[0], d.sfile[1], d.slen[1], d.member);
+ d.member = erealloc(d.member, (d.slen[1]+2)*sizeof(int));
+
+ d.class = (int *)d.file[0];
+ unsort(d.sfile[0], d.slen[0], d.class);
+ d.class = erealloc(d.class, (d.slen[0]+2)*sizeof(int));
+
+ d.klist = emalloc((d.slen[0]+2)*sizeof(int));
+ d.clist = emalloc(sizeof(Cand));
+ k = stone(&d, d.class, d.slen[0], d.member, d.klist);
+ free(d.member);
+ free(d.class);
+
+ d.J = emalloc((d.len[0]+2)*sizeof(int));
+ unravel(&d, d.klist[k]);
+ free(d.clist);
+ free(d.klist);
+
+ d.ixold = emalloc((d.len[0]+2)*sizeof(long));
+ d.ixnew = emalloc((d.len[1]+2)*sizeof(long));
+ Bseek(b0, 0, 0); Bseek(b1, 0, 0);
+ check(&d, b0, b1);
+ output(&d);
+ free(d.J);
+ free(d.ixold);
+ free(d.ixnew);
+ Bterm(b0);
+ Bterm(b1);
+}
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,12 @@
+< /$objtype/mkfile
+
+TARG=diff
+OFILES=\
+ diffdir.$O\
+ diffio.$O\
+ diffreg.$O\
+
+HFILES=diff.h
+
+BIN=/$objtype/bin
+</sys/src/cmd/mkmany