OSDN Git Service

New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandna...
[android-x86/external-toybox.git] / toys / posix / comm.c
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  *
7  * See http://opengroup.org/onlinepubs/9699919799/utilities/comm.html
8
9 // <# and ># take single digit, so 321 define flags
10 USE_COMM(NEWTOY(comm, "<2>2321", TOYFLAG_USR|TOYFLAG_BIN))
11
12 config COMM
13         bool "comm"
14         default y
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 #define FOR_comm
28 #include "toys.h"
29
30 static void writeline(const char *line, int col)
31 {
32         if (col == 0 && toys.optflags & FLAG_1) return;
33         else if (col == 1) {
34                 if (toys.optflags & FLAG_2) return;
35                 if (!(toys.optflags & FLAG_1)) putchar('\t');
36         } else if (col == 2) {
37                 if (toys.optflags & FLAG_3) return;
38                 if (!(toys.optflags & FLAG_1)) putchar('\t');
39                 if (!(toys.optflags & FLAG_2)) putchar('\t');
40         }
41         puts(line);
42 }
43
44 void comm_main(void)
45 {
46         int file[2];
47         char *line[2];
48         int i;
49
50         if (toys.optflags == 7) return;
51
52         for (i = 0; i < 2; i++) {
53                 file[i] = strcmp("-", toys.optargs[i]) ? xopen(toys.optargs[i], O_RDONLY) : 0;
54                 line[i] = get_line(file[i]);
55         }
56
57         while (line[0] && line[1]) {
58                 int order = strcmp(line[0], line[1]);
59
60                 if (order == 0) {
61                         writeline(line[0], 2);
62                         for (i = 0; i < 2; i++) {
63                                 free(line[i]);
64                                 line[i] = get_line(file[i]);
65                         }
66                 } else {
67                         i = order < 0 ? 0 : 1;
68                         writeline(line[i], i);
69                         free(line[i]);
70                         line[i] = get_line(file[i]);
71                 }
72         }
73
74         /* print rest of the longer file */
75         for (i = line[0] ? 0 : 1; line[i];) {
76                 writeline(line[i], i);
77                 free(line[i]);
78                 line[i] = get_line(file[i]);
79         }
80
81         if (CFG_TOYBOX_FREE) for (i = 0; i < 2; i--) xclose(file[i]);
82 }