comm.c revision 1.2
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.2Smycroft/*static char sccsid[] = "from: @(#)comm.c	5.7 (Berkeley) 11/1/90";*/
451.2Smycroftstatic char rcsid[] = "$Id: comm.c,v 1.2 1993/08/01 18:17:21 mycroft Exp $";
461.1Scgd#endif /* not lint */
471.1Scgd
481.1Scgd#include <sys/file.h>
491.1Scgd#include <limits.h>
501.1Scgd#include <stdio.h>
511.1Scgd
521.1Scgd#define	MAXLINELEN	(_POSIX2_LINE_MAX + 1)
531.1Scgd
541.1Scgdchar *tabs[] = { "", "\t", "\t\t" };
551.1Scgd
561.1Scgdmain(argc,argv)
571.1Scgd	int argc;
581.1Scgd	char *argv[];
591.1Scgd{
601.1Scgd	register int comp, file1done, file2done, read1, read2;
611.1Scgd	register char *col1, *col2, *col3;
621.1Scgd	int ch, flag1, flag2, flag3;
631.1Scgd	FILE *fp1, *fp2, *file();
641.1Scgd	char **p, line1[MAXLINELEN], line2[MAXLINELEN];
651.1Scgd	extern int optind;
661.1Scgd
671.1Scgd	flag1 = flag2 = flag3 = 1;
681.1Scgd	while ((ch = getopt(argc, argv, "-123")) != EOF)
691.1Scgd		switch(ch) {
701.1Scgd		case '-':
711.1Scgd			--optind;
721.1Scgd			goto done;
731.1Scgd		case '1':
741.1Scgd			flag1 = 0;
751.1Scgd			break;
761.1Scgd		case '2':
771.1Scgd			flag2 = 0;
781.1Scgd			break;
791.1Scgd		case '3':
801.1Scgd			flag3 = 0;
811.1Scgd			break;
821.1Scgd		case '?':
831.1Scgd		default:
841.1Scgd			usage();
851.1Scgd		}
861.1Scgddone:	argc -= optind;
871.1Scgd	argv += optind;
881.1Scgd
891.1Scgd	if (argc != 2)
901.1Scgd		usage();
911.1Scgd
921.1Scgd	fp1 = file(argv[0]);
931.1Scgd	fp2 = file(argv[1]);
941.1Scgd
951.1Scgd	/* for each column printed, add another tab offset */
961.1Scgd	p = tabs;
971.1Scgd	col1 = col2 = col3 = NULL;
981.1Scgd	if (flag1)
991.1Scgd		col1 = *p++;
1001.1Scgd	if (flag2)
1011.1Scgd		col2 = *p++;
1021.1Scgd	if (flag3)
1031.1Scgd		col3 = *p;
1041.1Scgd
1051.1Scgd	for (read1 = read2 = 1;;) {
1061.1Scgd		/* read next line, check for EOF */
1071.1Scgd		if (read1)
1081.1Scgd			file1done = !fgets(line1, MAXLINELEN, fp1);
1091.1Scgd		if (read2)
1101.1Scgd			file2done = !fgets(line2, MAXLINELEN, fp2);
1111.1Scgd
1121.1Scgd		/* if one file done, display the rest of the other file */
1131.1Scgd		if (file1done) {
1141.1Scgd			if (!file2done && col2)
1151.1Scgd				show(fp2, col2, line2);
1161.1Scgd			break;
1171.1Scgd		}
1181.1Scgd		if (file2done) {
1191.1Scgd			if (!file1done && col1)
1201.1Scgd				show(fp1, col1, line1);
1211.1Scgd			break;
1221.1Scgd		}
1231.1Scgd
1241.1Scgd		/* lines are the same */
1251.1Scgd		if (!(comp = strcmp(line1, line2))) {
1261.1Scgd			read1 = read2 = 1;
1271.1Scgd			if (col3)
1281.1Scgd				(void)printf("%s%s", col3, line1);
1291.1Scgd			continue;
1301.1Scgd		}
1311.1Scgd
1321.1Scgd		/* lines are different */
1331.1Scgd		if (comp < 0) {
1341.1Scgd			read1 = 1;
1351.1Scgd			read2 = 0;
1361.1Scgd			if (col1)
1371.1Scgd				(void)printf("%s%s", col1, line1);
1381.1Scgd		} else {
1391.1Scgd			read1 = 0;
1401.1Scgd			read2 = 1;
1411.1Scgd			if (col2)
1421.1Scgd				(void)printf("%s%s", col2, line2);
1431.1Scgd		}
1441.1Scgd	}
1451.1Scgd	exit(0);
1461.1Scgd}
1471.1Scgd
1481.1Scgdshow(fp, offset, buf)
1491.1Scgd	FILE *fp;
1501.1Scgd	char *offset, *buf;
1511.1Scgd{
1521.1Scgd	do {
1531.1Scgd		(void)printf("%s%s", offset, buf);
1541.1Scgd	} while (fgets(buf, MAXLINELEN, fp));
1551.1Scgd}
1561.1Scgd
1571.1ScgdFILE *
1581.1Scgdfile(name)
1591.1Scgd	char *name;
1601.1Scgd{
1611.1Scgd	FILE *fp;
1621.1Scgd
1631.1Scgd	if (!strcmp(name, "-"))
1641.1Scgd		return(stdin);
1651.1Scgd	if (!(fp = fopen(name, "r"))) {
1661.1Scgd		(void)fprintf(stderr, "comm: can't read %s.\n", name);
1671.1Scgd		exit(1);
1681.1Scgd	}
1691.1Scgd	return(fp);
1701.1Scgd}
1711.1Scgd
1721.1Scgdusage()
1731.1Scgd{
1741.1Scgd	(void)fprintf(stderr, "usage: comm [-123] [ - ] file1 file2\n");
1751.1Scgd	exit(1);
1761.1Scgd}
177