1 /* comm.c - select or reject lines common to two files
3 * Copyright 2012 Ilya Kuzmich <ikv@safe-mail.net>
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/comm.html
7 // <# and ># take single digit, so 321 define flags
8 USE_COMM(NEWTOY(comm, "<2>2321", TOYFLAG_USR|TOYFLAG_BIN))
14 usage: comm [-123] FILE1 FILE2
16 Reads FILE1 and FILE2, which should be ordered, and produces three text
17 columns as output: lines only in FILE1; lines only in FILE2; and lines
18 in both files. Filename "-" is a synonym for stdin.
20 -1 suppress the output column of lines unique to FILE1
21 -2 suppress the output column of lines unique to FILE2
22 -3 suppress the output column of lines duplicated in FILE1 and FILE2
28 static void writeline(const char *line, int col)
30 if (col == 0 && toys.optflags & FLAG_1) return;
32 if (toys.optflags & FLAG_2) return;
33 if (!(toys.optflags & FLAG_1)) putchar('\t');
34 } else if (col == 2) {
35 if (toys.optflags & FLAG_3) return;
36 if (!(toys.optflags & FLAG_1)) putchar('\t');
37 if (!(toys.optflags & FLAG_2)) putchar('\t');
48 if (toys.optflags == 7) return;
50 for (i = 0; i < 2; i++) {
51 file[i] = xopenro(toys.optargs[i]);
52 line[i] = get_line(file[i]);
55 while (line[0] && line[1]) {
56 int order = strcmp(line[0], line[1]);
59 writeline(line[0], 2);
60 for (i = 0; i < 2; i++) {
62 line[i] = get_line(file[i]);
65 i = order < 0 ? 0 : 1;
66 writeline(line[i], i);
68 line[i] = get_line(file[i]);
72 /* print rest of the longer file */
73 for (i = line[0] ? 0 : 1; line[i];) {
74 writeline(line[i], i);
76 line[i] = get_line(file[i]);
79 if (CFG_TOYBOX_FREE) for (i = 0; i < 2; i++) xclose(file[i]);