Annotation of /trunk/mkinitrd-magellan/busybox/coreutils/comm.c
Parent Directory
|
Revision Log
Revision 816 -
(hide annotations)
(download)
Fri Apr 24 18:33:46 2009 UTC (15 years, 2 months ago) by niro
File MIME type: text/plain
File size: 2007 byte(s)
Fri Apr 24 18:33:46 2009 UTC (15 years, 2 months ago) by niro
File MIME type: text/plain
File size: 2007 byte(s)
-updated to busybox-1.13.4
1 | niro | 532 | /* 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 tarball for details. | ||
8 | */ | ||
9 | |||
10 | niro | 816 | #include "libbb.h" |
11 | niro | 532 | |
12 | niro | 816 | #define COMM_OPT_1 (1 << 0) |
13 | #define COMM_OPT_2 (1 << 1) | ||
14 | #define COMM_OPT_3 (1 << 2) | ||
15 | niro | 532 | |
16 | /* writeline outputs the input given, appropriately aligned according to class */ | ||
17 | static void writeline(char *line, int class) | ||
18 | { | ||
19 | niro | 816 | int flags = option_mask32; |
20 | niro | 532 | if (class == 0) { |
21 | niro | 816 | if (flags & COMM_OPT_1) |
22 | niro | 532 | return; |
23 | } else if (class == 1) { | ||
24 | niro | 816 | if (flags & COMM_OPT_2) |
25 | niro | 532 | return; |
26 | niro | 816 | if (!(flags & COMM_OPT_1)) |
27 | niro | 532 | putchar('\t'); |
28 | niro | 816 | } else /*if (class == 2)*/ { |
29 | if (flags & COMM_OPT_3) | ||
30 | niro | 532 | return; |
31 | niro | 816 | if (!(flags & COMM_OPT_1)) |
32 | niro | 532 | putchar('\t'); |
33 | niro | 816 | if (!(flags & COMM_OPT_2)) |
34 | niro | 532 | putchar('\t'); |
35 | } | ||
36 | niro | 816 | puts(line); |
37 | niro | 532 | } |
38 | |||
39 | niro | 816 | int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
40 | int comm_main(int argc UNUSED_PARAM, char **argv) | ||
41 | niro | 532 | { |
42 | niro | 816 | char *thisline[2]; |
43 | FILE *stream[2]; | ||
44 | niro | 532 | int i; |
45 | niro | 816 | int order; |
46 | niro | 532 | |
47 | niro | 816 | opt_complementary = "=2"; |
48 | getopt32(argv, "123"); | ||
49 | argv += optind; | ||
50 | |||
51 | niro | 532 | for (i = 0; i < 2; ++i) { |
52 | niro | 816 | stream[i] = xfopen_stdin(argv[i]); |
53 | niro | 532 | } |
54 | |||
55 | niro | 816 | 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 | niro | 532 | } |
62 | niro | 816 | if (order >= 0) { |
63 | free(thisline[1]); | ||
64 | thisline[1] = xmalloc_fgetline(stream[1]); | ||
65 | } | ||
66 | niro | 532 | |
67 | niro | 816 | i = !thisline[0] + (!thisline[1] << 1); |
68 | if (i) | ||
69 | niro | 532 | break; |
70 | niro | 816 | order = strcmp(thisline[0], thisline[1]); |
71 | niro | 532 | |
72 | niro | 816 | if (order >= 0) |
73 | writeline(thisline[1], order ? 1 : 2); | ||
74 | else | ||
75 | writeline(thisline[0], 0); | ||
76 | } | ||
77 | niro | 532 | |
78 | niro | 816 | /* 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); | ||
91 | niro | 532 | } |
92 | } | ||
93 | |||
94 | niro | 816 | if (ENABLE_FEATURE_CLEAN_UP) { |
95 | fclose(stream[0]); | ||
96 | fclose(stream[1]); | ||
97 | } | ||
98 | niro | 532 | |
99 | niro | 816 | return EXIT_SUCCESS; |
100 | niro | 532 | } |