commit c586c75de892d57c7f6e44cecc00540e21acebeb
parent c250de6219bd67e8336f1e6bd7ba2a870d9cecf6
Author: torter <koubbamohamedrayan@gmail.com>
Date: Mon, 16 Nov 2026 21:43:26 +0000
cmp and diff: unified, context and normal output, -r -N -q -s -i -w -b, matched against GNU
Diffstat:
| M | rifle.c | | | 455 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | tests/cases | | | 7 | +++++++ |
2 files changed, 462 insertions(+), 0 deletions(-)
diff --git a/rifle.c b/rifle.c
@@ -11734,6 +11734,459 @@ static int t_fmt(int argc, char **argv)
return finish_stdout() ? 1 : bad;
}
+/* ---- cmp and diff ---- */
+
+static void cmp_ch(unsigned char c, char *o)
+{
+ int i = 0;
+ if (c >= 128) { o[i++] = 'M'; o[i++] = '-'; c -= 128; }
+ if (c < 32) { o[i++] = '^'; o[i++] = c + 64; } else if (c == 127) { o[i++] = '^'; o[i++] = '?'; } else o[i++] = c;
+ o[i] = 0;
+}
+
+static int t_cmp(int argc, char **argv)
+{
+ static const struct opt lo[] = { { "print-bytes", 0, 'b' }, { "ignore-initial", 1, 'i' }, { "verbose", 0, 'l' }, { "bytes", 1, 'n' }, { "quiet", 0, 's' }, { "silent", 0, 's' }, { NULL, 0, 0 } };
+ int c, pb = 0, verbose = 0, quiet = 0, fa, fb, differ = 0, width = 1;
+ unsigned long skip1 = 0, skip2 = 0, limit = ~0UL, v, line = 1, t;
+ char *ba, *bb, c1[8], c2[8];
+ size_t la, lb, i, n;
+ usage_status = 2;
+ opts_reset();
+ while ((c = getopts(argc, argv, "bi:ln:s", lo))) {
+ switch (c) {
+ case 'b': pb = 1; break; case 'l': verbose = 1; break; case 's': quiet = 1; break;
+ case 'i': { char *e; skip1 = strtoul(optarg, &e, 10); skip2 = skip1; if (*e == ':') skip2 = strtoul(e + 1, &e, 10); if (*e) die(2, "invalid --ignore-initial value '%s'", optarg); break; }
+ case 'n': if (!parse_ulong(optarg, &v)) die(2, "invalid --bytes value '%s'", optarg); limit = v; break;
+ default: break;
+ }
+ }
+ if (nops < 2) { if (!nops) missing_operand(); say("missing operand after '%s'", ops[0]); usage_error(); }
+ if (nops > 2) extra_operand(ops[2]);
+ if (!strcmp(ops[0], "-") && !strcmp(ops[1], "-")) return 0;
+ fa = open_input(ops[0]);
+ if (fa < 0) { if (!quiet) perr("%s", ops[0]); return 2; }
+ fb = open_input(ops[1]);
+ if (fb < 0) { if (!quiet) perr("%s", ops[1]); return 2; }
+ if (read_all(fa, &ba, &la) < 0 || read_all(fb, &bb, &lb) < 0) { if (!quiet) perr("read error"); return 2; }
+ if (skip1 > la) skip1 = la;
+ if (skip2 > lb) skip2 = lb;
+ ba += skip1; la -= skip1; bb += skip2; lb -= skip2;
+ n = la < lb ? la : lb;
+ if (n > limit) n = limit;
+ for (t = n; t >= 10; t /= 10) width++;
+ for (i = 0; i < n; i++) {
+ if (ba[i] != bb[i]) {
+ if (!verbose) {
+ if (!quiet) {
+ printf("%s %s differ: %s %lu, line %lu", ops[0], ops[1], pb ? "byte" : "char", (unsigned long)i + 1, line);
+ if (pb) { cmp_ch(ba[i], c1); cmp_ch(bb[i], c2); printf(" is %3o %s %3o %s", (unsigned char)ba[i], c1, (unsigned char)bb[i], c2); }
+ putchar('\n');
+ }
+ return finish_stdout() ? 2 : 1;
+ }
+ differ = 1;
+ if (pb) { cmp_ch(ba[i], c1); cmp_ch(bb[i], c2); printf("%*lu %3o %-4s %3o %s", width, (unsigned long)i + 1, (unsigned char)ba[i], c1, (unsigned char)bb[i], c2); }
+ else printf("%*lu %3o %3o", width, (unsigned long)i + 1, (unsigned char)ba[i], (unsigned char)bb[i]);
+ putchar('\n');
+ }
+ if (ba[i] == '\n') line++;
+ }
+ if (n == limit && limit != ~0UL) return finish_stdout() ? 2 : differ;
+ if (la != lb) {
+ const char *sh = la < lb ? ops[0] : ops[1];
+ if (!quiet) {
+ if (!n) fprintf(stderr, "cmp: EOF on '%s' which is empty\n", sh);
+ else if (verbose) fprintf(stderr, "cmp: EOF on '%s' after byte %lu\n", sh, (unsigned long)n);
+ else fprintf(stderr, "cmp: EOF on '%s' after byte %lu, line %lu\n", sh, (unsigned long)n, line - 1);
+ }
+ finish_stdout();
+ return 1;
+ }
+ return finish_stdout() ? 2 : differ;
+}
+
+struct dline { const char *s; size_t len; int nonl; };
+struct dfile { struct dline *l; long n; char *buf; int binary; };
+struct dchg { long as, an, bs, bn; };
+
+static int df_fold, df_ws, df_blank, df_ctx = 3, df_mode, df_brief, df_same, df_rec, df_newf, df_text;
+static const char *df_lab[2];
+static const char *df_cmdline, *df_hdr_a, *df_hdr_b;
+static char **df_excl;
+static int df_nexcl;
+
+static int df_read(const char *path, struct dfile *f)
+{
+ int fd;
+ size_t len, i, st = 0;
+ long cap = 0;
+ f->l = NULL; f->n = 0; f->binary = 0;
+ fd = open_input(path);
+ if (fd < 0) return -1;
+ if (read_all(fd, &f->buf, &len) < 0) { if (fd) close(fd); return -1; }
+ if (fd) close(fd);
+ for (i = 0; i < len && i < 8192; i++) if (!f->buf[i]) { f->binary = 1; break; }
+ for (i = 0; i < len; i++) {
+ if (f->buf[i] == '\n') {
+ if (f->n == cap) { cap = cap ? cap * 2 : 128; f->l = xrealloc(f->l, cap * sizeof *f->l); }
+ f->l[f->n].s = f->buf + st; f->l[f->n].len = i - st; f->l[f->n].nonl = 0; f->n++;
+ st = i + 1;
+ }
+ }
+ if (st < len) {
+ if (f->n == cap) { cap = cap ? cap * 2 : 128; f->l = xrealloc(f->l, cap * sizeof *f->l); }
+ f->l[f->n].s = f->buf + st; f->l[f->n].len = len - st; f->l[f->n].nonl = 1; f->n++;
+ }
+ return 0;
+}
+
+static char *df_key(const struct dline *l)
+{
+ char *k = xmalloc(l->len + 2);
+ size_t i, o = 0;
+ int sp = 0;
+ for (i = 0; i < l->len; i++) {
+ unsigned char c = l->s[i];
+ if (df_ws && (c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f')) continue;
+ if (df_blank && (c == ' ' || c == '\t')) { if (!sp) k[o++] = ' '; sp = 1; continue; }
+ sp = 0;
+ k[o++] = df_fold ? tolower(c) : c;
+ }
+ if (df_blank && o && k[o - 1] == ' ') o--;
+ k[o++] = l->nonl && !df_ws && !df_blank ? 1 : 2;
+ k[o] = 0;
+ return k;
+}
+
+struct dkey { char *k; long idx; };
+static int df_kcmp(const void *a, const void *b) { return strcmp(((const struct dkey *)a)->k, ((const struct dkey *)b)->k); }
+
+/* ids[i] equal for equal lines; returns edit flags */
+static void df_edit(struct dfile *a, struct dfile *b, char **da, char **ib)
+{
+ long n = a->n, m = b->n, i, id = 0, s, e, N, M, D = 0, d, k, x, y, off;
+ int *ia = xmalloc((n + 1) * sizeof(int)), *ibb = xmalloc((m + 1) * sizeof(int)), **trace, *V;
+ struct dkey *ks = xmalloc((n + m + 1) * sizeof *ks);
+ char *dd = xmalloc(n + 1), *ii = xmalloc(m + 1);
+ memset(dd, 0, n + 1); memset(ii, 0, m + 1);
+ for (i = 0; i < n; i++) { ks[i].k = df_key(&a->l[i]); ks[i].idx = i; }
+ for (i = 0; i < m; i++) { ks[n + i].k = df_key(&b->l[i]); ks[n + i].idx = n + i; }
+ qsort(ks, n + m, sizeof *ks, df_kcmp);
+ for (i = 0; i < n + m; i++) {
+ if (i && strcmp(ks[i].k, ks[i - 1].k)) id++;
+ if (ks[i].idx < n) ia[ks[i].idx] = id; else ibb[ks[i].idx - n] = id;
+ }
+ for (s = 0; s < n && s < m && ia[s] == ibb[s]; s++) ;
+ for (e = 0; e < n - s && e < m - s && ia[n - 1 - e] == ibb[m - 1 - e]; e++) ;
+ N = n - s - e; M = m - s - e;
+ if (N || M) {
+ int *A = ia + s, *B = ibb + s;
+ long max = N + M, found = 0;
+ off = max;
+ V = xmalloc((2 * max + 2) * sizeof(int));
+ trace = xmalloc((max + 2) * sizeof *trace);
+ V[off + 1] = 0;
+ for (d = 0; d <= max && !found; d++) {
+ trace[d] = xmalloc((2 * d + 3) * sizeof(int));
+ for (k = -d; k <= d; k += 2) {
+ if (k == -d || (k != d && V[off + k - 1] < V[off + k + 1])) x = V[off + k + 1]; else x = V[off + k - 1] + 1;
+ y = x - k;
+ while (x < N && y < M && A[x] == B[y]) { x++; y++; }
+ V[off + k] = x;
+ if (x >= N && y >= M) { found = 1; D = d; break; }
+ }
+ for (k = -d; k <= d; k++) trace[d][k + d] = V[off + k];
+ }
+ x = N; y = M;
+ for (d = D; d > 0; d--) {
+ int *pv = trace[d - 1];
+ long pk, px, py;
+ k = x - y;
+ if (k == -d || (k != d && pv[k - 1 + d - 1] < pv[k + 1 + d - 1])) pk = k + 1; else pk = k - 1;
+ px = pv[pk + d - 1]; py = px - pk;
+ if (pk == k + 1) ii[s + py] = 1; else dd[s + px] = 1;
+ x = px; y = py;
+ }
+ }
+ *da = dd; *ib = ii;
+}
+
+static void df_print(struct dfile *f, long i, const char *pre)
+{
+ fputs(pre, stdout);
+ fwrite(f->l[i].s, 1, f->l[i].len, stdout);
+ putchar('\n');
+ if (f->l[i].nonl) puts("\\ No newline at end of file");
+}
+
+static void df_stamp(const char *path, const char *lab, const char *pre)
+{
+ struct stat st;
+ char tb[64], tz[16];
+ time_t t;
+ struct tm *tm;
+ if (lab) { printf("%s %s\n", pre, lab); return; }
+ if (pre[0] == '*' || pre[0] == '-' && pre[1] == 'C') {
+ if (stat(path, &st) < 0) { printf("%s %s\n", pre, path); return; }
+ t = st.st_mtime; tm = localtime(&t);
+ strftime(tb, sizeof tb, "%a %b %e %H:%M:%S %Y", tm);
+ printf("%s %s\t%s\n", pre[0] == '*' ? "***" : "---", path, tb);
+ return;
+ }
+ if (stat(path, &st) < 0) { printf("%s %s\n", pre, path); return; }
+ t = st.st_mtime; tm = localtime(&t);
+ strftime(tb, sizeof tb, "%Y-%m-%d %H:%M:%S", tm);
+ strftime(tz, sizeof tz, "%z", tm);
+ if (!strcmp(tz, "+0000")) tz[0] = '-';
+ printf("%s %s\t%s.%09lu %s\n", pre, path, tb, (unsigned long)st.st_mtime_nsec, tz);
+}
+
+static void df_range(long first, long len, char *out)
+{
+ if (len == 1) sprintf(out, "%ld", first + 1);
+ else if (len == 0) sprintf(out, "%ld,0", first);
+ else sprintf(out, "%ld,%ld", first + 1, first + len);
+}
+
+static int df_files(const char *pa, const char *pb, const char *la, const char *lb)
+{
+ struct dfile a, b;
+ char *da, *ib;
+ struct dchg *ch = NULL;
+ long nch = 0, cap = 0, i, j, g;
+ int ra = 0, rb = 0;
+ if (pa) ra = df_read(pa, &a); else { a.l = NULL; a.n = 0; a.binary = 0; }
+ if (pb) rb = df_read(pb, &b); else { b.l = NULL; b.n = 0; b.binary = 0; }
+ if (ra < 0) { perr("%s", pa); return 2; }
+ if (rb < 0) { perr("%s", pb); return 2; }
+ if ((a.binary || b.binary) && !df_text) {
+ if (a.n == b.n && a.n >= 0) {
+ long k; int same = 1;
+ if (a.n == b.n) { for (k = 0; k < a.n && same; k++) same = a.l[k].len == b.l[k].len && !memcmp(a.l[k].s, b.l[k].s, a.l[k].len) && a.l[k].nonl == b.l[k].nonl; }
+ else same = 0;
+ if (same) { if (df_same) printf("Files %s and %s are identical\n", la, lb); return 0; }
+ }
+ if (df_hdr_a && !df_brief) { printf("diff%s %s %s\n", df_cmdline, df_hdr_a, df_hdr_b); df_hdr_a = NULL; }
+ printf(df_brief ? "Files %s and %s differ\n" : "Binary files %s and %s differ\n", la, lb);
+ return 1;
+ }
+ df_edit(&a, &b, &da, &ib);
+ if (df_hdr_a) { int any = 0; for (i = 0; i < a.n; i++) if (da[i]) any = 1; for (i = 0; i < b.n; i++) if (ib[i]) any = 1; if (any && !df_brief) { printf("diff%s %s %s\n", df_cmdline, df_hdr_a, df_hdr_b); df_hdr_a = NULL; } }
+ for (i = 0, j = 0; i < a.n || j < b.n; ) {
+ if (i < a.n && da[i]) {
+ long as = i, bs = j;
+ while (i < a.n && da[i]) i++;
+ while (j < b.n && ib[j]) j++;
+ if (nch == cap) { cap = cap ? cap * 2 : 16; ch = xrealloc(ch, cap * sizeof *ch); }
+ ch[nch].as = as; ch[nch].an = i - as; ch[nch].bs = bs; ch[nch].bn = j - bs; nch++;
+ } else if (j < b.n && ib[j]) {
+ long bs = j, as = i;
+ while (j < b.n && ib[j]) j++;
+ if (nch == cap) { cap = cap ? cap * 2 : 16; ch = xrealloc(ch, cap * sizeof *ch); }
+ ch[nch].as = as; ch[nch].an = 0; ch[nch].bs = bs; ch[nch].bn = j - bs; nch++;
+ } else { i++; j++; }
+ }
+ if (!nch) { if (df_same) printf("Files %s and %s are identical\n", la, lb); return 0; }
+ if (df_brief) { printf("Files %s and %s differ\n", la, lb); return 1; }
+ if (df_mode == 'u' || df_mode == 'c') {
+ int u = df_mode == 'u';
+ df_stamp(pa ? pa : "/dev/null", df_lab[0], u ? "---" : "***");
+ df_stamp(pb ? pb : "/dev/null", df_lab[1], u ? "+++" : "-C-");
+ for (g = 0; g < nch; ) {
+ long h = g, af, al, bf, bl, pos, k;
+ while (h + 1 < nch && ch[h + 1].as - (ch[h].as + ch[h].an) <= 2 * df_ctx) h++;
+ af = ch[g].as - df_ctx; if (af < 0) af = 0;
+ al = ch[h].as + ch[h].an + df_ctx; if (al > a.n) al = a.n;
+ bf = ch[g].bs - (ch[g].as - af);
+ bl = ch[h].bs + ch[h].bn + (al - (ch[h].as + ch[h].an));
+ if (u) {
+ char ra_[48], rb_[48];
+ long an_ = al - af, bn_ = bl - bf;
+ if (an_ == 1) sprintf(ra_, "%ld", af + 1); else sprintf(ra_, "%ld,%ld", an_ ? af + 1 : af, an_);
+ if (bn_ == 1) sprintf(rb_, "%ld", bf + 1); else sprintf(rb_, "%ld,%ld", bn_ ? bf + 1 : bf, bn_);
+ printf("@@ -%s +%s @@\n", ra_, rb_);
+ pos = af;
+ for (k = g; k <= h; k++) {
+ long x;
+ for (x = pos; x < ch[k].as; x++) df_print(&a, x, " ");
+ for (x = 0; x < ch[k].an; x++) df_print(&a, ch[k].as + x, "-");
+ for (x = 0; x < ch[k].bn; x++) df_print(&b, ch[k].bs + x, "+");
+ pos = ch[k].as + ch[k].an;
+ }
+ for (; pos < al; pos++) df_print(&a, pos, " ");
+ } else {
+ int hasa = 0, hasb = 0;
+ char r1[48], r2[48];
+ long x;
+ for (k = g; k <= h; k++) { if (ch[k].an) hasa = 1; if (ch[k].bn) hasb = 1; }
+ puts("***************");
+ if (al - af == 0) sprintf(r1, "%ld", af); else if (al - af == 1) sprintf(r1, "%ld", af + 1); else sprintf(r1, "%ld,%ld", af + 1, al);
+ printf("*** %s ****\n", r1);
+ if (hasa) {
+ pos = af;
+ for (k = g; k <= h; k++) {
+ for (x = pos; x < ch[k].as; x++) df_print(&a, x, " ");
+ for (x = 0; x < ch[k].an; x++) df_print(&a, ch[k].as + x, ch[k].bn ? "! " : "- ");
+ pos = ch[k].as + ch[k].an;
+ }
+ for (; pos < al; pos++) df_print(&a, pos, " ");
+ }
+ if (bl - bf == 0) sprintf(r2, "%ld", bf); else if (bl - bf == 1) sprintf(r2, "%ld", bf + 1); else sprintf(r2, "%ld,%ld", bf + 1, bl);
+ printf("--- %s ----\n", r2);
+ if (hasb) {
+ pos = af;
+ for (k = g; k <= h; k++) {
+ for (x = pos; x < ch[k].as; x++) df_print(&a, x, " ");
+ for (x = 0; x < ch[k].bn; x++) df_print(&b, ch[k].bs + x, ch[k].an ? "! " : "+ ");
+ pos = ch[k].as + ch[k].an;
+ }
+ for (; pos < al; pos++) df_print(&a, pos, " ");
+ }
+ }
+ g = h + 1;
+ }
+ return 1;
+ }
+ for (g = 0; g < nch; g++) {
+ char r1[48], r2[48];
+ long x;
+ if (ch[g].an) df_range(ch[g].as, ch[g].an, r1); else sprintf(r1, "%ld", ch[g].as);
+ if (ch[g].bn) df_range(ch[g].bs, ch[g].bn, r2); else sprintf(r2, "%ld", ch[g].bs);
+ printf("%s%c%s\n", r1, !ch[g].an ? 'a' : !ch[g].bn ? 'd' : 'c', r2);
+ for (x = 0; x < ch[g].an; x++) df_print(&a, ch[g].as + x, "< ");
+ if (ch[g].an && ch[g].bn) puts("---");
+ for (x = 0; x < ch[g].bn; x++) df_print(&b, ch[g].bs + x, "> ");
+ }
+ return 1;
+}
+
+static int df_name_cmp(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); }
+
+static int df_excluded(const char *name)
+{
+ int i;
+ for (i = 0; i < df_nexcl; i++) if (glob_match(df_excl[i], name)) return 1;
+ return 0;
+}
+
+static char **df_list(const char *dir, int *n)
+{
+ DIR *d = opendir(dir);
+ struct dirent *e;
+ char **v = NULL;
+ int cap = 0;
+ *n = 0;
+ if (!d) return NULL;
+ while ((e = readdir(d))) {
+ if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..") || df_excluded(e->d_name)) continue;
+ if (*n == cap) { cap = cap ? cap * 2 : 32; v = xrealloc(v, cap * sizeof *v); }
+ v[(*n)++] = xstrdup(e->d_name);
+ }
+ closedir(d);
+ qsort(v, *n, sizeof *v, df_name_cmp);
+ return v;
+}
+
+static char *df_join(const char *d, const char *n)
+{
+ size_t dl = strlen(d);
+ char *r = xmalloc(dl + strlen(n) + 2);
+ sprintf(r, "%s%s%s", d, dl && d[dl - 1] == '/' ? "" : "/", n);
+ return r;
+}
+
+
+
+static int df_pair(const char *pa, const char *pb, int top)
+{
+ struct stat sa, sb;
+ int ea = pa ? stat(pa, &sa) : -1, eb = pb ? stat(pb, &sb) : -1, r = 0;
+ if (pa && ea < 0 && !df_newf) { perr("%s", pa); return 2; }
+ if (pb && eb < 0 && !df_newf) { perr("%s", pb); return 2; }
+ if (ea == 0 && eb == 0 && S_ISDIR(sa.st_mode) && S_ISDIR(sb.st_mode)) {
+ int na, nb, i = 0, j = 0;
+ char **va = df_list(pa, &na), **vb = df_list(pb, &nb);
+ while (i < na || j < nb) {
+ int c = i >= na ? 1 : j >= nb ? -1 : strcmp(va[i], vb[j]);
+ char *x = NULL, *y = NULL;
+ int rr;
+ if (c < 0 && !df_newf) { printf("Only in %s: %s\n", pa, va[i]); r = r < 1 ? 1 : r; i++; continue; }
+ if (c > 0 && !df_newf) { printf("Only in %s: %s\n", pb, vb[j]); r = r < 1 ? 1 : r; j++; continue; }
+ if (c <= 0) x = df_join(pa, va[i]);
+ if (c >= 0) y = df_join(pb, vb[j]);
+ {
+ struct stat t;
+ int isdir = (x && stat(x, &t) == 0 && S_ISDIR(t.st_mode)) || (y && stat(y, &t) == 0 && S_ISDIR(t.st_mode));
+ if (isdir && !df_rec) { printf("Common subdirectories: %s and %s\n", x, y); rr = 0; }
+ else {
+ if (!isdir) { df_hdr_a = x ? x : df_join(pa, vb[j]); df_hdr_b = y ? y : df_join(pb, va[i]); }
+ rr = df_pair(x ? x : NULL, y ? y : NULL, 0);
+ df_hdr_a = NULL;
+ }
+ if (!x || !y) { }
+ }
+ if (rr > r) r = rr;
+ if (c <= 0) i++;
+ if (c >= 0) j++;
+ }
+ return r;
+ }
+ if (ea == 0 && S_ISDIR(sa.st_mode) && !(eb == 0 && S_ISDIR(sb.st_mode))) {
+ if (eb == 0 && pb) { char *base = strrchr(pb, '/'); (void)base; }
+ if (eb < 0 && df_newf) { if (!df_rec) return 2; }
+ { const char *bn = strrchr(pb ? pb : "", '/'); char *np = df_join(pa, bn ? bn + 1 : pb); return df_pair(np, pb, 0); }
+ }
+ if (eb == 0 && S_ISDIR(sb.st_mode) && !(ea == 0 && S_ISDIR(sa.st_mode))) {
+ const char *bn = strrchr(pa ? pa : "", '/'); char *np = df_join(pb, bn ? bn + 1 : pa);
+ return df_pair(pa, np, 0);
+ }
+ if (ea == 0 && eb == 0 && S_ISREG(sa.st_mode) && S_ISREG(sb.st_mode) && sa.st_dev == sb.st_dev && sa.st_ino == sb.st_ino) {
+ if (df_same) printf("Files %s and %s are identical\n", pa, pb);
+ return 0;
+ }
+ (void)top;
+ return df_files(ea == 0 ? pa : NULL, eb == 0 ? pb : NULL, pa ? pa : "/dev/null", pb ? pb : "/dev/null");
+}
+
+static int t_diff(int argc, char **argv)
+{
+ static const struct opt lo[] = { { "brief", 0, 'q' }, { "report-identical-files", 0, 's' }, { "recursive", 0, 'r' }, { "new-file", 0, 'N' }, { "text", 0, 'a' },
+ { "ignore-case", 0, 'i' }, { "ignore-all-space", 0, 'w' }, { "ignore-space-change", 0, 'b' }, { "ignore-blank-lines", 0, 'B' }, { "unified", 2, 'U' },
+ { "context", 2, 'C' }, { "label", 1, 1000 }, { "exclude", 1, 'x' }, { "show-c-function", 0, 'p' }, { "expand-tabs", 0, 't' }, { "initial-tab", 0, 'T' },
+ { "normal", 0, 1001 }, { NULL, 0, 0 } };
+ int c, i, r, nl = 0;
+ unsigned long v;
+ static char cl[512];
+ usage_status = 2;
+ opts_reset();
+ while ((c = getopts(argc, argv, "qsrNaiwbBu::c::U:C:x:ptTy", lo))) {
+ switch (c) {
+ case 'q': df_brief = 1; break; case 's': df_same = 1; break; case 'r': df_rec = 1; break; case 'N': df_newf = 1; break; case 'a': df_text = 1; break;
+ case 'i': df_fold = 1; break; case 'w': df_ws = 1; break; case 'b': df_blank = 1; break; case 'B': break;
+ case 'u': df_mode = 'u'; if (optarg && parse_ulong(optarg, &v)) df_ctx = v; break;
+ case 'c': df_mode = 'c'; if (optarg && parse_ulong(optarg, &v)) df_ctx = v; break;
+ case 'U': if (!parse_ulong(optarg, &v)) die(2, "invalid context length '%s'", optarg); df_mode = 'u'; df_ctx = v; break;
+ case 'C': if (!parse_ulong(optarg, &v)) die(2, "invalid context length '%s'", optarg); df_mode = 'c'; df_ctx = v; break;
+ case 'x': df_excl = xrealloc(df_excl, (df_nexcl + 1) * sizeof *df_excl); df_excl[df_nexcl++] = optarg; break;
+ case 1000: if (nl < 2) df_lab[nl++] = optarg; break;
+ case 1001: df_mode = 0; break;
+ default: break;
+ }
+ }
+ if (nops < 2) { if (!nops) { say("missing operand after '%s'", argv[argc - 1]); } else say("missing operand after '%s'", ops[0]); usage_error(); }
+ if (nops > 2) extra_operand(ops[2]);
+ for (i = 1; i < argc; i++) {
+ int isop = 0, k;
+ for (k = 0; k < nops; k++) if (argv[i] == ops[k]) isop = 1;
+ if (!isop) { strcat(cl, " "); strncat(cl, argv[i], 60); }
+ }
+ df_cmdline = cl;
+ r = df_pair(ops[0], ops[1], 1);
+ if (finish_stdout()) return 2;
+ return r;
+}
+
/* ---- the table and main ---- */
struct applet { const char *name; int (*fn)(int, char **); const char *usage; const char *about; };
@@ -11765,6 +12218,8 @@ static const struct applet applets[] = {
{ "stty", t_stty, "[-F DEVICE] [--file=DEVICE] [SETTING]...", "Print or change terminal characteristics." },
{ "pr", t_pr, "[OPTION]... [FILE]...", "Paginate or columnate FILE(s) for printing." },
{ "fmt", t_fmt, "[-WIDTH] [OPTION]... [FILE]...", "Reformat each paragraph in the FILE(s), writing to standard output." },
+ { "cmp", t_cmp, "[OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]", "Compare two files byte by byte." },
+ { "diff", t_diff, "[OPTION]... FILES", "Compare FILES line by line." },
{ "dir", t_dir, "[OPTION]... [FILE]...", "List directory contents in columns, with C-style escapes." },
{ "dirname", t_dirname, "[OPTION] NAME...", "Print NAME with its last component removed." },
{ "du", t_du, "[OPTION]... [FILE]...", "Summarize device usage of the set of FILEs, recursively for directories." },
diff --git a/tests/cases b/tests/cases
@@ -1810,3 +1810,10 @@ echo 2048 | $X numfmt --to=iec
# pr
printf '1\n2\n3\n4\n5\n6\n' | $X pr -t -3
printf '1\n2\n3\n' | $X pr -t -n
+# diff cmp
+printf 'a\nb\nc\n' > d1; printf 'a\nx\nc\n' > d2; $X diff d1 d2
+printf 'a\nb\nc\n' > d1; printf 'a\nx\nc\n' > d2; $X diff -q d1 d2
+printf 'a\nb\nc\n' > d1; printf 'a\nx\nc\n' > d2; $X diff -u --label one --label two d1 d2
+printf 'a\nb\nc\n' > d1; printf 'a\nx\nc\n' > d2; $X diff -c --label one --label two d1 d2 | tail -n +3
+printf 'abc' > c1; printf 'abd' > c2; $X cmp -l c1 c2
+printf 'abc' > c1; printf 'abd' > c2; $X cmp c1 c2