annotate toys/cmp.c @ 434:580f4647fe2e

Implement cmp
author Timothy Elliott <tle@holymonkey.com>
date Mon, 06 Feb 2012 01:28:40 -0800
parents
children a64003e250d4
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;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
33 else if (0>(fd = open(file, O_RDONLY, 0))) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
34 perror_exit("%s", file);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
35 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
36 return fd;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
37 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
38
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
39 void do_cmp(int fd1, int fd2, char *file1, char *file2, char *buf1, char *buf2,
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
40 size_t size) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
41 int i, len1, len2, min_len;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
42 size_t byte_no = 1, line_no = 1;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
43
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
44 for (;;) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
45 len1 = read(fd1, buf1, size);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
46 len2 = read(fd2, buf2, size);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
47
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
48 min_len = len1 < len2 ? len1 : len2;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
49 for (i=0; i<min_len; i++) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
50 if (buf1[i] != buf2[i]) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
51 toys.exitval = 1;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
52 if (toys.optflags & FLAG_l) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
53 printf("%d %o %o\n", byte_no, buf1[i],
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
54 buf2[i]);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
55 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
56 else {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
57 if (!(toys.optflags & FLAG_s)) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
58 printf("%s %s differ: char %d, line %d\n",
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
59 file1, file2, byte_no,
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
60 line_no);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
61 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
62 return;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
63 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
64
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
65 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
66 byte_no++;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
67 if (buf1[i] == '\n') line_no++;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
68 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
69 if (len1 != len2) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
70 if (!(toys.optflags & FLAG_s)) {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
71 fdprintf(2, "cmp: EOF on %s\n",
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
72 len1 < len2 ? file1 : file2);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
73 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
74 toys.exitval = 1;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
75 break;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
76 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
77 if (len1 < 1) break;
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
78 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
79 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
80
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
81 void cmp_main(void)
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
82 {
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
83 char *file1 = toys.optargs[0];
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
84 char *file2 = toys.optargs[1];
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
85 int fd1, fd2, size=sizeof(toybuf);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
86 char *toybuf2 = xmalloc(size);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
87
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
88 fd1 = get_fd(file1);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
89 fd2 = get_fd(file2);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
90
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
91 do_cmp(fd1, fd2, file1, file2, toybuf, toybuf2, size);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
92
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
93 close(fd1);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
94 close(fd2);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
95 free(toybuf2);
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
96 }
580f4647fe2e Implement cmp
Timothy Elliott <tle@holymonkey.com>
parents:
diff changeset
97