comm.c revision 1.20
1/*	$NetBSD: comm.c,v 1.20 2012/09/05 04:01:23 simonb Exp $	*/
2
3/*
4 * Copyright (c) 1989, 1993, 1994
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Case Larsen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36#ifndef lint
37__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
38 The Regents of the University of California.  All rights reserved.");
39#endif /* not lint */
40
41#ifndef lint
42#if 0
43static char sccsid[] = "@(#)comm.c	8.4 (Berkeley) 5/4/95";
44#endif
45__RCSID("$NetBSD: comm.c,v 1.20 2012/09/05 04:01:23 simonb Exp $");
46#endif /* not lint */
47
48#include <err.h>
49#include <limits.h>
50#include <locale.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <unistd.h>
55
56#define	MAXLINELEN	(LINE_MAX + 1)
57
58static const char *tabs[] = { "", "\t", "\t\t" };
59
60static FILE   *file(const char *);
61static void	show(FILE *, const char *, char *);
62__dead static void	usage(void);
63static char   *getnextln(char *buf, FILE *);
64
65int
66main(int argc, char **argv)
67{
68	int comp, file1done, file2done, read1, read2;
69	int ch, flag1, flag2, flag3;
70	FILE *fp1, *fp2;
71	const char *col1, *col2, *col3, **p;
72	char line1[MAXLINELEN], line2[MAXLINELEN];
73	int (*compare)(const char*,const char*);
74
75	(void)setlocale(LC_ALL, "");
76
77	file1done = file2done = 0;
78	flag1 = flag2 = flag3 = 1;
79	compare = strcoll;
80	while ((ch = getopt(argc, argv, "123f")) != -1)
81		switch(ch) {
82		case '1':
83			flag1 = 0;
84			break;
85		case '2':
86			flag2 = 0;
87			break;
88		case '3':
89			flag3 = 0;
90			break;
91		case 'f':
92			compare = strcasecmp;
93			break;
94		case '?':
95		default:
96			usage();
97		}
98	argc -= optind;
99	argv += optind;
100
101	if (argc != 2)
102		usage();
103
104	fp1 = file(argv[0]);
105	fp2 = file(argv[1]);
106
107	/* for each column printed, add another tab offset */
108	p = tabs;
109	col1 = col2 = col3 = NULL;
110	if (flag1)
111		col1 = *p++;
112	if (flag2)
113		col2 = *p++;
114	if (flag3)
115		col3 = *p;
116
117	for (read1 = read2 = 1;;) {
118		/* read next line, check for EOF */
119		if (read1)
120			file1done = !getnextln(line1, fp1);
121		if (read2)
122			file2done = !getnextln(line2, fp2);
123
124		/* if one file done, display the rest of the other file */
125		if (file1done) {
126			if (!file2done && col2)
127				show(fp2, col2, line2);
128			break;
129		}
130		if (file2done) {
131			if (!file1done && col1)
132				show(fp1, col1, line1);
133			break;
134		}
135
136		/* lines are the same */
137		if (!(comp = compare(line1, line2))) {
138			read1 = read2 = 1;
139			if (col3)
140				if (printf("%s%s\n", col3, line1) < 0)
141					break;
142			continue;
143		}
144
145		/* lines are different */
146		if (comp < 0) {
147			read1 = 1;
148			read2 = 0;
149			if (col1)
150				if (printf("%s%s\n", col1, line1) < 0)
151					break;
152		} else {
153			read1 = 0;
154			read2 = 1;
155			if (col2)
156				if (printf("%s%s\n", col2, line2) < 0)
157					break;
158		}
159	}
160
161	if (ferror (stdout) || fclose (stdout) == EOF)
162		err(1, "stdout");
163
164	exit(0);
165}
166
167static void
168show(FILE *fp, const char *offset, char *buf)
169{
170	while (printf("%s%s\n", offset, buf) >= 0 && getnextln(buf, fp))
171		;
172}
173
174static FILE *
175file(const char *name)
176{
177	FILE *fp;
178
179	if (!strcmp(name, "-"))
180		return (stdin);
181	if ((fp = fopen(name, "r")) == NULL)
182		err(1, "%s", name);
183	return (fp);
184}
185
186static void
187usage(void)
188{
189
190	(void)fprintf(stderr, "usage: comm [-123f] file1 file2\n");
191	exit(1);
192}
193
194static char *
195getnextln(char *buf, FILE *fp)
196{
197	size_t i = 0;
198	int c;
199
200	while ((c = getc(fp)) != '\n' && c != EOF) {
201		buf[i++] = c;
202
203		if (i >= MAXLINELEN)
204			i--; /* consumes extra characters till newline */
205	}
206
207	if (c == EOF && !i)
208		return NULL;
209
210	buf[i] = 0;
211	return buf;
212}
213
214