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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
2 *
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
3 * cmp.c - Compare two files.
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
4 *
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
5 * Copyright 2012 Timothy Elliott <tle@holymonkey.com>
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
6 *
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
7 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cmp.html
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
8
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
9 USE_CMP(NEWTOY(cmp, "<2>2ls", TOYFLAG_USR|TOYFLAG_BIN))
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
10
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
11 config CMP
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
12 bool "cmp"
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
13 default y
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
14 help
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
15 usage: cmp [-l] [-s] FILE1 FILE2
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
16
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
17 Compare the contents of two files.
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
18
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
19 -l show all differing bytes
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
20 -s silent
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
21 */
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
22
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
23 #include "toys.h"
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
24
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
25 #define FLAG_s 1
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
26 #define FLAG_l 2
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
27
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
28 int get_fd(char *file)
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
29 {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
30 int fd;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
31
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
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
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
34 return fd;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
35 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
36
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
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
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
40 int i, len1, len2, min_len;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
41 size_t byte_no = 1, line_no = 1;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
42
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
43 for (;;) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
44 len1 = read(fd1, buf1, size);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
45 len2 = read(fd2, buf2, size);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
46
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
47 min_len = len1 < len2 ? len1 : len2;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
48 for (i=0; i<min_len; i++) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
49 if (buf1[i] != buf2[i]) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
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
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
53 else {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
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
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
58 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
59 return;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
60 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
61
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
62 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
63 byte_no++;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
64 if (buf1[i] == '\n') line_no++;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
65 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
66 if (len1 != len2) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
67 if (!(toys.optflags & FLAG_s)) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
68 fdprintf(2, "cmp: EOF on %s\n",
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
69 len1 < len2 ? file1 : file2);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
70 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
71 toys.exitval = 1;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
72 break;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
73 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
74 if (len1 < 1) break;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
75 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
76 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
77
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
78 void cmp_main(void)
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
79 {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
80 char *file1 = toys.optargs[0];
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
81 char *file2 = toys.optargs[1];
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
82 int fd1, fd2, size=sizeof(toybuf);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
83 char *toybuf2 = xmalloc(size);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
84
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
85 fd1 = get_fd(file1);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
86 fd2 = get_fd(file2);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
87
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
88 do_cmp(fd1, fd2, file1, file2, toybuf, toybuf2, size);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
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
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
95 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
96