1 /* cmp - compare two files Author: Kees J. Bot. */
11 _PROTOTYPE(void fatal
, (char *label
));
12 _PROTOTYPE(int cmp
, (int fd1
, int fd2
));
13 _PROTOTYPE(void Usage
, (void));
14 _PROTOTYPE(int main
, (int argc
, char **argv
));
18 static int loud
= 0, silent
= 0;
19 static char *name1
, *name2
;
27 /* Process the '-l' or '-s' option. */
28 while (argc
> 1 && argv
[1][0] == '-' && argv
[1][1] != 0) {
29 if (argv
[1][2] != 0) Usage();
33 /* '--': no-op option. */
47 if (argc
!= 3) Usage();
49 /* Open the first file, '-' means standard input. */
50 if (argv
[1][0] == '-' && argv
[1][1] == 0) {
55 if ((fd1
= open(name1
, 0)) < 0) fatal(name1
);
58 /* Second file likewise. */
59 if (argv
[2][0] == '-' && argv
[2][1] == 0) {
64 if ((fd2
= open(name2
, 0)) < 0) fatal(name2
);
73 static char buf1
[BLOCK
], buf2
[BLOCK
];
74 int n1
= 0, n2
= 0, i1
= 0, i2
= 0, c1
, c2
;
75 off_t pos
= 0, line
= 1;
76 int eof
= 0, differ
= 0;
82 if ((n1
= read(fd1
, buf1
, sizeof(buf1
))) <= 0) {
83 if (n1
< 0) fatal(name1
);
89 if ((n2
= read(fd2
, buf2
, sizeof(buf2
))) <= 0) {
90 if (n2
< 0) fatal(name2
);
103 printf("%s %s differ: char %ld, line %ld\n",
104 name1
, name2
, pos
+ i1
, line
);
108 printf("%10ld %3o %3o\n", pos
+ i1
, c1
& 0xFF, c2
& 0xFF);
111 if (c1
== '\n') line
++;
113 if (eof
== (1 | 2)) return(differ
);
114 if (!silent
) fprintf(stderr
, "cmp: EOF on %s\n", eof
== 1 ? name1
: name2
);
121 if (!silent
) fprintf(stderr
, "cmp: %s: %s\n", label
, strerror(errno
));
127 fprintf(stderr
, "Usage: cmp [-l | -s] file1 file2\n");