source: MondoRescue/branches/stable/mindi-busybox/coreutils/comm.c@ 1247

Last change on this file since 1247 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 2.7 KB
RevLine 
[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 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
14#include "busybox.h"
15
16#define COMM_OPT_1 0x01
17#define COMM_OPT_2 0x02
18#define COMM_OPT_3 0x04
19
20/* These three variables control behaviour if non-zero */
21
22static int only_file_1;
23static int only_file_2;
24static int both;
25
26/* writeline outputs the input given, appropriately aligned according to class */
27static void writeline(char *line, int class)
28{
29 if (class == 0) {
30 if (!only_file_1)
31 return;
32 } else if (class == 1) {
33 if (!only_file_2)
34 return;
35 if (only_file_1)
36 putchar('\t');
37 }
38 else /*if (class == 2)*/ {
39 if (!both)
40 return;
41 if (only_file_1)
42 putchar('\t');
43 if (only_file_2)
44 putchar('\t');
45 }
46 fputs(line, stdout);
47}
48
49/* This is the real core of the program - lines are compared here */
50static void cmp_files(char **infiles)
51{
52#define LINE_LEN 100
53#define BB_EOF_0 0x1
54#define BB_EOF_1 0x2
55 char thisline[2][LINE_LEN];
56 FILE *streams[2];
57 int i;
58
59 for (i = 0; i < 2; ++i) {
60 streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : bb_xfopen(infiles[i], "r"));
61 fgets(thisline[i], LINE_LEN, streams[i]);
62 }
63
64 while (*thisline[0] || *thisline[1]) {
65 int order = 0;
66
67 i = 0;
68 if (feof(streams[0])) i |= BB_EOF_0;
69 if (feof(streams[1])) i |= BB_EOF_1;
70
71 if (!*thisline[0])
72 order = 1;
73 else if (!*thisline[1])
74 order = -1;
75 else {
76 int tl0_len, tl1_len;
77 tl0_len = strlen(thisline[0]);
78 tl1_len = strlen(thisline[1]);
79 order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len);
80 if (!order)
81 order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len;
82 }
83
84 if (order == 0 && !i)
85 writeline(thisline[1], 2);
86 else if (order > 0 && !(i & BB_EOF_1))
87 writeline(thisline[1], 1);
88 else if (order < 0 && !(i & BB_EOF_0))
89 writeline(thisline[0], 0);
90
91 if (i & BB_EOF_0 & BB_EOF_1) {
92 break;
93
94 } else if (i) {
95 i = (i & BB_EOF_0 ? 1 : 0);
96 while (!feof(streams[i])) {
97 if ((order < 0 && i) || (order > 0 && !i))
98 writeline(thisline[i], i);
99 fgets(thisline[i], LINE_LEN, streams[i]);
100 }
101 break;
102
103 } else {
104 if (order >= 0)
105 fgets(thisline[1], LINE_LEN, streams[1]);
106 if (order <= 0)
107 fgets(thisline[0], LINE_LEN, streams[0]);
108 }
109 }
110
111 fclose(streams[0]);
112 fclose(streams[1]);
113}
114
115int comm_main(int argc, char **argv)
116{
117 unsigned long flags;
118
119 flags = bb_getopt_ulflags(argc, argv, "123");
120
121 if (optind + 2 != argc)
122 bb_show_usage();
123
124 only_file_1 = !(flags & COMM_OPT_1);
125 only_file_2 = !(flags & COMM_OPT_2);
126 both = !(flags & COMM_OPT_3);
127
128 cmp_files(argv + optind);
129 exit(EXIT_SUCCESS);
130}
Note: See TracBrowser for help on using the repository browser.