blob: 221cbfbcc844320258aeac7f5b149b9931e3d581 [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 *
Rob Landleye104d5f2006-04-04 19:26:37 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Rob Landley2acfd7b2005-05-11 23:12:49 +00008 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Rob Landley2acfd7b2005-05-11 23:12:49 +000011
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000012#define COMM_OPT_1 (1 << 0)
13#define COMM_OPT_2 (1 << 1)
14#define COMM_OPT_3 (1 << 2)
Rob Landley2acfd7b2005-05-11 23:12:49 +000015
16/* writeline outputs the input given, appropriately aligned according to class */
Denis Vlasenkobb130792008-07-15 06:45:14 +000017static void writeline(char *line, int class)
Mike Frysinger0d605e92005-05-12 22:25:41 +000018{
Denis Vlasenkobb130792008-07-15 06:45:14 +000019 int flags = option_mask32;
Mike Frysinger867a6082005-05-13 00:57:30 +000020 if (class == 0) {
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000021 if (flags & COMM_OPT_1)
Mike Frysinger0fb397e2005-05-13 00:58:18 +000022 return;
Mike Frysinger867a6082005-05-13 00:57:30 +000023 } else if (class == 1) {
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000024 if (flags & COMM_OPT_2)
Mike Frysingerdad4cf72005-05-12 22:50:12 +000025 return;
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000026 if (!(flags & COMM_OPT_1))
Mike Frysingerdad4cf72005-05-12 22:50:12 +000027 putchar('\t');
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000028 } else /*if (class == 2)*/ {
29 if (flags & COMM_OPT_3)
Mike Frysingerdad4cf72005-05-12 22:50:12 +000030 return;
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000031 if (!(flags & COMM_OPT_1))
Mike Frysingerdad4cf72005-05-12 22:50:12 +000032 putchar('\t');
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000033 if (!(flags & COMM_OPT_2))
Mike Frysingerdad4cf72005-05-12 22:50:12 +000034 putchar('\t');
Rob Landley2acfd7b2005-05-11 23:12:49 +000035 }
Denis Vlasenkobb130792008-07-15 06:45:14 +000036 puts(line);
Rob Landley2acfd7b2005-05-11 23:12:49 +000037}
38
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000039int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000040int comm_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger0d605e92005-05-12 22:25:41 +000041{
Denis Vlasenkobb130792008-07-15 06:45:14 +000042 char *thisline[2];
43 FILE *stream[2];
Mike Frysinger867a6082005-05-13 00:57:30 +000044 int i;
Denis Vlasenkobb130792008-07-15 06:45:14 +000045 int order;
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000046
47 opt_complementary = "=2";
Denis Vlasenkobb130792008-07-15 06:45:14 +000048 getopt32(argv, "123");
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000049 argv += optind;
Mike Frysinger0d605e92005-05-12 22:25:41 +000050
Mike Frysinger867a6082005-05-13 00:57:30 +000051 for (i = 0; i < 2; ++i) {
Denis Vlasenkobb130792008-07-15 06:45:14 +000052 stream[i] = xfopen_stdin(argv[i]);
Rob Landley2acfd7b2005-05-11 23:12:49 +000053 }
54
Denis Vlasenkobb130792008-07-15 06:45:14 +000055 order = 0;
56 thisline[1] = thisline[0] = NULL;
57 while (1) {
58 if (order <= 0) {
59 free(thisline[0]);
60 thisline[0] = xmalloc_fgetline(stream[0]);
61 }
62 if (order >= 0) {
63 free(thisline[1]);
64 thisline[1] = xmalloc_fgetline(stream[1]);
Rob Landley2acfd7b2005-05-11 23:12:49 +000065 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000066
Denis Vlasenkobb130792008-07-15 06:45:14 +000067 i = !thisline[0] + (!thisline[1] << 1);
68 if (i)
Rob Landley2acfd7b2005-05-11 23:12:49 +000069 break;
Denis Vlasenkobb130792008-07-15 06:45:14 +000070 order = strcmp(thisline[0], thisline[1]);
Mike Frysinger0d605e92005-05-12 22:25:41 +000071
Denis Vlasenkobb130792008-07-15 06:45:14 +000072 if (order >= 0)
73 writeline(thisline[1], order ? 1 : 2);
74 else
75 writeline(thisline[0], 0);
76 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000077
Denis Vlasenkobb130792008-07-15 06:45:14 +000078 /* EOF at least on one of the streams */
79 i &= 1;
80 if (thisline[i]) {
81 /* stream[i] is not at EOF yet */
82 /* we did not print thisline[i] yet */
83 char *p = thisline[i];
84 writeline(p, i);
85 while (1) {
86 free(p);
87 p = xmalloc_fgetline(stream[i]);
88 if (!p)
89 break;
90 writeline(p, i);
Rob Landley2acfd7b2005-05-11 23:12:49 +000091 }
92 }
93
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000094 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkobb130792008-07-15 06:45:14 +000095 fclose(stream[0]);
96 fclose(stream[1]);
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000097 }
Rob Landley2acfd7b2005-05-11 23:12:49 +000098
Denis Vlasenkobb5b01c2007-04-07 00:45:27 +000099 return EXIT_SUCCESS;
Rob Landley2acfd7b2005-05-11 23:12:49 +0000100}