annotate toys/comm.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents feb909b2e6aa
children 2b40588a3d25
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
557
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
2 *
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
3 * comm.c - select or reject lines common to two files
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
4 *
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
5 * Copyright 2012 Ilya Kuzmich <ikv@safe-mail.net>
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
6 *
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
7 * See http://pubs.opengroup.org/onlinepubs/009695399/utilities/comm.html
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
8
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
9 USE_COMM(NEWTOY(comm, "123", TOYFLAG_USR|TOYFLAG_BIN))
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
10
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
11 config COMM
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
12 bool "comm"
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
13 default n
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
14 help
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
15 usage: comm [-123] FILE1 FILE2
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
16
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
17 Reads FILE1 and FILE2, which should be ordered, and produces three text
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
18 columns as output: lines only in FILE1; lines only in FILE2; and lines
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
19 in both files. Filename "-" is a synonym for stdin.
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
20
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
21 -1 suppress the output column of lines unique to FILE1
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
22 -2 suppress the output column of lines unique to FILE2
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
23 -3 suppress the output column of lines duplicated in FILE1 and FILE2
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
24 */
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
25
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
26 #include "toys.h"
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
27
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
28 #define FLAG_SUPPRESS_3 1
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
29 #define FLAG_SUPPRESS_2 2
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
30 #define FLAG_SUPPRESS_1 4
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
31
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
32 static void writeline(const char *line, int col)
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
33 {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
34 if (col == 0 && toys.optflags & FLAG_SUPPRESS_1) return;
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
35 else if (col == 1) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
36 if (toys.optflags & FLAG_SUPPRESS_2) return;
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
37 if (!(toys.optflags & FLAG_SUPPRESS_1)) putchar('\t');
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
38 } else if (col == 2) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
39 if (toys.optflags & FLAG_SUPPRESS_3) return;
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
40 if (!(toys.optflags & FLAG_SUPPRESS_1)) putchar('\t');
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
41 if (!(toys.optflags & FLAG_SUPPRESS_2)) putchar('\t');
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
42 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
43 puts(line);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
44 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
45
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
46 void comm_main(void)
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
47 {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
48 int file[2];
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
49 char *line[2];
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
50 int i;
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
51
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
52 if (toys.optc != 2)
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
53 perror_exit("exactly 2 operands required");
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
54
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
55 if (toys.optflags == (FLAG_SUPPRESS_1 | FLAG_SUPPRESS_2 | FLAG_SUPPRESS_3))
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
56 return;
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
57
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
58 for (i = 0; i < 2; i++) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
59 file[i] = strcmp("-", toys.optargs[i]) ? xopen(toys.optargs[i], O_RDONLY) : 0;
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
60 line[i] = get_line(file[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
61 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
62
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
63 while (line[0] && line[1]) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
64 int order = strcmp(line[0], line[1]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
65
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
66 if (order == 0) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
67 writeline(line[0], 2);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
68 for (i = 0; i < 2; i++) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
69 free(line[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
70 line[i] = get_line(file[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
71 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
72 } else {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
73 i = order < 0 ? 0 : 1;
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
74 writeline(line[i], i);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
75 free(line[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
76 line[i] = get_line(file[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
77 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
78 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
79
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
80 /* print rest of the longer file */
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
81 for (i = line[0] ? 0 : 1; line[i];) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
82 writeline(line[i], i);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
83 free(line[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
84 line[i] = get_line(file[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
85 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
86
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
87 if (CFG_TOYBOX_FREE) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
88 for (i = 0; i < 2; i--)
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
89 xclose(file[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
90 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
91 }