1 /* comm.c - select or reject lines common to two files 2 * 3 * Copyright 2012 Ilya Kuzmich <ikv (at) safe-mail.net> 4 * 5 * See http://opengroup.org/onlinepubs/9699919799/utilities/comm.html 6 7 // <# and ># take single digit, so 321 define flags 8 USE_COMM(NEWTOY(comm, "<2>2321", TOYFLAG_USR|TOYFLAG_BIN)) 9 10 config COMM 11 bool "comm" 12 default y 13 help 14 usage: comm [-123] FILE1 FILE2 15 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. 19 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 23 */ 24 25 #define FOR_comm 26 #include "toys.h" 27 28 static void writeline(const char *line, int col) 29 { 30 if (col == 0 && toys.optflags & FLAG_1) return; 31 else if (col == 1) { 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'); 38 } 39 puts(line); 40 } 41 42 void comm_main(void) 43 { 44 int file[2]; 45 char *line[2]; 46 int i; 47 48 if (toys.optflags == 7) return; 49 50 for (i = 0; i < 2; i++) { 51 file[i] = strcmp("-", toys.optargs[i]) 52 ? xopen(toys.optargs[i], O_RDONLY) : 0; 53 line[i] = get_line(file[i]); 54 } 55 56 while (line[0] && line[1]) { 57 int order = strcmp(line[0], line[1]); 58 59 if (order == 0) { 60 writeline(line[0], 2); 61 for (i = 0; i < 2; i++) { 62 free(line[i]); 63 line[i] = get_line(file[i]); 64 } 65 } else { 66 i = order < 0 ? 0 : 1; 67 writeline(line[i], i); 68 free(line[i]); 69 line[i] = get_line(file[i]); 70 } 71 } 72 73 /* print rest of the longer file */ 74 for (i = line[0] ? 0 : 1; line[i];) { 75 writeline(line[i], i); 76 free(line[i]); 77 line[i] = get_line(file[i]); 78 } 79 80 if (CFG_TOYBOX_FREE) for (i = 0; i < 2; i++) xclose(file[i]); 81 } 82