comm.c revision 1.1
11.1Scgd/*
21.1Scgd * Copyright (c) 1989 The Regents of the University of California.
31.1Scgd * All rights reserved.
41.1Scgd *
51.1Scgd * This code is derived from software contributed to Berkeley by
61.1Scgd * Case Larsen.
71.1Scgd *
81.1Scgd * Redistribution and use in source and binary forms, with or without
91.1Scgd * modification, are permitted provided that the following conditions
101.1Scgd * are met:
111.1Scgd * 1. Redistributions of source code must retain the above copyright
121.1Scgd *    notice, this list of conditions and the following disclaimer.
131.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
141.1Scgd *    notice, this list of conditions and the following disclaimer in the
151.1Scgd *    documentation and/or other materials provided with the distribution.
161.1Scgd * 3. All advertising materials mentioning features or use of this software
171.1Scgd *    must display the following acknowledgement:
181.1Scgd *	This product includes software developed by the University of
191.1Scgd *	California, Berkeley and its contributors.
201.1Scgd * 4. Neither the name of the University nor the names of its contributors
211.1Scgd *    may be used to endorse or promote products derived from this software
221.1Scgd *    without specific prior written permission.
231.1Scgd *
241.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341.1Scgd * SUCH DAMAGE.
351.1Scgd */
361.1Scgd
371.1Scgd#ifndef lint
381.1Scgdchar copyright[] =
391.1Scgd"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
401.1Scgd All rights reserved.\n";
411.1Scgd#endif /* not lint */
421.1Scgd
431.1Scgd#ifndef lint
441.1Scgdstatic char sccsid[] = "@(#)comm.c	5.7 (Berkeley) 11/1/90";
451.1Scgd#endif /* not lint */
461.1Scgd
471.1Scgd#include <sys/file.h>
481.1Scgd#include <limits.h>
491.1Scgd#include <stdio.h>
501.1Scgd
511.1Scgd#define	MAXLINELEN	(_POSIX2_LINE_MAX + 1)
521.1Scgd
531.1Scgdchar *tabs[] = { "", "\t", "\t\t" };
541.1Scgd
551.1Scgdmain(argc,argv)
561.1Scgd	int argc;
571.1Scgd	char *argv[];
581.1Scgd{
591.1Scgd	register int comp, file1done, file2done, read1, read2;
601.1Scgd	register char *col1, *col2, *col3;
611.1Scgd	int ch, flag1, flag2, flag3;
621.1Scgd	FILE *fp1, *fp2, *file();
631.1Scgd	char **p, line1[MAXLINELEN], line2[MAXLINELEN];
641.1Scgd	extern int optind;
651.1Scgd
661.1Scgd	flag1 = flag2 = flag3 = 1;
671.1Scgd	while ((ch = getopt(argc, argv, "-123")) != EOF)
681.1Scgd		switch(ch) {
691.1Scgd		case '-':
701.1Scgd			--optind;
711.1Scgd			goto done;
721.1Scgd		case '1':
731.1Scgd			flag1 = 0;
741.1Scgd			break;
751.1Scgd		case '2':
761.1Scgd			flag2 = 0;
771.1Scgd			break;
781.1Scgd		case '3':
791.1Scgd			flag3 = 0;
801.1Scgd			break;
811.1Scgd		case '?':
821.1Scgd		default:
831.1Scgd			usage();
841.1Scgd		}
851.1Scgddone:	argc -= optind;
861.1Scgd	argv += optind;
871.1Scgd
881.1Scgd	if (argc != 2)
891.1Scgd		usage();
901.1Scgd
911.1Scgd	fp1 = file(argv[0]);
921.1Scgd	fp2 = file(argv[1]);
931.1Scgd
941.1Scgd	/* for each column printed, add another tab offset */
951.1Scgd	p = tabs;
961.1Scgd	col1 = col2 = col3 = NULL;
971.1Scgd	if (flag1)
981.1Scgd		col1 = *p++;
991.1Scgd	if (flag2)
1001.1Scgd		col2 = *p++;
1011.1Scgd	if (flag3)
1021.1Scgd		col3 = *p;
1031.1Scgd
1041.1Scgd	for (read1 = read2 = 1;;) {
1051.1Scgd		/* read next line, check for EOF */
1061.1Scgd		if (read1)
1071.1Scgd			file1done = !fgets(line1, MAXLINELEN, fp1);
1081.1Scgd		if (read2)
1091.1Scgd			file2done = !fgets(line2, MAXLINELEN, fp2);
1101.1Scgd
1111.1Scgd		/* if one file done, display the rest of the other file */
1121.1Scgd		if (file1done) {
1131.1Scgd			if (!file2done && col2)
1141.1Scgd				show(fp2, col2, line2);
1151.1Scgd			break;
1161.1Scgd		}
1171.1Scgd		if (file2done) {
1181.1Scgd			if (!file1done && col1)
1191.1Scgd				show(fp1, col1, line1);
1201.1Scgd			break;
1211.1Scgd		}
1221.1Scgd
1231.1Scgd		/* lines are the same */
1241.1Scgd		if (!(comp = strcmp(line1, line2))) {
1251.1Scgd			read1 = read2 = 1;
1261.1Scgd			if (col3)
1271.1Scgd				(void)printf("%s%s", col3, line1);
1281.1Scgd			continue;
1291.1Scgd		}
1301.1Scgd
1311.1Scgd		/* lines are different */
1321.1Scgd		if (comp < 0) {
1331.1Scgd			read1 = 1;
1341.1Scgd			read2 = 0;
1351.1Scgd			if (col1)
1361.1Scgd				(void)printf("%s%s", col1, line1);
1371.1Scgd		} else {
1381.1Scgd			read1 = 0;
1391.1Scgd			read2 = 1;
1401.1Scgd			if (col2)
1411.1Scgd				(void)printf("%s%s", col2, line2);
1421.1Scgd		}
1431.1Scgd	}
1441.1Scgd	exit(0);
1451.1Scgd}
1461.1Scgd
1471.1Scgdshow(fp, offset, buf)
1481.1Scgd	FILE *fp;
1491.1Scgd	char *offset, *buf;
1501.1Scgd{
1511.1Scgd	do {
1521.1Scgd		(void)printf("%s%s", offset, buf);
1531.1Scgd	} while (fgets(buf, MAXLINELEN, fp));
1541.1Scgd}
1551.1Scgd
1561.1ScgdFILE *
1571.1Scgdfile(name)
1581.1Scgd	char *name;
1591.1Scgd{
1601.1Scgd	FILE *fp;
1611.1Scgd
1621.1Scgd	if (!strcmp(name, "-"))
1631.1Scgd		return(stdin);
1641.1Scgd	if (!(fp = fopen(name, "r"))) {
1651.1Scgd		(void)fprintf(stderr, "comm: can't read %s.\n", name);
1661.1Scgd		exit(1);
1671.1Scgd	}
1681.1Scgd	return(fp);
1691.1Scgd}
1701.1Scgd
1711.1Scgdusage()
1721.1Scgd{
1731.1Scgd	(void)fprintf(stderr, "usage: comm [-123] [ - ] file1 file2\n");
1741.1Scgd	exit(1);
1751.1Scgd}
176