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
29
30
31static void do_cmp(int fd, char *name)
32{
33 int i, len1, len2, min_len, size = sizeof(toybuf)/2;
34 long byte_no = 1, line_no = 1;
35 char *buf2 = toybuf+size;
36
37
38 if (!TT.fd) {
39 TT.name = name;
40
41
42 TT.fd = dup(fd);
43 return;
44 }
45
46 toys.exitval = 0;
47
48 for (;;) {
49 len1 = readall(TT.fd, toybuf, size);
50 len2 = readall(fd, buf2, size);
51
52 min_len = len1 < len2 ? len1 : len2;
53 for (i=0; i<min_len; i++) {
54 if (toybuf[i] != buf2[i]) {
55 toys.exitval = 1;
56 if (toys.optflags & FLAG_l)
57 printf("%ld %o %o\n", byte_no, toybuf[i], buf2[i]);
58 else {
59 if (!(toys.optflags & FLAG_s))
60 printf("%s %s differ: char %ld, line %ld\n",
61 TT.name, name, byte_no, line_no);
62 goto out;
63 }
64 }
65 byte_no++;
66 if (toybuf[i] == '\n') line_no++;
67 }
68 if (len1 != len2) {
69 if (!(toys.optflags & FLAG_s))
70 fprintf(stderr, "cmp: EOF on %s\n", len1 < len2 ? TT.name : name);
71 toys.exitval = 1;
72 break;
73 }
74 if (len1 < 1) break;
75 }
76out:
77 if (CFG_TOYBOX_FREE) close(TT.fd);
78}
79
80void cmp_main(void)
81{
82 toys.exitval = 2;
83 loopfiles_rw(toys.optargs, O_CLOEXEC|(WARN_ONLY*!(toys.optflags&FLAG_s)), 0,
84 do_cmp);
85}
86
87