blob: 6b7b9554c4b8ac5ef4353e7018a6fb6eb8d687d0 [file] [log] [blame]
Rob Landley2acfd7b2005-05-11 23:12:49 +00001/* 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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 *
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include "busybox.h"
29
30#define COMM_OPT_1 0x01
31#define COMM_OPT_2 0x02
32#define COMM_OPT_3 0x04
33
34/* These three variables control behaviour if non-zero */
35
36static int only_file_1;
37static int only_file_2;
38static int both;
39
40/* writeline outputs the input given, appropriately aligned according to class */
Mike Frysinger0d605e92005-05-12 22:25:41 +000041static void writeline (char *line, int class)
42{
Rob Landley2acfd7b2005-05-11 23:12:49 +000043 switch (class) {
Mike Frysinger0d605e92005-05-12 22:25:41 +000044 case 1:
45 if (!only_file_1)
Rob Landley2acfd7b2005-05-11 23:12:49 +000046 return;
47 break;
Mike Frysinger0d605e92005-05-12 22:25:41 +000048 case 2:
49 if (!only_file_2)
Rob Landley2acfd7b2005-05-11 23:12:49 +000050 return;
51 if (only_file_1)
52 putchar('\t');
53 break;
Mike Frysinger0d605e92005-05-12 22:25:41 +000054 case 3:
55 if (!both)
Rob Landley2acfd7b2005-05-11 23:12:49 +000056 return;
57 if (only_file_1)
58 putchar('\t');
59 if (only_file_2)
60 putchar('\t');
61 break;
62 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000063 fputs(line, stdout);
Rob Landley2acfd7b2005-05-11 23:12:49 +000064}
65
66/* This is the real core of the program - lines are compared here */
Mike Frysinger0d605e92005-05-12 22:25:41 +000067static int cmp_files(char **infiles)
68{
Rob Landley2acfd7b2005-05-11 23:12:49 +000069 char thisline[2][100];
70 FILE *streams[2];
71 int i = 0;
Mike Frysinger0d605e92005-05-12 22:25:41 +000072
Rob Landley2acfd7b2005-05-11 23:12:49 +000073 for (i = 0; i < 2; i++) {
74 streams[i] = (strcmp(infiles[i], "=") == 0 ? stdin : fopen(infiles[i], "r"));
75 fgets(thisline[i], 100, streams[i]);
76 }
77
78 while (thisline[0] || thisline[1]) {
Rob Landley2acfd7b2005-05-11 23:12:49 +000079 int order = 0;
80 int tl0_len = strlen(thisline[0]);
81 int tl1_len = strlen(thisline[1]);
Mike Frysinger0d605e92005-05-12 22:25:41 +000082
Rob Landley2acfd7b2005-05-11 23:12:49 +000083 if (!thisline[0])
84 order = 1;
Mike Frysinger0d605e92005-05-12 22:25:41 +000085 else if (!thisline[1])
Rob Landley2acfd7b2005-05-11 23:12:49 +000086 order = -1;
87 else {
88 order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len);
89 if (!order)
90 order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len;
91 }
Mike Frysinger0d605e92005-05-12 22:25:41 +000092
Rob Landley2acfd7b2005-05-11 23:12:49 +000093 if ((order == 0) && (!feof(streams[0])) && (!feof(streams[1])))
94 writeline(thisline[1], 3);
95 else if ((order > 0) && (!feof(streams[1])))
96 writeline(thisline[1], 2);
97 else if ((order < 0) && (!feof(streams[0])))
98 writeline(thisline[0], 1);
Mike Frysinger0d605e92005-05-12 22:25:41 +000099
Rob Landley2acfd7b2005-05-11 23:12:49 +0000100 if (feof(streams[0]) && feof(streams[1])) {
101 fclose(streams[0]);
102 fclose(streams[1]);
103 break;
Mike Frysinger0d605e92005-05-12 22:25:41 +0000104
105 } else if (feof(streams[0])) {
Rob Landley2acfd7b2005-05-11 23:12:49 +0000106 while (!feof(streams[1])) {
107 if (order < 0)
108 writeline(thisline[1], 2);
109 fgets(thisline[1], 100, streams[1]);
110 }
111 fclose(streams[0]);
112 fclose(streams[1]);
113 break;
Rob Landley2acfd7b2005-05-11 23:12:49 +0000114
Mike Frysinger0d605e92005-05-12 22:25:41 +0000115 } else if (feof(streams[1])) {
Rob Landley2acfd7b2005-05-11 23:12:49 +0000116 while (!feof(streams[0])) {
117 if (order > 0)
118 writeline(thisline[0], 1);
119 fgets(thisline[0], 100, streams[0]);
120 }
121 fclose(streams[0]);
122 fclose(streams[1]);
123 break;
Mike Frysinger0d605e92005-05-12 22:25:41 +0000124
125 } else {
Rob Landley2acfd7b2005-05-11 23:12:49 +0000126 if (order >= 0)
127 fgets(thisline[1], 100, streams[1]);
128 if (order <= 0)
129 fgets(thisline[0], 100, streams[0]);
130 }
131 }
132
133 return 0;
134}
135
Mike Frysinger0d605e92005-05-12 22:25:41 +0000136int comm_main (int argc, char **argv)
137{
Rob Landley2acfd7b2005-05-11 23:12:49 +0000138 unsigned long opt;
139 only_file_1 = 1;
140 only_file_2 = 1;
141 both = 1;
Mike Frysinger0d605e92005-05-12 22:25:41 +0000142
Rob Landley2acfd7b2005-05-11 23:12:49 +0000143 opt = bb_getopt_ulflags(argc, argv, "123");
Mike Frysinger0d605e92005-05-12 22:25:41 +0000144
145 if ((opt & 0x80000000UL) || (optind == argc))
146 bb_show_usage();
147
Rob Landley2acfd7b2005-05-11 23:12:49 +0000148 if (opt & COMM_OPT_1)
149 only_file_1 = 0;
150 if (opt & COMM_OPT_2)
151 only_file_2 = 0;
152 if (opt & COMM_OPT_3)
153 both = 0;
154
155 exit(cmp_files(argv + optind) == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
156}