Mercurial > hg > toybox
annotate toys/cmp.c @ 439:a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 07 Feb 2012 21:32:32 -0600 |
parents | 580f4647fe2e |
children | 7cff5420c90a |
rev | line source |
---|---|
434 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * cmp.c - Compare two files. | |
4 * | |
5 * Copyright 2012 Timothy Elliott <tle@holymonkey.com> | |
6 * | |
7 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cmp.html | |
8 | |
9 USE_CMP(NEWTOY(cmp, "<2>2ls", TOYFLAG_USR|TOYFLAG_BIN)) | |
10 | |
11 config CMP | |
12 bool "cmp" | |
13 default y | |
14 help | |
15 usage: cmp [-l] [-s] FILE1 FILE2 | |
16 | |
17 Compare the contents of two files. | |
18 | |
19 -l show all differing bytes | |
20 -s silent | |
21 */ | |
22 | |
23 #include "toys.h" | |
24 | |
25 #define FLAG_s 1 | |
26 #define FLAG_l 2 | |
27 | |
28 int get_fd(char *file) | |
29 { | |
30 int fd; | |
31 | |
32 if (!strcmp(file,"-")) fd=0; | |
439
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
33 else fd = xopen(file, O_RDONLY); |
434 | 34 return fd; |
35 } | |
36 | |
37 void do_cmp(int fd1, int fd2, char *file1, char *file2, char *buf1, char *buf2, | |
439
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
38 size_t size) |
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
39 { |
434 | 40 int i, len1, len2, min_len; |
41 size_t byte_no = 1, line_no = 1; | |
42 | |
43 for (;;) { | |
44 len1 = read(fd1, buf1, size); | |
45 len2 = read(fd2, buf2, size); | |
46 | |
47 min_len = len1 < len2 ? len1 : len2; | |
48 for (i=0; i<min_len; i++) { | |
49 if (buf1[i] != buf2[i]) { | |
50 toys.exitval = 1; | |
439
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
51 if (toys.optflags & FLAG_l) |
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
52 printf("%ld %o %o\n", (long)byte_no, buf1[i], buf2[i]); |
434 | 53 else { |
54 if (!(toys.optflags & FLAG_s)) { | |
439
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
55 printf("%s %s differ: char %ld, line %ld\n", |
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
56 file1, file2, (long)byte_no, |
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
57 (long)line_no); |
434 | 58 } |
59 return; | |
60 } | |
61 | |
62 } | |
63 byte_no++; | |
64 if (buf1[i] == '\n') line_no++; | |
65 } | |
66 if (len1 != len2) { | |
67 if (!(toys.optflags & FLAG_s)) { | |
68 fdprintf(2, "cmp: EOF on %s\n", | |
69 len1 < len2 ? file1 : file2); | |
70 } | |
71 toys.exitval = 1; | |
72 break; | |
73 } | |
74 if (len1 < 1) break; | |
75 } | |
76 } | |
77 | |
78 void cmp_main(void) | |
79 { | |
80 char *file1 = toys.optargs[0]; | |
81 char *file2 = toys.optargs[1]; | |
82 int fd1, fd2, size=sizeof(toybuf); | |
83 char *toybuf2 = xmalloc(size); | |
84 | |
85 fd1 = get_fd(file1); | |
86 fd2 = get_fd(file2); | |
87 | |
88 do_cmp(fd1, fd2, file1, file2, toybuf, toybuf2, size); | |
89 | |
439
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
90 if (CFG_TOYBOX_FREE) { |
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
91 close(fd1); |
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
92 close(fd2); |
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
93 free(toybuf2); |
a64003e250d4
Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
Rob Landley <rob@landley.net>
parents:
434
diff
changeset
|
94 } |
434 | 95 } |
96 |