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