xref: /csrg-svn/usr.bin/comm/comm.c (revision 42723)
138096Sbostic /*
238096Sbostic  * Copyright (c) 1989 The Regents of the University of California.
338096Sbostic  * All rights reserved.
438096Sbostic  *
538096Sbostic  * This code is derived from software contributed to Berkeley by
638096Sbostic  * Case Larsen.
738096Sbostic  *
8*42723Sbostic  * %sccs.include.redist.c%
938096Sbostic  */
1038096Sbostic 
1138096Sbostic #ifndef lint
1238096Sbostic char copyright[] =
1338096Sbostic "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
1438096Sbostic  All rights reserved.\n";
1538096Sbostic #endif /* not lint */
1638096Sbostic 
1738096Sbostic #ifndef lint
18*42723Sbostic static char sccsid[] = "@(#)comm.c	5.4 (Berkeley) 06/01/90";
1938096Sbostic #endif /* not lint */
2038096Sbostic 
2138096Sbostic #include <sys/file.h>
2241088Smarc #include <limits.h>
23988Sbill #include <stdio.h>
24988Sbill 
2541088Smarc #define	MAXLINELEN	(LINE_MAX + 1)
26988Sbill 
2741088Smarc char *tabs[] = { "", "\t", "\t\t" };
2838096Sbostic 
29988Sbill main(argc,argv)
3038096Sbostic 	int argc;
3141088Smarc 	char *argv[];
32988Sbill {
3338096Sbostic 	register int comp, file1done, file2done, read1, read2;
3438096Sbostic 	register char *col1, *col2, *col3;
3538096Sbostic 	int ch, flag1, flag2, flag3;
3641088Smarc 	FILE *fp1, *fp2, *file();
3738096Sbostic 	char **p, line1[MAXLINELEN], line2[MAXLINELEN];
3841088Smarc 	extern int optind;
39988Sbill 
4038096Sbostic 	flag1 = flag2 = flag3 = 1;
4138096Sbostic 	while ((ch = getopt(argc, argv, "-123")) != EOF)
4238096Sbostic 		switch(ch) {
4338096Sbostic 		case '-':
4438096Sbostic 			--optind;
4538096Sbostic 			goto done;
4638096Sbostic 		case '1':
4738096Sbostic 			flag1 = 0;
4838096Sbostic 			break;
4938096Sbostic 		case '2':
5038096Sbostic 			flag2 = 0;
5138096Sbostic 			break;
5238096Sbostic 		case '3':
5338096Sbostic 			flag3 = 0;
5438096Sbostic 			break;
5538096Sbostic 		case '?':
5638096Sbostic 		default:
5738096Sbostic 			usage();
58988Sbill 		}
5938096Sbostic done:	argc -= optind;
6038096Sbostic 	argv += optind;
61988Sbill 
6238096Sbostic 	if (argc != 2)
6338096Sbostic 		usage();
64988Sbill 
6538096Sbostic 	fp1 = file(argv[0]);
6638096Sbostic 	fp2 = file(argv[1]);
67988Sbill 
6838096Sbostic 	/* for each column printed, add another tab offset */
6938096Sbostic 	p = tabs;
7038096Sbostic 	if (flag1)
7138096Sbostic 		col1 = *p++;
7238096Sbostic 	if (flag2)
7338096Sbostic 		col2 = *p++;
7438096Sbostic 	if (flag3)
7538096Sbostic 		col3 = *p++;
76988Sbill 
7738096Sbostic 	for (read1 = read2 = 1;;) {
7838096Sbostic 		/* read next line, check for EOF */
7938096Sbostic 		if (read1)
8038096Sbostic 			file1done = !fgets(line1, MAXLINELEN, fp1);
8138096Sbostic 		if (read2)
8238096Sbostic 			file2done = !fgets(line2, MAXLINELEN, fp2);
83988Sbill 
8438096Sbostic 		/* if one file done, display the rest of the other file */
8538096Sbostic 		if (file1done) {
8638096Sbostic 			if (!file2done && col2)
8741087Smarc 				show(fp2, col2, line2);
8838096Sbostic 			break;
8938096Sbostic 		}
9038096Sbostic 		if (file2done) {
9138096Sbostic 			if (!file1done && col1)
9241087Smarc 				show(fp1, col1, line1);
9338096Sbostic 			break;
9438096Sbostic 		}
95988Sbill 
9638096Sbostic 		/* lines are the same */
9738096Sbostic 		if (!(comp = strcmp(line1, line2))) {
9838096Sbostic 			read1 = read2 = 1;
9938096Sbostic 			if (col3)
10038096Sbostic 				(void)printf("%s%s", col3, line1);
10138096Sbostic 			continue;
10238096Sbostic 		}
103988Sbill 
10438096Sbostic 		/* lines are different */
10538096Sbostic 		if (comp < 0) {
10638096Sbostic 			read1 = 1;
10738096Sbostic 			read2 = 0;
10838096Sbostic 			if (col1)
10938096Sbostic 				(void)printf("%s%s", col1, line1);
11038096Sbostic 		} else {
11138096Sbostic 			read1 = 0;
11238096Sbostic 			read2 = 1;
11338096Sbostic 			if (col2)
11438096Sbostic 				(void)printf("%s%s", col2, line2);
115988Sbill 		}
116988Sbill 	}
11738096Sbostic 	exit(0);
118988Sbill }
119988Sbill 
12038096Sbostic show(fp, offset, buf)
12138096Sbostic 	FILE *fp;
12238096Sbostic 	char *offset, *buf;
123988Sbill {
12438096Sbostic 	do {
12538096Sbostic 		(void)printf("%s%s", offset, buf);
12638096Sbostic 	} while (fgets(buf, MAXLINELEN, fp));
127988Sbill }
128988Sbill 
12938096Sbostic FILE *
13038096Sbostic file(name)
13138096Sbostic 	char *name;
132988Sbill {
13338096Sbostic 	FILE *fp;
134988Sbill 
13538096Sbostic 	if (!strcmp(name, "-"))
13638096Sbostic 		return(stdin);
13738096Sbostic 	if (!(fp = fopen(name, "r"))) {
13838096Sbostic 		(void)fprintf(stderr, "comm: can't read %s.\n", name);
13938096Sbostic 		exit(1);
140988Sbill 	}
14138096Sbostic 	return(fp);
142988Sbill }
143988Sbill 
14438096Sbostic usage()
145988Sbill {
14638096Sbostic 	(void)fprintf(stderr, "usage: comm [-123] [ - ] file1 file2\n");
14738096Sbostic 	exit(1);
148988Sbill }
149