1 /* vi: set sw=4 ts=4: */
3 * Mini comm implementation for busybox
5 * Copyright (C) 2005 by Robert Sullivan <cogito.ergo.cogito@gmail.com>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //usage:#define comm_trivial_usage
11 //usage: "[-123] FILE1 FILE2"
12 //usage:#define comm_full_usage "\n\n"
13 //usage: "Compare FILE1 with FILE2\n"
15 //usage: "\n -1 Suppress lines unique to FILE1"
16 //usage: "\n -2 Suppress lines unique to FILE2"
17 //usage: "\n -3 Suppress lines common to both files"
21 #define COMM_OPT_1 (1 << 0)
22 #define COMM_OPT_2 (1 << 1)
23 #define COMM_OPT_3 (1 << 2)
25 /* writeline outputs the input given, appropriately aligned according to class */
26 static void writeline(char *line, int class)
28 int flags = option_mask32;
30 if (flags & COMM_OPT_1)
32 } else if (class == 1) {
33 if (flags & COMM_OPT_2)
35 if (!(flags & COMM_OPT_1))
37 } else /*if (class == 2)*/ {
38 if (flags & COMM_OPT_3)
40 if (!(flags & COMM_OPT_1))
42 if (!(flags & COMM_OPT_2))
48 int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49 int comm_main(int argc UNUSED_PARAM, char **argv)
56 opt_complementary = "=2";
57 getopt32(argv, "123");
60 for (i = 0; i < 2; ++i) {
61 stream[i] = xfopen_stdin(argv[i]);
65 thisline[1] = thisline[0] = NULL;
69 thisline[0] = xmalloc_fgetline(stream[0]);
73 thisline[1] = xmalloc_fgetline(stream[1]);
76 i = !thisline[0] + (!thisline[1] << 1);
79 order = strcmp(thisline[0], thisline[1]);
82 writeline(thisline[1], order ? 1 : 2);
84 writeline(thisline[0], 0);
87 /* EOF at least on one of the streams */
90 /* stream[i] is not at EOF yet */
91 /* we did not print thisline[i] yet */
92 char *p = thisline[i];
96 p = xmalloc_fgetline(stream[i]);
103 if (ENABLE_FEATURE_CLEAN_UP) {