comm.c revision 204928
159342Sobrien/*
259342Sobrien * Copyright (c) 1989, 1993, 1994
318962Ssos *	The Regents of the University of California.  All rights reserved.
418962Ssos *
518962Ssos * This code is derived from software contributed to Berkeley by
6 * Case Larsen.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static const char copyright[] =
39"@(#) Copyright (c) 1989, 1993, 1994\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif
42
43#if 0
44#ifndef lint
45static char sccsid[] = "From: @(#)comm.c	8.4 (Berkeley) 5/4/95";
46#endif
47#endif
48
49#include <sys/cdefs.h>
50__FBSDID("$FreeBSD: head/usr.bin/comm/comm.c 204928 2010-03-09 21:06:17Z ache $");
51
52#include <err.h>
53#include <limits.h>
54#include <locale.h>
55#include <stdint.h>
56#define _WITH_GETLINE
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61#include <wchar.h>
62#include <wctype.h>
63
64int iflag;
65const char *tabs[] = { "", "\t", "\t\t" };
66
67FILE   *file(const char *);
68wchar_t	*convert(const char *);
69void	show(FILE *, const char *, const char *, char **, size_t *);
70static void	usage(void);
71
72int
73main(int argc, char *argv[])
74{
75	int comp, read1, read2;
76	int ch, flag1, flag2, flag3;
77	FILE *fp1, *fp2;
78	const char *col1, *col2, *col3;
79	size_t line1len, line2len;
80	char *line1, *line2;
81	ssize_t n1, n2;
82	wchar_t *tline1, *tline2;
83	const char **p;
84
85	(void) setlocale(LC_ALL, "");
86
87	flag1 = flag2 = flag3 = 1;
88
89	while ((ch = getopt(argc, argv, "123i")) != -1)
90		switch(ch) {
91		case '1':
92			flag1 = 0;
93			break;
94		case '2':
95			flag2 = 0;
96			break;
97		case '3':
98			flag3 = 0;
99			break;
100		case 'i':
101			iflag = 1;
102			break;
103		case '?':
104		default:
105			usage();
106		}
107	argc -= optind;
108	argv += optind;
109
110	if (argc != 2)
111		usage();
112
113	fp1 = file(argv[0]);
114	fp2 = file(argv[1]);
115
116	/* for each column printed, add another tab offset */
117	p = tabs;
118	col1 = col2 = col3 = NULL;
119	if (flag1)
120		col1 = *p++;
121	if (flag2)
122		col2 = *p++;
123	if (flag3)
124		col3 = *p;
125
126	line1len = line2len = 0;
127	line1 = line2 = NULL;
128	n1 = n2 = -1;
129
130	for (read1 = read2 = 1;;) {
131		/* read next line, check for EOF */
132		if (read1) {
133			n1 = getline(&line1, &line1len, fp1);
134			if (n1 < 0 && ferror(fp1))
135				err(1, "%s", argv[0]);
136			if (n1 > 0 && line1[n1 - 1] == '\n')
137				line1[n1 - 1] = '\0';
138
139		}
140		if (read2) {
141			n2 = getline(&line2, &line2len, fp2);
142			if (n2 < 0 && ferror(fp2))
143				err(1, "%s", argv[1]);
144			if (n2 > 0 && line2[n2 - 1] == '\n')
145				line2[n2 - 1] = '\0';
146		}
147
148		/* if one file done, display the rest of the other file */
149		if (n1 < 0) {
150			if (n2 >= 0 && col2 != NULL)
151				show(fp2, argv[1], col2, &line2, &line2len);
152			break;
153		}
154		if (n2 < 0) {
155			if (n1 >= 0 && col1 != NULL)
156				show(fp1, argv[0], col1, &line1, &line1len);
157			break;
158		}
159
160		tline2 = NULL;
161		if ((tline1 = convert(line1)) != NULL)
162			tline2 = convert(line2);
163		if (tline1 == NULL || tline2 == NULL)
164			comp = strcmp(line1, line2);
165		else
166			comp = wcscoll(tline1, tline2);
167		if (tline1 != NULL)
168			free(tline1);
169		if (tline2 != NULL)
170			free(tline2);
171
172		/* lines are the same */
173		if (!comp) {
174			read1 = read2 = 1;
175			if (col3 != NULL)
176				(void)printf("%s%s\n", col3, line1);
177			continue;
178		}
179
180		/* lines are different */
181		if (comp < 0) {
182			read1 = 1;
183			read2 = 0;
184			if (col1 != NULL)
185				(void)printf("%s%s\n", col1, line1);
186		} else {
187			read1 = 0;
188			read2 = 1;
189			if (col2 != NULL)
190				(void)printf("%s%s\n", col2, line2);
191		}
192	}
193	exit(0);
194}
195
196wchar_t *
197convert(const char *str)
198{
199	size_t n;
200	wchar_t *buf, *p;
201
202	if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
203		return (NULL);
204	if (SIZE_MAX / sizeof(*buf) < n + 1)
205		errx(1, "conversion buffer length overflow");
206	if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
207		err(1, "malloc");
208	if (mbstowcs(buf, str, n + 1) != n)
209		errx(1, "internal mbstowcs() error");
210
211	if (iflag) {
212		for (p = buf; *p != L'\0'; p++)
213			*p = towlower(*p);
214	}
215
216	return (buf);
217}
218
219void
220show(FILE *fp, const char *fn, const char *offset, char **bufp, size_t *buflenp)
221{
222	ssize_t n;
223
224	do {
225		(void)printf("%s%s\n", offset, *bufp);
226		if ((n = getline(bufp, buflenp, fp)) < 0)
227			break;
228		if (n > 0 && (*bufp)[n - 1] == '\n')
229			(*bufp)[n - 1] = '\0';
230	} while (1);
231	if (ferror(fp))
232		err(1, "%s", fn);
233}
234
235FILE *
236file(const char *name)
237{
238	FILE *fp;
239
240	if (!strcmp(name, "-"))
241		return (stdin);
242	if ((fp = fopen(name, "r")) == NULL) {
243		err(1, "%s", name);
244	}
245	return (fp);
246}
247
248static void
249usage(void)
250{
251	(void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
252	exit(1);
253}
254