annotate toys/comm.c @ 614:2b40588a3d25

Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
author Rob Landley <rob@landley.net>
date Tue, 26 Jun 2012 20:47:01 -0500
parents feb909b2e6aa
children
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
614
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
9 // <# and ># take single digit, so 321 define flags
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
10 USE_COMM(NEWTOY(comm, "<2>2321", TOYFLAG_USR|TOYFLAG_BIN))
557
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
11
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
12 config COMM
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
13 bool "comm"
614
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
14 default y
557
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
15 help
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
16 usage: comm [-123] FILE1 FILE2
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
17
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
18 Reads FILE1 and FILE2, which should be ordered, and produces three text
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
19 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
20 in both files. Filename "-" is a synonym for stdin.
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
21
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
22 -1 suppress the output column of lines unique to FILE1
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
23 -2 suppress the output column of lines unique to FILE2
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
24 -3 suppress the output column of lines duplicated in FILE1 and FILE2
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
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
27 #include "toys.h"
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
28
614
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
29 #define FLAG_1 1
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
30 #define FLAG_2 2
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
31 #define FLAG_3 4
557
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
32
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
33 static void writeline(const char *line, int col)
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
34 {
614
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
35 if (col == 0 && toys.optflags & FLAG_1) return;
557
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
36 else if (col == 1) {
614
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
37 if (toys.optflags & FLAG_2) return;
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
38 if (!(toys.optflags & FLAG_1)) putchar('\t');
557
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
39 } else if (col == 2) {
614
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
40 if (toys.optflags & FLAG_3) return;
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
41 if (!(toys.optflags & FLAG_1)) putchar('\t');
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
42 if (!(toys.optflags & FLAG_2)) putchar('\t');
557
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
43 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
44 puts(line);
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
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
47 void comm_main(void)
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
48 {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
49 int file[2];
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
50 char *line[2];
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
51 int i;
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
52
614
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
53 if (toys.optflags == 7) return;
557
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 for (i = 0; i < 2; i++) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
56 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
57 line[i] = get_line(file[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
58 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
59
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
60 while (line[0] && line[1]) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
61 int order = strcmp(line[0], line[1]);
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 if (order == 0) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
64 writeline(line[0], 2);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
65 for (i = 0; i < 2; i++) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
66 free(line[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
67 line[i] = get_line(file[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
68 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
69 } else {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
70 i = order < 0 ? 0 : 1;
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
71 writeline(line[i], i);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
72 free(line[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
73 line[i] = get_line(file[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
74 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
75 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
76
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
77 /* print rest of the longer file */
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
78 for (i = line[0] ? 0 : 1; line[i];) {
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
79 writeline(line[i], i);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
80 free(line[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
81 line[i] = get_line(file[i]);
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
82 }
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
83
614
2b40588a3d25 Minor cleanups and refactoring. Make FLAG macros closer to what should eventually be generated for us by the build infrastructure.
Rob Landley <rob@landley.net>
parents: 557
diff changeset
84 if (CFG_TOYBOX_FREE) for (i = 0; i < 2; i--) xclose(file[i]);
557
feb909b2e6aa Implement comm.
Ilya Kuzmich <ilya.kuzmich@gmail.com>
parents:
diff changeset
85 }