ls: reorder and rename functions. No code changes
[platform/upstream/busybox.git] / coreutils / comm.c
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  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
10 //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"
14 //usage:     "\nOptions:"
15 //usage:     "\n        -1      Suppress lines unique to FILE1"
16 //usage:     "\n        -2      Suppress lines unique to FILE2"
17 //usage:     "\n        -3      Suppress lines common to both files"
18
19 #include "libbb.h"
20
21 #define COMM_OPT_1 (1 << 0)
22 #define COMM_OPT_2 (1 << 1)
23 #define COMM_OPT_3 (1 << 2)
24
25 /* writeline outputs the input given, appropriately aligned according to class */
26 static void writeline(char *line, int class)
27 {
28         int flags = option_mask32;
29         if (class == 0) {
30                 if (flags & COMM_OPT_1)
31                         return;
32         } else if (class == 1) {
33                 if (flags & COMM_OPT_2)
34                         return;
35                 if (!(flags & COMM_OPT_1))
36                         putchar('\t');
37         } else /*if (class == 2)*/ {
38                 if (flags & COMM_OPT_3)
39                         return;
40                 if (!(flags & COMM_OPT_1))
41                         putchar('\t');
42                 if (!(flags & COMM_OPT_2))
43                         putchar('\t');
44         }
45         puts(line);
46 }
47
48 int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49 int comm_main(int argc UNUSED_PARAM, char **argv)
50 {
51         char *thisline[2];
52         FILE *stream[2];
53         int i;
54         int order;
55
56         opt_complementary = "=2";
57         getopt32(argv, "123");
58         argv += optind;
59
60         for (i = 0; i < 2; ++i) {
61                 stream[i] = xfopen_stdin(argv[i]);
62         }
63
64         order = 0;
65         thisline[1] = thisline[0] = NULL;
66         while (1) {
67                 if (order <= 0) {
68                         free(thisline[0]);
69                         thisline[0] = xmalloc_fgetline(stream[0]);
70                 }
71                 if (order >= 0) {
72                         free(thisline[1]);
73                         thisline[1] = xmalloc_fgetline(stream[1]);
74                 }
75
76                 i = !thisline[0] + (!thisline[1] << 1);
77                 if (i)
78                         break;
79                 order = strcmp(thisline[0], thisline[1]);
80
81                 if (order >= 0)
82                         writeline(thisline[1], order ? 1 : 2);
83                 else
84                         writeline(thisline[0], 0);
85         }
86
87         /* EOF at least on one of the streams */
88         i &= 1;
89         if (thisline[i]) {
90                 /* stream[i] is not at EOF yet */
91                 /* we did not print thisline[i] yet */
92                 char *p = thisline[i];
93                 writeline(p, i);
94                 while (1) {
95                         free(p);
96                         p = xmalloc_fgetline(stream[i]);
97                         if (!p)
98                                 break;
99                         writeline(p, i);
100                 }
101         }
102
103         if (ENABLE_FEATURE_CLEAN_UP) {
104                 fclose(stream[0]);
105                 fclose(stream[1]);
106         }
107
108         return EXIT_SUCCESS;
109 }