Mercurial > hg > toybox
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 |
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 * | |
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 | 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 | |
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 | 24 #include "toys.h" |
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 | 27 int fd; |
440 | 28 char *name; |
29 ) | |
30 | |
31 // This handles opening the file and | |
434 | 32 |
440 | 33 void do_cmp(int fd, char *name) |
34 { | |
35 int i, len1, len2, min_len, size = sizeof(toybuf)/2; | |
36 long byte_no = 1, line_no = 1; | |
37 char *buf2 = toybuf+size; | |
434 | 38 |
440 | 39 // First time through, cache the data and return. |
40 if (!TT.fd) { | |
41 TT.name = name; | |
42 // On return the old filehandle is closed, and this assures that even | |
43 // if we were called with stdin closed, the new filehandle != 0. | |
44 TT.fd = dup(fd); | |
45 return; | |
46 } | |
434 | 47 |
48 for (;;) { | |
440 | 49 len1 = readall(TT.fd, toybuf, size); |
50 len2 = readall(fd, buf2, size); | |
434 | 51 |
52 min_len = len1 < len2 ? len1 : len2; | |
53 for (i=0; i<min_len; i++) { | |
440 | 54 if (toybuf[i] != buf2[i]) { |
434 | 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 | 57 printf("%ld %o %o\n", byte_no, toybuf[i], buf2[i]); |
434 | 58 else { |
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 | 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 | 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 | 65 } |
66 } | |
67 byte_no++; | |
440 | 68 if (toybuf[i] == '\n') line_no++; |
434 | 69 } |
70 if (len1 != len2) { | |
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 | 73 len1 < len2 ? TT.name : name); |
434 | 74 } |
75 toys.exitval = 1; | |
76 break; | |
77 } | |
78 if (len1 < 1) break; | |
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 | 81 if (CFG_TOYBOX_FREE) close(TT.fd); |
434 | 82 } |
83 | |
84 void cmp_main(void) | |
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 | 87 } |
88 |