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"
14 //usage: "\n -1 Suppress lines unique to FILE1"
15 //usage: "\n -2 Suppress lines unique to FILE2"
16 //usage: "\n -3 Suppress lines common to both files"
20 #define COMM_OPT_1 (1 << 0)
21 #define COMM_OPT_2 (1 << 1)
22 #define COMM_OPT_3 (1 << 2)
24 /* writeline outputs the input given, appropriately aligned according to class */
25 static void writeline(char *line
, int class)
27 int flags
= option_mask32
;
29 if (flags
& COMM_OPT_1
)
31 } else if (class == 1) {
32 if (flags
& COMM_OPT_2
)
34 if (!(flags
& COMM_OPT_1
))
36 } else /*if (class == 2)*/ {
37 if (flags
& COMM_OPT_3
)
39 if (!(flags
& COMM_OPT_1
))
41 if (!(flags
& COMM_OPT_2
))
47 int comm_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
48 int comm_main(int argc UNUSED_PARAM
, char **argv
)
55 opt_complementary
= "=2";
56 getopt32(argv
, "123");
59 for (i
= 0; i
< 2; ++i
) {
60 stream
[i
] = xfopen_stdin(argv
[i
]);
64 thisline
[1] = thisline
[0] = NULL
;
68 thisline
[0] = xmalloc_fgetline(stream
[0]);
72 thisline
[1] = xmalloc_fgetline(stream
[1]);
75 i
= !thisline
[0] + (!thisline
[1] << 1);
78 order
= strcmp(thisline
[0], thisline
[1]);
81 writeline(thisline
[1], order
? 1 : 2);
83 writeline(thisline
[0], 0);
86 /* EOF at least on one of the streams */
89 /* stream[i] is not at EOF yet */
90 /* we did not print thisline[i] yet */
91 char *p
= thisline
[i
];
95 p
= xmalloc_fgetline(stream
[i
]);
102 if (ENABLE_FEATURE_CLEAN_UP
) {