xref: /csrg-svn/usr.bin/comm/comm.c (revision 69231)
138096Sbostic /*
266655Spendry  * Copyright (c) 1989, 1993, 1994
361952Sbostic  *	The Regents of the University of California.  All rights reserved.
438096Sbostic  *
538096Sbostic  * This code is derived from software contributed to Berkeley by
638096Sbostic  * Case Larsen.
738096Sbostic  *
842723Sbostic  * %sccs.include.redist.c%
938096Sbostic  */
1038096Sbostic 
1138096Sbostic #ifndef lint
1261952Sbostic static char copyright[] =
1366655Spendry "@(#) Copyright (c) 1989, 1993, 1994\n\
1461952Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1538096Sbostic #endif /* not lint */
1638096Sbostic 
1738096Sbostic #ifndef lint
18*69231Sbostic static char sccsid[] = "@(#)comm.c	8.4 (Berkeley) 05/04/95";
1938096Sbostic #endif /* not lint */
2038096Sbostic 
2150559Sbostic #include <fcntl.h>
2241088Smarc #include <limits.h>
2350559Sbostic #include <errno.h>
24988Sbill #include <stdio.h>
2550559Sbostic #include <stdlib.h>
2650559Sbostic #include <string.h>
27*69231Sbostic #include <unistd.h>
28988Sbill 
2966593Spendry #define	MAXLINELEN	(LINE_MAX + 1)
30988Sbill 
3141088Smarc char *tabs[] = { "", "\t", "\t\t" };
3238096Sbostic 
3366593Spendry FILE   *file __P((char *));
3466593Spendry void	show __P((FILE *, char *, char *));
3566593Spendry void	usage __P((void));
3666593Spendry 
3766593Spendry int
main(argc,argv)3866593Spendry main(argc, argv)
3938096Sbostic 	int argc;
4041088Smarc 	char *argv[];
41988Sbill {
4266593Spendry 	int comp, file1done, file2done, read1, read2;
4338096Sbostic 	int ch, flag1, flag2, flag3;
4466593Spendry 	FILE *fp1, *fp2;
4566593Spendry 	char *col1, *col2, *col3;
4638096Sbostic 	char **p, line1[MAXLINELEN], line2[MAXLINELEN];
47988Sbill 
4838096Sbostic 	flag1 = flag2 = flag3 = 1;
4938096Sbostic 	while ((ch = getopt(argc, argv, "-123")) != EOF)
5038096Sbostic 		switch(ch) {
5138096Sbostic 		case '-':
5238096Sbostic 			--optind;
5338096Sbostic 			goto done;
5438096Sbostic 		case '1':
5538096Sbostic 			flag1 = 0;
5638096Sbostic 			break;
5738096Sbostic 		case '2':
5838096Sbostic 			flag2 = 0;
5938096Sbostic 			break;
6038096Sbostic 		case '3':
6138096Sbostic 			flag3 = 0;
6238096Sbostic 			break;
6338096Sbostic 		case '?':
6438096Sbostic 		default:
6538096Sbostic 			usage();
66988Sbill 		}
6738096Sbostic done:	argc -= optind;
6838096Sbostic 	argv += optind;
69988Sbill 
7038096Sbostic 	if (argc != 2)
7138096Sbostic 		usage();
72988Sbill 
7338096Sbostic 	fp1 = file(argv[0]);
7438096Sbostic 	fp2 = file(argv[1]);
75988Sbill 
7638096Sbostic 	/* for each column printed, add another tab offset */
7738096Sbostic 	p = tabs;
7845457Sbostic 	col1 = col2 = col3 = NULL;
7938096Sbostic 	if (flag1)
8038096Sbostic 		col1 = *p++;
8138096Sbostic 	if (flag2)
8238096Sbostic 		col2 = *p++;
8338096Sbostic 	if (flag3)
8445457Sbostic 		col3 = *p;
85988Sbill 
8638096Sbostic 	for (read1 = read2 = 1;;) {
8738096Sbostic 		/* read next line, check for EOF */
8838096Sbostic 		if (read1)
8938096Sbostic 			file1done = !fgets(line1, MAXLINELEN, fp1);
9038096Sbostic 		if (read2)
9138096Sbostic 			file2done = !fgets(line2, MAXLINELEN, fp2);
92988Sbill 
9338096Sbostic 		/* if one file done, display the rest of the other file */
9438096Sbostic 		if (file1done) {
9538096Sbostic 			if (!file2done && col2)
9641087Smarc 				show(fp2, col2, line2);
9738096Sbostic 			break;
9838096Sbostic 		}
9938096Sbostic 		if (file2done) {
10038096Sbostic 			if (!file1done && col1)
10141087Smarc 				show(fp1, col1, line1);
10238096Sbostic 			break;
10338096Sbostic 		}
104988Sbill 
10538096Sbostic 		/* lines are the same */
10638096Sbostic 		if (!(comp = strcmp(line1, line2))) {
10738096Sbostic 			read1 = read2 = 1;
10838096Sbostic 			if (col3)
10938096Sbostic 				(void)printf("%s%s", col3, line1);
11038096Sbostic 			continue;
11138096Sbostic 		}
112988Sbill 
11338096Sbostic 		/* lines are different */
11438096Sbostic 		if (comp < 0) {
11538096Sbostic 			read1 = 1;
11638096Sbostic 			read2 = 0;
11738096Sbostic 			if (col1)
11838096Sbostic 				(void)printf("%s%s", col1, line1);
11938096Sbostic 		} else {
12038096Sbostic 			read1 = 0;
12138096Sbostic 			read2 = 1;
12238096Sbostic 			if (col2)
12338096Sbostic 				(void)printf("%s%s", col2, line2);
124988Sbill 		}
125988Sbill 	}
12638096Sbostic 	exit(0);
127988Sbill }
128988Sbill 
12966593Spendry void
show(fp,offset,buf)13038096Sbostic show(fp, offset, buf)
13138096Sbostic 	FILE *fp;
13238096Sbostic 	char *offset, *buf;
133988Sbill {
13466593Spendry 
13538096Sbostic 	do {
13638096Sbostic 		(void)printf("%s%s", offset, buf);
13738096Sbostic 	} while (fgets(buf, MAXLINELEN, fp));
138988Sbill }
139988Sbill 
14038096Sbostic FILE *
file(name)14138096Sbostic file(name)
14238096Sbostic 	char *name;
143988Sbill {
14438096Sbostic 	FILE *fp;
145988Sbill 
14638096Sbostic 	if (!strcmp(name, "-"))
14766593Spendry 		return (stdin);
14850559Sbostic 	if ((fp = fopen(name, "r")) == NULL) {
14950559Sbostic 		(void)fprintf(stderr, "comm: %s: %s\n", name, strerror(errno));
15038096Sbostic 		exit(1);
151988Sbill 	}
15266593Spendry 	return (fp);
153988Sbill }
154988Sbill 
15566593Spendry void
usage()15638096Sbostic usage()
157988Sbill {
15866593Spendry 
15950559Sbostic 	(void)fprintf(stderr, "usage: comm [-123] file1 file2\n");
16038096Sbostic 	exit(1);
161988Sbill }
162