annotate toys/posix/cmp.c @ 674:7e846e281e38

New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author Rob Landley <rob@landley.net>
date Mon, 08 Oct 2012 00:02:30 -0500
parents 6df4ccc0acbe
children 786841fdb1e0
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
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
23 #define FOR_cmp
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
24 #include "toys.h"
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
25
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
26 GLOBALS(
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
27 int fd;
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
28 char *name;
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
29 )
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
30
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
31 // This handles opening the file and
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
32
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
33 void do_cmp(int fd, char *name)
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 int i, len1, len2, min_len, size = sizeof(toybuf)/2;
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
36 long byte_no = 1, line_no = 1;
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
37 char *buf2 = toybuf+size;
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
38
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
39 // First time through, cache the data and return.
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
40 if (!TT.fd) {
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
41 TT.name = name;
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
42 // 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
43 // 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
44 TT.fd = dup(fd);
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
45 return;
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
46 }
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
47
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
48 for (;;) {
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
49 len1 = readall(TT.fd, toybuf, size);
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
50 len2 = readall(fd, buf2, size);
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 min_len = len1 < len2 ? len1 : len2;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
53 for (i=0; i<min_len; i++) {
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
54 if (toybuf[i] != buf2[i]) {
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
55 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
56 if (toys.optflags & FLAG_l)
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
57 printf("%ld %o %o\n", byte_no, toybuf[i], buf2[i]);
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
58 else {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
59 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
60 printf("%s %s differ: char %ld, line %ld\n",
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
61 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
62 toys.exitval++;
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
63 }
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
64 goto out;
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
65 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
66 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
67 byte_no++;
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
68 if (toybuf[i] == '\n') line_no++;
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 if (len1 != len2) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
71 if (!(toys.optflags & FLAG_s)) {
618
26098529bbe6 Put cmp.c EOF notice on stderr
Elie De Brauwer <eliedebrauwer@gmail.com>
parents: 556
diff changeset
72 fprintf(stderr, "cmp: EOF on %s\n",
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
73 len1 < len2 ? TT.name : name);
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
74 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
75 toys.exitval = 1;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
76 break;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
77 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
78 if (len1 < 1) break;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
79 }
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
80 out:
440
7cff5420c90a More cmp.c shrinkage.
Rob Landley <rob@landley.net>
parents: 439
diff changeset
81 if (CFG_TOYBOX_FREE) close(TT.fd);
434
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
82 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
83
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
84 void cmp_main(void)
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
85 {
556
56a53ff54096 The -s flag includes staying quiet about missing files.
Rob Landley <rob@landley.net>
parents: 509
diff changeset
86 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
87 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
88