1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#define FOR_cmp
24#include "toys.h"
25
26GLOBALS(
27 long n;
28
29 int fd;
30 char *name;
31)
32
33
34static void do_cmp(int fd, char *name)
35{
36 int i, len1, len2, min_len, size = sizeof(toybuf)/2;
37 long 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
42
43 if (!TT.fd) {
44 TT.name = name;
45
46
47 TT.fd = dup(fd);
48 return;
49 }
50
51 toys.exitval = 0;
52
53 for (;!FLAG(n) || TT.n;) {
54 if (FLAG(n)) TT.n -= size = minof(size, TT.n);
55 len1 = readall(TT.fd, toybuf, size);
56 len2 = readall(fd, buf2, size);
57 min_len = minof(len1, len2);
58 for (i = 0; i<min_len; i++) {
59 if (toybuf[i] != buf2[i]) {
60 toys.exitval = 1;
61 if (FLAG(l)) printf("%lld %o %o\n", byte_no, toybuf[i], buf2[i]);
62 else {
63 if (!FLAG(s)) printf("%s %s differ: char %lld, line %lld\n",
64 TT.name, name, byte_no, line_no);
65 goto out;
66 }
67 }
68 byte_no++;
69 if (toybuf[i] == '\n') line_no++;
70 }
71 if (len1 != len2) {
72 if (!FLAG(s)) {
73 strcpy(toybuf, "EOF on %s after byte %lld, line %lld");
74 if (FLAG(l)) *strchr(toybuf, ',') = 0;
75 error_msg(toybuf, len1 < len2 ? TT.name : name, byte_no-1, line_no-1);
76 } else toys.exitval = 1;
77 break;
78 }
79 if (len1 < 1) break;
80 }
81out:
82 if (CFG_TOYBOX_FREE) close(TT.fd);
83 xexit();
84}
85
86void cmp_main(void)
87{
88 toys.exitval = 2;
89 loopfiles_rw(toys.optargs, O_CLOEXEC|(WARN_ONLY*!FLAG(s)), 0, do_cmp);
90 if (toys.optc == 1) do_cmp(0, "-");
91}
92