blob: 8f9dcde294e814e7fd1acd5b316ac223d0f3a98a [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 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config COMM
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "comm (3.9 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
13//config: comm is used to compare two files line by line and return
14//config: a three-column output.
15
16//applet:IF_COMM(APPLET(comm, BB_DIR_USR_BIN, BB_SUID_DROP))
17
18//kbuild:lib-$(CONFIG_COMM) += comm.o
Rob Landley2acfd7b2005-05-11 23:12:49 +000019
Pere Orga34425382011-03-31 14:43:25 +020020//usage:#define comm_trivial_usage
21//usage: "[-123] FILE1 FILE2"
22//usage:#define comm_full_usage "\n\n"
23//usage: "Compare FILE1 with FILE2\n"
Pere Orga34425382011-03-31 14:43:25 +020024//usage: "\n -1 Suppress lines unique to FILE1"
25//usage: "\n -2 Suppress lines unique to FILE2"
26//usage: "\n -3 Suppress lines common to both files"
27
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Rob Landley2acfd7b2005-05-11 23:12:49 +000029
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000030#define COMM_OPT_1 (1 << 0)
31#define COMM_OPT_2 (1 << 1)
32#define COMM_OPT_3 (1 << 2)
Rob Landley2acfd7b2005-05-11 23:12:49 +000033
34/* writeline outputs the input given, appropriately aligned according to class */
Denis Vlasenkobb130792008-07-15 06:45:14 +000035static void writeline(char *line, int class)
Mike Frysinger0d605e92005-05-12 22:25:41 +000036{
Denis Vlasenkobb130792008-07-15 06:45:14 +000037 int flags = option_mask32;
Mike Frysinger867a6082005-05-13 00:57:30 +000038 if (class == 0) {
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000039 if (flags & COMM_OPT_1)
Mike Frysinger0fb397e2005-05-13 00:58:18 +000040 return;
Mike Frysinger867a6082005-05-13 00:57:30 +000041 } else if (class == 1) {
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000042 if (flags & COMM_OPT_2)
Mike Frysingerdad4cf72005-05-12 22:50:12 +000043 return;
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000044 if (!(flags & COMM_OPT_1))
Mike Frysingerdad4cf72005-05-12 22:50:12 +000045 putchar('\t');
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000046 } else /*if (class == 2)*/ {
47 if (flags & COMM_OPT_3)
Mike Frysingerdad4cf72005-05-12 22:50:12 +000048 return;
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000049 if (!(flags & COMM_OPT_1))
Mike Frysingerdad4cf72005-05-12 22:50:12 +000050 putchar('\t');
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000051 if (!(flags & COMM_OPT_2))
Mike Frysingerdad4cf72005-05-12 22:50:12 +000052 putchar('\t');
Rob Landley2acfd7b2005-05-11 23:12:49 +000053 }
Denis Vlasenkobb130792008-07-15 06:45:14 +000054 puts(line);
Rob Landley2acfd7b2005-05-11 23:12:49 +000055}
56
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000057int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000058int comm_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger0d605e92005-05-12 22:25:41 +000059{
Denis Vlasenkobb130792008-07-15 06:45:14 +000060 char *thisline[2];
61 FILE *stream[2];
Mike Frysinger867a6082005-05-13 00:57:30 +000062 int i;
Denis Vlasenkobb130792008-07-15 06:45:14 +000063 int order;
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000064
65 opt_complementary = "=2";
Denis Vlasenkobb130792008-07-15 06:45:14 +000066 getopt32(argv, "123");
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000067 argv += optind;
Mike Frysinger0d605e92005-05-12 22:25:41 +000068
Mike Frysinger867a6082005-05-13 00:57:30 +000069 for (i = 0; i < 2; ++i) {
Denis Vlasenkobb130792008-07-15 06:45:14 +000070 stream[i] = xfopen_stdin(argv[i]);
Rob Landley2acfd7b2005-05-11 23:12:49 +000071 }
72
Denis Vlasenkobb130792008-07-15 06:45:14 +000073 order = 0;
74 thisline[1] = thisline[0] = NULL;
75 while (1) {
76 if (order <= 0) {
77 free(thisline[0]);
78 thisline[0] = xmalloc_fgetline(stream[0]);
79 }
80 if (order >= 0) {
81 free(thisline[1]);
82 thisline[1] = xmalloc_fgetline(stream[1]);
Rob Landley2acfd7b2005-05-11 23:12:49 +000083 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000084
Denis Vlasenkobb130792008-07-15 06:45:14 +000085 i = !thisline[0] + (!thisline[1] << 1);
86 if (i)
Rob Landley2acfd7b2005-05-11 23:12:49 +000087 break;
Denis Vlasenkobb130792008-07-15 06:45:14 +000088 order = strcmp(thisline[0], thisline[1]);
Mike Frysinger0d605e92005-05-12 22:25:41 +000089
Denis Vlasenkobb130792008-07-15 06:45:14 +000090 if (order >= 0)
91 writeline(thisline[1], order ? 1 : 2);
92 else
93 writeline(thisline[0], 0);
94 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000095
Denis Vlasenkobb130792008-07-15 06:45:14 +000096 /* EOF at least on one of the streams */
97 i &= 1;
98 if (thisline[i]) {
99 /* stream[i] is not at EOF yet */
100 /* we did not print thisline[i] yet */
101 char *p = thisline[i];
102 writeline(p, i);
103 while (1) {
104 free(p);
105 p = xmalloc_fgetline(stream[i]);
106 if (!p)
107 break;
108 writeline(p, i);
Rob Landley2acfd7b2005-05-11 23:12:49 +0000109 }
110 }
111
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +0000112 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkobb130792008-07-15 06:45:14 +0000113 fclose(stream[0]);
114 fclose(stream[1]);
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +0000115 }
Rob Landley2acfd7b2005-05-11 23:12:49 +0000116
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +0000117 return EXIT_SUCCESS;
Rob Landley2acfd7b2005-05-11 23:12:49 +0000118}