Contents of /trunk/mkinitrd-magellan/busybox/coreutils/comm.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2722 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2722 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
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 tarball for details. |
8 | */ |
9 | |
10 | #include "busybox.h" |
11 | |
12 | #define COMM_OPT_1 0x01 |
13 | #define COMM_OPT_2 0x02 |
14 | #define COMM_OPT_3 0x04 |
15 | |
16 | /* These three variables control behaviour if non-zero */ |
17 | |
18 | static int only_file_1; |
19 | static int only_file_2; |
20 | static int both; |
21 | |
22 | /* writeline outputs the input given, appropriately aligned according to class */ |
23 | static void writeline(char *line, int class) |
24 | { |
25 | if (class == 0) { |
26 | if (!only_file_1) |
27 | return; |
28 | } else if (class == 1) { |
29 | if (!only_file_2) |
30 | return; |
31 | if (only_file_1) |
32 | putchar('\t'); |
33 | } |
34 | else /*if (class == 2)*/ { |
35 | if (!both) |
36 | return; |
37 | if (only_file_1) |
38 | putchar('\t'); |
39 | if (only_file_2) |
40 | putchar('\t'); |
41 | } |
42 | fputs(line, stdout); |
43 | } |
44 | |
45 | /* This is the real core of the program - lines are compared here */ |
46 | static void cmp_files(char **infiles) |
47 | { |
48 | #define LINE_LEN 100 |
49 | #define BB_EOF_0 0x1 |
50 | #define BB_EOF_1 0x2 |
51 | char thisline[2][LINE_LEN]; |
52 | FILE *streams[2]; |
53 | int i; |
54 | |
55 | for (i = 0; i < 2; ++i) { |
56 | streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : xfopen(infiles[i], "r")); |
57 | fgets(thisline[i], LINE_LEN, streams[i]); |
58 | } |
59 | |
60 | while (*thisline[0] || *thisline[1]) { |
61 | int order = 0; |
62 | |
63 | i = 0; |
64 | if (feof(streams[0])) i |= BB_EOF_0; |
65 | if (feof(streams[1])) i |= BB_EOF_1; |
66 | |
67 | if (!*thisline[0]) |
68 | order = 1; |
69 | else if (!*thisline[1]) |
70 | order = -1; |
71 | else { |
72 | int tl0_len, tl1_len; |
73 | tl0_len = strlen(thisline[0]); |
74 | tl1_len = strlen(thisline[1]); |
75 | order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len); |
76 | if (!order) |
77 | order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len; |
78 | } |
79 | |
80 | if (order == 0 && !i) |
81 | writeline(thisline[1], 2); |
82 | else if (order > 0 && !(i & BB_EOF_1)) |
83 | writeline(thisline[1], 1); |
84 | else if (order < 0 && !(i & BB_EOF_0)) |
85 | writeline(thisline[0], 0); |
86 | |
87 | if (i & BB_EOF_0 & BB_EOF_1) { |
88 | break; |
89 | |
90 | } else if (i) { |
91 | i = (i & BB_EOF_0 ? 1 : 0); |
92 | while (!feof(streams[i])) { |
93 | if ((order < 0 && i) || (order > 0 && !i)) |
94 | writeline(thisline[i], i); |
95 | fgets(thisline[i], LINE_LEN, streams[i]); |
96 | } |
97 | break; |
98 | |
99 | } else { |
100 | if (order >= 0) |
101 | fgets(thisline[1], LINE_LEN, streams[1]); |
102 | if (order <= 0) |
103 | fgets(thisline[0], LINE_LEN, streams[0]); |
104 | } |
105 | } |
106 | |
107 | fclose(streams[0]); |
108 | fclose(streams[1]); |
109 | } |
110 | |
111 | int comm_main(int argc, char **argv) |
112 | { |
113 | unsigned long flags; |
114 | |
115 | flags = getopt32(argc, argv, "123"); |
116 | |
117 | if (optind + 2 != argc) |
118 | bb_show_usage(); |
119 | |
120 | only_file_1 = !(flags & COMM_OPT_1); |
121 | only_file_2 = !(flags & COMM_OPT_2); |
122 | both = !(flags & COMM_OPT_3); |
123 | |
124 | cmp_files(argv + optind); |
125 | exit(EXIT_SUCCESS); |
126 | } |