annotate toys/posix/cmp.c @ 656:6df4ccc0acbe

Regularize command headers, update links to standards documents.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 18:08:51 -0500
parents 2986aa63a021
children 7e846e281e38
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 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/cmp.html
434
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
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
28 DEFINE_GLOBALS(
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
29 int fd;
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
30 char *name;
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
31 )
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
32
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
33 #define TT this.cmp
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
34
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
35 // This handles opening the file and
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
36
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
37 void do_cmp(int fd, char *name)
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
38 {
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
39 int i, len1, len2, min_len, size = sizeof(toybuf)/2;
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
40 long byte_no = 1, line_no = 1;
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
41 char *buf2 = toybuf+size;
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
42
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
43 // First time through, cache the data and return.
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
44 if (!TT.fd) {
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
45 TT.name = name;
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
46 // On return the old filehandle is closed, and this assures that even
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
47 // if we were called with stdin closed, the new filehandle != 0.
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
48 TT.fd = dup(fd);
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
49 return;
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
50 }
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
51
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
52 for (;;) {
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
53 len1 = readall(TT.fd, toybuf, size);
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
54 len2 = readall(fd, buf2, size);
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
55
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
56 min_len = len1 < len2 ? len1 : len2;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
57 for (i=0; i<min_len; i++) {
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
58 if (toybuf[i] != buf2[i]) {
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
59 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
60 if (toys.optflags & FLAG_l)
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
61 printf("%ld %o %o\n", byte_no, toybuf[i], buf2[i]);
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
62 else {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
63 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
64 printf("%s %s differ: char %ld, line %ld\n",
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
65 TT.name, name, byte_no, line_no);
509
e7c742f78361 Only show one normal difference (sans -l) and EOF message should go to stdout, not stderr.
Rob Landley <rob@landley.net>
parents: 440
diff changeset
66 toys.exitval++;
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
67 }
509
e7c742f78361 Only show one normal difference (sans -l) and EOF message should go to stdout, not stderr.
Rob Landley <rob@landley.net>
parents: 440
diff changeset
68 goto out;
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
69 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
70 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
71 byte_no++;
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
72 if (toybuf[i] == '\n') line_no++;
434
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 != len2) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
75 if (!(toys.optflags & FLAG_s)) {
618
26098529bbe6 Put cmp.c EOF notice on stderr
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 556
diff changeset
76 fprintf(stderr, "cmp: EOF on %s\n",
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
77 len1 < len2 ? TT.name : name);
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
78 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
79 toys.exitval = 1;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
80 break;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
81 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
82 if (len1 < 1) break;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
83 }
509
e7c742f78361 Only show one normal difference (sans -l) and EOF message should go to stdout, not stderr.
Rob Landley <rob@landley.net>
parents: 440
diff changeset
84 out:
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
85 if (CFG_TOYBOX_FREE) close(TT.fd);
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
86 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
87
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
88 void cmp_main(void)
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
89 {
556
56a53ff54096 The -s flag includes staying quiet about missing files.
Rob Landley <rob@landley.net>
parents: 509
diff changeset
90 loopfiles_rw(toys.optargs, O_RDONLY, 0, toys.optflags&FLAG_s, do_cmp);
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
91 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
92