Mercurial > hg > toybox
annotate toys/posix/comm.c @ 656:6df4ccc0acbe
Regularize command headers, update links to standards documents.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 25 Aug 2012 18:08:51 -0500 |
parents | 2986aa63a021 |
children | 7e846e281e38 |
rev | line source |
---|---|
557 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * comm.c - select or reject lines common to two files | |
4 * | |
5 * Copyright 2012 Ilya Kuzmich <ikv@safe-mail.net> | |
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/comm.html |
557 | 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 | 11 |
12 config COMM | |
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 | 15 help |
16 usage: comm [-123] FILE1 FILE2 | |
17 | |
18 Reads FILE1 and FILE2, which should be ordered, and produces three text | |
19 columns as output: lines only in FILE1; lines only in FILE2; and lines | |
20 in both files. Filename "-" is a synonym for stdin. | |
21 | |
22 -1 suppress the output column of lines unique to FILE1 | |
23 -2 suppress the output column of lines unique to FILE2 | |
24 -3 suppress the output column of lines duplicated in FILE1 and FILE2 | |
25 */ | |
26 | |
27 #include "toys.h" | |
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 | 32 |
33 static void writeline(const char *line, int col) | |
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 | 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 | 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 | 43 } |
44 puts(line); | |
45 } | |
46 | |
47 void comm_main(void) | |
48 { | |
49 int file[2]; | |
50 char *line[2]; | |
51 int i; | |
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 | 54 |
55 for (i = 0; i < 2; i++) { | |
56 file[i] = strcmp("-", toys.optargs[i]) ? xopen(toys.optargs[i], O_RDONLY) : 0; | |
57 line[i] = get_line(file[i]); | |
58 } | |
59 | |
60 while (line[0] && line[1]) { | |
61 int order = strcmp(line[0], line[1]); | |
62 | |
63 if (order == 0) { | |
64 writeline(line[0], 2); | |
65 for (i = 0; i < 2; i++) { | |
66 free(line[i]); | |
67 line[i] = get_line(file[i]); | |
68 } | |
69 } else { | |
70 i = order < 0 ? 0 : 1; | |
71 writeline(line[i], i); | |
72 free(line[i]); | |
73 line[i] = get_line(file[i]); | |
74 } | |
75 } | |
76 | |
77 /* print rest of the longer file */ | |
78 for (i = line[0] ? 0 : 1; line[i];) { | |
79 writeline(line[i], i); | |
80 free(line[i]); | |
81 line[i] = get_line(file[i]); | |
82 } | |
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 | 85 } |