blob: c59fccdd1d637bd13967262876fa711ecc20f7a4 [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 Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "comm (4.2 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: comm is used to compare two files line by line and return
14//config: a three-column output.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010015
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
Denys Vlasenko22542ec2017-08-08 21:55:02 +020065 getopt32(argv, "^" "123" "\0" "=2");
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000066 argv += optind;
Mike Frysinger0d605e92005-05-12 22:25:41 +000067
Mike Frysinger867a6082005-05-13 00:57:30 +000068 for (i = 0; i < 2; ++i) {
Denis Vlasenkobb130792008-07-15 06:45:14 +000069 stream[i] = xfopen_stdin(argv[i]);
Rob Landley2acfd7b2005-05-11 23:12:49 +000070 }
71
Denis Vlasenkobb130792008-07-15 06:45:14 +000072 order = 0;
73 thisline[1] = thisline[0] = NULL;
74 while (1) {
75 if (order <= 0) {
76 free(thisline[0]);
77 thisline[0] = xmalloc_fgetline(stream[0]);
78 }
79 if (order >= 0) {
80 free(thisline[1]);
81 thisline[1] = xmalloc_fgetline(stream[1]);
Rob Landley2acfd7b2005-05-11 23:12:49 +000082 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000083
Denis Vlasenkobb130792008-07-15 06:45:14 +000084 i = !thisline[0] + (!thisline[1] << 1);
85 if (i)
Rob Landley2acfd7b2005-05-11 23:12:49 +000086 break;
Denis Vlasenkobb130792008-07-15 06:45:14 +000087 order = strcmp(thisline[0], thisline[1]);
Mike Frysinger0d605e92005-05-12 22:25:41 +000088
Denis Vlasenkobb130792008-07-15 06:45:14 +000089 if (order >= 0)
90 writeline(thisline[1], order ? 1 : 2);
91 else
92 writeline(thisline[0], 0);
93 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000094
Denis Vlasenkobb130792008-07-15 06:45:14 +000095 /* EOF at least on one of the streams */
96 i &= 1;
97 if (thisline[i]) {
98 /* stream[i] is not at EOF yet */
99 /* we did not print thisline[i] yet */
100 char *p = thisline[i];
101 writeline(p, i);
102 while (1) {
103 free(p);
104 p = xmalloc_fgetline(stream[i]);
105 if (!p)
106 break;
107 writeline(p, i);
Rob Landley2acfd7b2005-05-11 23:12:49 +0000108 }
109 }
110
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +0000111 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkobb130792008-07-15 06:45:14 +0000112 fclose(stream[0]);
113 fclose(stream[1]);
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +0000114 }
Rob Landley2acfd7b2005-05-11 23:12:49 +0000115
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +0000116 return EXIT_SUCCESS;
Rob Landley2acfd7b2005-05-11 23:12:49 +0000117}