[821] | 1 | /* 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 | *
|
---|
[2725] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 8 | */
|
---|
| 9 |
|
---|
[1765] | 10 | #include "libbb.h"
|
---|
[821] | 11 |
|
---|
[1765] | 12 | #define COMM_OPT_1 (1 << 0)
|
---|
| 13 | #define COMM_OPT_2 (1 << 1)
|
---|
| 14 | #define COMM_OPT_3 (1 << 2)
|
---|
[821] | 15 |
|
---|
| 16 | /* writeline outputs the input given, appropriately aligned according to class */
|
---|
[2725] | 17 | static void writeline(char *line, int class)
|
---|
[821] | 18 | {
|
---|
[2725] | 19 | int flags = option_mask32;
|
---|
[821] | 20 | if (class == 0) {
|
---|
[1765] | 21 | if (flags & COMM_OPT_1)
|
---|
[821] | 22 | return;
|
---|
| 23 | } else if (class == 1) {
|
---|
[1765] | 24 | if (flags & COMM_OPT_2)
|
---|
[821] | 25 | return;
|
---|
[1765] | 26 | if (!(flags & COMM_OPT_1))
|
---|
[821] | 27 | putchar('\t');
|
---|
[1765] | 28 | } else /*if (class == 2)*/ {
|
---|
| 29 | if (flags & COMM_OPT_3)
|
---|
[821] | 30 | return;
|
---|
[1765] | 31 | if (!(flags & COMM_OPT_1))
|
---|
[821] | 32 | putchar('\t');
|
---|
[1765] | 33 | if (!(flags & COMM_OPT_2))
|
---|
[821] | 34 | putchar('\t');
|
---|
| 35 | }
|
---|
[2725] | 36 | puts(line);
|
---|
[821] | 37 | }
|
---|
| 38 |
|
---|
[2725] | 39 | int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 40 | int comm_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 41 | {
|
---|
[2725] | 42 | char *thisline[2];
|
---|
| 43 | FILE *stream[2];
|
---|
[821] | 44 | int i;
|
---|
[2725] | 45 | int order;
|
---|
[821] | 46 |
|
---|
[1765] | 47 | opt_complementary = "=2";
|
---|
[2725] | 48 | getopt32(argv, "123");
|
---|
[1765] | 49 | argv += optind;
|
---|
| 50 |
|
---|
[821] | 51 | for (i = 0; i < 2; ++i) {
|
---|
[2725] | 52 | stream[i] = xfopen_stdin(argv[i]);
|
---|
[821] | 53 | }
|
---|
| 54 |
|
---|
[2725] | 55 | 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]);
|
---|
[821] | 61 | }
|
---|
[2725] | 62 | if (order >= 0) {
|
---|
| 63 | free(thisline[1]);
|
---|
| 64 | thisline[1] = xmalloc_fgetline(stream[1]);
|
---|
| 65 | }
|
---|
[821] | 66 |
|
---|
[2725] | 67 | i = !thisline[0] + (!thisline[1] << 1);
|
---|
| 68 | if (i)
|
---|
[821] | 69 | break;
|
---|
[2725] | 70 | order = strcmp(thisline[0], thisline[1]);
|
---|
[821] | 71 |
|
---|
[2725] | 72 | if (order >= 0)
|
---|
| 73 | writeline(thisline[1], order ? 1 : 2);
|
---|
| 74 | else
|
---|
| 75 | writeline(thisline[0], 0);
|
---|
| 76 | }
|
---|
[821] | 77 |
|
---|
[2725] | 78 | /* 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);
|
---|
[821] | 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[1765] | 94 | if (ENABLE_FEATURE_CLEAN_UP) {
|
---|
[2725] | 95 | fclose(stream[0]);
|
---|
| 96 | fclose(stream[1]);
|
---|
[1765] | 97 | }
|
---|
[821] | 98 |
|
---|
[1765] | 99 | return EXIT_SUCCESS;
|
---|
[821] | 100 | }
|
---|