1 /* cmp.c - Compare two files.
2 *
3 * Copyright 2012 Timothy Elliott <tle@holymonkey.com>
4 *
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/cmp.html
6
7 USE_CMP(NEWTOY(cmp, "<1>4ls(silent)(quiet)n#<1[!ls]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
8
9 config CMP
10 bool "cmp"
11 default y
12 help
13 usage: cmp [-ls] [-n LEN] FILE1 [FILE2 [SKIP1 [SKIP2]]]
14
15 Compare the contents of files (vs stdin if only one given), optionally
16 skipping bytes at start.
17
18 -l Show all differing bytes
19 -n LEN Compare at most LEN bytes
20 -s Silent
21 */
22
23 #define FOR_cmp
24 #include "toys.h"
25
GLOBALS(long n;int fd;char * name;)26 GLOBALS(
27 long n;
28
29 int fd;
30 char *name;
31 )
32
33 // We hijack loopfiles() to open and understand the "-" filename for us.
34 static void do_cmp(int fd, char *name)
35 {
36 int i, len1, len2, min_len, size = sizeof(toybuf)/2;
37 long byte_no = 1, line_no = 1;
38 char *buf2 = toybuf+size;
39
40 if (toys.optc>(i = 2+!!TT.fd) && lskip(fd, atolx(toys.optargs[i])))
41 error_exit("EOF on %s", name);
42
43 // First time through, cache the data and return.
44 if (!TT.fd) {
45 TT.name = name;
46 // On return the old filehandle is closed, and this assures that even
47 // if we were called with stdin closed, the new filehandle != 0.
48 TT.fd = dup(fd);
49 return;
50 }
51
52 toys.exitval = 0;
53
54 for (;!FLAG(n) || TT.n;) {
55 if (FLAG(n)) TT.n -= size = minof(size, TT.n);
56 len1 = readall(TT.fd, toybuf, size);
57 len2 = readall(fd, buf2, size);
58 min_len = minof(len1, len2);
59 for (i=0; i<min_len; i++) {
60 if (toybuf[i] != buf2[i]) {
61 toys.exitval = 1;
62 if (FLAG(l)) printf("%ld %o %o\n", byte_no, toybuf[i], buf2[i]);
63 else {
64 if (!FLAG(s)) printf("%s %s differ: char %ld, line %ld\n",
65 TT.name, name, byte_no, line_no);
66 goto out;
67 }
68 }
69 byte_no++;
70 if (toybuf[i] == '\n') line_no++;
71 }
72 if (len1 != len2) {
73 if (!FLAG(s)) error_msg("EOF on %s", len1 < len2 ? TT.name : name);
74 else toys.exitval = 1;
75 break;
76 }
77 if (len1 < 1) break;
78 }
79 out:
80 if (CFG_TOYBOX_FREE) close(TT.fd);
81 xexit();
82 }
83
cmp_main(void)84 void cmp_main(void)
85 {
86 toys.exitval = 2;
87 loopfiles_rw(toys.optargs, O_CLOEXEC|(WARN_ONLY*!FLAG(s)), 0, do_cmp);
88 if (toys.optc == 1) do_cmp(0, "-");
89 }
90