xref: /netbsd-src/usr.bin/comm/comm.c (revision 809c33aadd4d94bd1b06b18dd5833f589564329b)
1*809c33aaSsimonb /*	$NetBSD: comm.c,v 1.20 2012/09/05 04:01:23 simonb Exp $	*/
2244eb8b5Sglass 
361f28255Scgd /*
4244eb8b5Sglass  * Copyright (c) 1989, 1993, 1994
5244eb8b5Sglass  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Case Larsen.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
1889aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
357202b758Slukem #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
3798e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
3898e5374cSlukem  The Regents of the University of California.  All rights reserved.");
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #ifndef lint
42244eb8b5Sglass #if 0
433e4b73c5Sjtc static char sccsid[] = "@(#)comm.c	8.4 (Berkeley) 5/4/95";
44244eb8b5Sglass #endif
45*809c33aaSsimonb __RCSID("$NetBSD: comm.c,v 1.20 2012/09/05 04:01:23 simonb Exp $");
4661f28255Scgd #endif /* not lint */
4761f28255Scgd 
48e5a8e93bSglass #include <err.h>
49e5a8e93bSglass #include <limits.h>
50e5a8e93bSglass #include <locale.h>
5161f28255Scgd #include <stdio.h>
52f83d35d0Sjtc #include <stdlib.h>
53f83d35d0Sjtc #include <string.h>
542ddbb97fSjtc #include <unistd.h>
5561f28255Scgd 
56244eb8b5Sglass #define	MAXLINELEN	(LINE_MAX + 1)
5761f28255Scgd 
581f81257aSjoerg static const char *tabs[] = { "", "\t", "\t\t" };
5961f28255Scgd 
601f81257aSjoerg static FILE   *file(const char *);
611f81257aSjoerg static void	show(FILE *, const char *, char *);
621f81257aSjoerg __dead static void	usage(void);
631f81257aSjoerg static char   *getnextln(char *buf, FILE *);
64f83d35d0Sjtc 
65f83d35d0Sjtc int
main(int argc,char ** argv)66ee89c9ffSxtraeme main(int argc, char **argv)
6761f28255Scgd {
68244eb8b5Sglass 	int comp, file1done, file2done, read1, read2;
6961f28255Scgd 	int ch, flag1, flag2, flag3;
70f83d35d0Sjtc 	FILE *fp1, *fp2;
7135ccf17cSlukem 	const char *col1, *col2, *col3, **p;
7235ccf17cSlukem 	char line1[MAXLINELEN], line2[MAXLINELEN];
7363d86770Shubertf 	int (*compare)(const char*,const char*);
74f83d35d0Sjtc 
7563d86770Shubertf 	(void)setlocale(LC_ALL, "");
7661f28255Scgd 
777202b758Slukem 	file1done = file2done = 0;
7861f28255Scgd 	flag1 = flag2 = flag3 = 1;
7963d86770Shubertf 	compare = strcoll;
8063d86770Shubertf 	while ((ch = getopt(argc, argv, "123f")) != -1)
8161f28255Scgd 		switch(ch) {
8261f28255Scgd 		case '1':
8361f28255Scgd 			flag1 = 0;
8461f28255Scgd 			break;
8561f28255Scgd 		case '2':
8661f28255Scgd 			flag2 = 0;
8761f28255Scgd 			break;
8861f28255Scgd 		case '3':
8961f28255Scgd 			flag3 = 0;
9061f28255Scgd 			break;
9163d86770Shubertf 		case 'f':
9263d86770Shubertf 			compare = strcasecmp;
9363d86770Shubertf 			break;
9461f28255Scgd 		case '?':
9561f28255Scgd 		default:
9661f28255Scgd 			usage();
9761f28255Scgd 		}
98f83d35d0Sjtc 	argc -= optind;
9961f28255Scgd 	argv += optind;
10061f28255Scgd 
10161f28255Scgd 	if (argc != 2)
10261f28255Scgd 		usage();
10361f28255Scgd 
10461f28255Scgd 	fp1 = file(argv[0]);
10561f28255Scgd 	fp2 = file(argv[1]);
10661f28255Scgd 
10761f28255Scgd 	/* for each column printed, add another tab offset */
10861f28255Scgd 	p = tabs;
10961f28255Scgd 	col1 = col2 = col3 = NULL;
11061f28255Scgd 	if (flag1)
11161f28255Scgd 		col1 = *p++;
11261f28255Scgd 	if (flag2)
11361f28255Scgd 		col2 = *p++;
11461f28255Scgd 	if (flag3)
11561f28255Scgd 		col3 = *p;
11661f28255Scgd 
11761f28255Scgd 	for (read1 = read2 = 1;;) {
11861f28255Scgd 		/* read next line, check for EOF */
11961f28255Scgd 		if (read1)
120fc5c0ee9Sdarcy 			file1done = !getnextln(line1, fp1);
12161f28255Scgd 		if (read2)
122fc5c0ee9Sdarcy 			file2done = !getnextln(line2, fp2);
12361f28255Scgd 
12461f28255Scgd 		/* if one file done, display the rest of the other file */
12561f28255Scgd 		if (file1done) {
12661f28255Scgd 			if (!file2done && col2)
12761f28255Scgd 				show(fp2, col2, line2);
12861f28255Scgd 			break;
12961f28255Scgd 		}
13061f28255Scgd 		if (file2done) {
13161f28255Scgd 			if (!file1done && col1)
13261f28255Scgd 				show(fp1, col1, line1);
13361f28255Scgd 			break;
13461f28255Scgd 		}
13561f28255Scgd 
13661f28255Scgd 		/* lines are the same */
13763d86770Shubertf 		if (!(comp = compare(line1, line2))) {
13861f28255Scgd 			read1 = read2 = 1;
13961f28255Scgd 			if (col3)
140fc5c0ee9Sdarcy 				if (printf("%s%s\n", col3, line1) < 0)
141bb8238b9Sjtc 					break;
14261f28255Scgd 			continue;
14361f28255Scgd 		}
14461f28255Scgd 
14561f28255Scgd 		/* lines are different */
14661f28255Scgd 		if (comp < 0) {
14761f28255Scgd 			read1 = 1;
14861f28255Scgd 			read2 = 0;
14961f28255Scgd 			if (col1)
150fc5c0ee9Sdarcy 				if (printf("%s%s\n", col1, line1) < 0)
151bb8238b9Sjtc 					break;
15261f28255Scgd 		} else {
15361f28255Scgd 			read1 = 0;
15461f28255Scgd 			read2 = 1;
15561f28255Scgd 			if (col2)
156fc5c0ee9Sdarcy 				if (printf("%s%s\n", col2, line2) < 0)
157bb8238b9Sjtc 					break;
15861f28255Scgd 		}
15961f28255Scgd 	}
160bb8238b9Sjtc 
161bb8238b9Sjtc 	if (ferror (stdout) || fclose (stdout) == EOF)
162bb8238b9Sjtc 		err(1, "stdout");
163bb8238b9Sjtc 
16461f28255Scgd 	exit(0);
16561f28255Scgd }
16661f28255Scgd 
1671f81257aSjoerg static void
show(FILE * fp,const char * offset,char * buf)16835ccf17cSlukem show(FILE *fp, const char *offset, char *buf)
16961f28255Scgd {
170fc5c0ee9Sdarcy 	while (printf("%s%s\n", offset, buf) >= 0 && getnextln(buf, fp))
171bb8238b9Sjtc 		;
17261f28255Scgd }
17361f28255Scgd 
1741f81257aSjoerg static FILE *
file(const char * name)175ee89c9ffSxtraeme file(const char *name)
17661f28255Scgd {
17761f28255Scgd 	FILE *fp;
17861f28255Scgd 
17961f28255Scgd 	if (!strcmp(name, "-"))
18061f28255Scgd 		return (stdin);
181e5a8e93bSglass 	if ((fp = fopen(name, "r")) == NULL)
182e5a8e93bSglass 		err(1, "%s", name);
18361f28255Scgd 	return (fp);
18461f28255Scgd }
18561f28255Scgd 
1861f81257aSjoerg static void
usage(void)187ee89c9ffSxtraeme usage(void)
18861f28255Scgd {
189244eb8b5Sglass 
1904b3439b2Swiz 	(void)fprintf(stderr, "usage: comm [-123f] file1 file2\n");
19161f28255Scgd 	exit(1);
19261f28255Scgd }
193fc5c0ee9Sdarcy 
1941f81257aSjoerg static char *
getnextln(char * buf,FILE * fp)195fc5c0ee9Sdarcy getnextln(char *buf, FILE *fp)
196fc5c0ee9Sdarcy {
197fc5c0ee9Sdarcy 	size_t i = 0;
198fc5c0ee9Sdarcy 	int c;
199fc5c0ee9Sdarcy 
200*809c33aaSsimonb 	while ((c = getc(fp)) != '\n' && c != EOF) {
201fc5c0ee9Sdarcy 		buf[i++] = c;
202fc5c0ee9Sdarcy 
203fc5c0ee9Sdarcy 		if (i >= MAXLINELEN)
204fc5c0ee9Sdarcy 			i--; /* consumes extra characters till newline */
205fc5c0ee9Sdarcy 	}
206fc5c0ee9Sdarcy 
207fc5c0ee9Sdarcy 	if (c == EOF && !i)
208fc5c0ee9Sdarcy 		return NULL;
209fc5c0ee9Sdarcy 
210fc5c0ee9Sdarcy 	buf[i] = 0;
211fc5c0ee9Sdarcy 	return buf;
212fc5c0ee9Sdarcy }
213fc5c0ee9Sdarcy 
214