blob: cd450950b5772a4c62768398086e71d09ecc32ed [file] [log] [blame]
Rob Landley2acfd7b2005-05-11 23:12:49 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini comm implementation for busybox
4 *
5 * Copyright (C) 2005 by Robert Sullivan <cogito.ergo.cogito@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Rob Landley2acfd7b2005-05-11 23:12:49 +00008 */
9
Pere Orga34425382011-03-31 14:43:25 +020010//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"
Pere Orga34425382011-03-31 14:43:25 +020014//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"
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Rob Landley2acfd7b2005-05-11 23:12:49 +000019
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000020#define COMM_OPT_1 (1 << 0)
21#define COMM_OPT_2 (1 << 1)
22#define COMM_OPT_3 (1 << 2)
Rob Landley2acfd7b2005-05-11 23:12:49 +000023
24/* writeline outputs the input given, appropriately aligned according to class */
Denis Vlasenkobb130792008-07-15 06:45:14 +000025static void writeline(char *line, int class)
Mike Frysinger0d605e92005-05-12 22:25:41 +000026{
Denis Vlasenkobb130792008-07-15 06:45:14 +000027 int flags = option_mask32;
Mike Frysinger867a6082005-05-13 00:57:30 +000028 if (class == 0) {
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000029 if (flags & COMM_OPT_1)
Mike Frysinger0fb397e2005-05-13 00:58:18 +000030 return;
Mike Frysinger867a6082005-05-13 00:57:30 +000031 } else if (class == 1) {
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000032 if (flags & COMM_OPT_2)
Mike Frysingerdad4cf72005-05-12 22:50:12 +000033 return;
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000034 if (!(flags & COMM_OPT_1))
Mike Frysingerdad4cf72005-05-12 22:50:12 +000035 putchar('\t');
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000036 } else /*if (class == 2)*/ {
37 if (flags & COMM_OPT_3)
Mike Frysingerdad4cf72005-05-12 22:50:12 +000038 return;
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000039 if (!(flags & COMM_OPT_1))
Mike Frysingerdad4cf72005-05-12 22:50:12 +000040 putchar('\t');
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000041 if (!(flags & COMM_OPT_2))
Mike Frysingerdad4cf72005-05-12 22:50:12 +000042 putchar('\t');
Rob Landley2acfd7b2005-05-11 23:12:49 +000043 }
Denis Vlasenkobb130792008-07-15 06:45:14 +000044 puts(line);
Rob Landley2acfd7b2005-05-11 23:12:49 +000045}
46
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000047int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000048int comm_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger0d605e92005-05-12 22:25:41 +000049{
Denis Vlasenkobb130792008-07-15 06:45:14 +000050 char *thisline[2];
51 FILE *stream[2];
Mike Frysinger867a6082005-05-13 00:57:30 +000052 int i;
Denis Vlasenkobb130792008-07-15 06:45:14 +000053 int order;
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000054
55 opt_complementary = "=2";
Denis Vlasenkobb130792008-07-15 06:45:14 +000056 getopt32(argv, "123");
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000057 argv += optind;
Mike Frysinger0d605e92005-05-12 22:25:41 +000058
Mike Frysinger867a6082005-05-13 00:57:30 +000059 for (i = 0; i < 2; ++i) {
Denis Vlasenkobb130792008-07-15 06:45:14 +000060 stream[i] = xfopen_stdin(argv[i]);
Rob Landley2acfd7b2005-05-11 23:12:49 +000061 }
62
Denis Vlasenkobb130792008-07-15 06:45:14 +000063 order = 0;
64 thisline[1] = thisline[0] = NULL;
65 while (1) {
66 if (order <= 0) {
67 free(thisline[0]);
68 thisline[0] = xmalloc_fgetline(stream[0]);
69 }
70 if (order >= 0) {
71 free(thisline[1]);
72 thisline[1] = xmalloc_fgetline(stream[1]);
Rob Landley2acfd7b2005-05-11 23:12:49 +000073 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000074
Denis Vlasenkobb130792008-07-15 06:45:14 +000075 i = !thisline[0] + (!thisline[1] << 1);
76 if (i)
Rob Landley2acfd7b2005-05-11 23:12:49 +000077 break;
Denis Vlasenkobb130792008-07-15 06:45:14 +000078 order = strcmp(thisline[0], thisline[1]);
Mike Frysinger0d605e92005-05-12 22:25:41 +000079
Denis Vlasenkobb130792008-07-15 06:45:14 +000080 if (order >= 0)
81 writeline(thisline[1], order ? 1 : 2);
82 else
83 writeline(thisline[0], 0);
84 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000085
Denis Vlasenkobb130792008-07-15 06:45:14 +000086 /* EOF at least on one of the streams */
87 i &= 1;
88 if (thisline[i]) {
89 /* stream[i] is not at EOF yet */
90 /* we did not print thisline[i] yet */
91 char *p = thisline[i];
92 writeline(p, i);
93 while (1) {
94 free(p);
95 p = xmalloc_fgetline(stream[i]);
96 if (!p)
97 break;
98 writeline(p, i);
Rob Landley2acfd7b2005-05-11 23:12:49 +000099 }
100 }
101
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +0000102 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkobb130792008-07-15 06:45:14 +0000103 fclose(stream[0]);
104 fclose(stream[1]);
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +0000105 }
Rob Landley2acfd7b2005-05-11 23:12:49 +0000106
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +0000107 return EXIT_SUCCESS;
Rob Landley2acfd7b2005-05-11 23:12:49 +0000108}