comm.c revision 1.3
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.3Sjtcstatic char rcsid[] = "$Id: comm.c,v 1.3 1993/09/21 23:20:36 jtc Exp $";
461.1Scgd#endif /* not lint */
471.1Scgd
481.3Sjtc#include <stdio.h>
491.3Sjtc#include <stdlib.h>
501.3Sjtc#include <string.h>
511.1Scgd#include <limits.h>
521.3Sjtc#include <locale.h>
531.1Scgd
541.1Scgd#define	MAXLINELEN	(_POSIX2_LINE_MAX + 1)
551.1Scgd
561.1Scgdchar *tabs[] = { "", "\t", "\t\t" };
571.1Scgd
581.3SjtcFILE *file __P((const char *));
591.3Sjtcvoid  show __P((FILE *, char *, char *));
601.3Sjtcvoid  usage __P((void));
611.3Sjtc
621.3Sjtcint
631.1Scgdmain(argc,argv)
641.1Scgd	int argc;
651.3Sjtc	char **argv;
661.1Scgd{
671.1Scgd	register int comp, file1done, file2done, read1, read2;
681.1Scgd	register char *col1, *col2, *col3;
691.1Scgd	int ch, flag1, flag2, flag3;
701.3Sjtc	FILE *fp1, *fp2;
711.1Scgd	char **p, line1[MAXLINELEN], line2[MAXLINELEN];
721.3Sjtc
731.3Sjtc	setlocale(LC_ALL, "");
741.1Scgd
751.1Scgd	flag1 = flag2 = flag3 = 1;
761.3Sjtc	while ((ch = getopt(argc, argv, "123")) != -1)
771.1Scgd		switch(ch) {
781.1Scgd		case '1':
791.1Scgd			flag1 = 0;
801.1Scgd			break;
811.1Scgd		case '2':
821.1Scgd			flag2 = 0;
831.1Scgd			break;
841.1Scgd		case '3':
851.1Scgd			flag3 = 0;
861.1Scgd			break;
871.1Scgd		case '?':
881.1Scgd		default:
891.1Scgd			usage();
901.1Scgd		}
911.3Sjtc	argc -= optind;
921.1Scgd	argv += optind;
931.1Scgd
941.1Scgd	if (argc != 2)
951.1Scgd		usage();
961.1Scgd
971.1Scgd	fp1 = file(argv[0]);
981.1Scgd	fp2 = file(argv[1]);
991.1Scgd
1001.1Scgd	/* for each column printed, add another tab offset */
1011.1Scgd	p = tabs;
1021.1Scgd	col1 = col2 = col3 = NULL;
1031.1Scgd	if (flag1)
1041.1Scgd		col1 = *p++;
1051.1Scgd	if (flag2)
1061.1Scgd		col2 = *p++;
1071.1Scgd	if (flag3)
1081.1Scgd		col3 = *p;
1091.1Scgd
1101.1Scgd	for (read1 = read2 = 1;;) {
1111.1Scgd		/* read next line, check for EOF */
1121.1Scgd		if (read1)
1131.1Scgd			file1done = !fgets(line1, MAXLINELEN, fp1);
1141.1Scgd		if (read2)
1151.1Scgd			file2done = !fgets(line2, MAXLINELEN, fp2);
1161.1Scgd
1171.1Scgd		/* if one file done, display the rest of the other file */
1181.1Scgd		if (file1done) {
1191.1Scgd			if (!file2done && col2)
1201.1Scgd				show(fp2, col2, line2);
1211.1Scgd			break;
1221.1Scgd		}
1231.1Scgd		if (file2done) {
1241.1Scgd			if (!file1done && col1)
1251.1Scgd				show(fp1, col1, line1);
1261.1Scgd			break;
1271.1Scgd		}
1281.1Scgd
1291.1Scgd		/* lines are the same */
1301.3Sjtc		if (!(comp = strcoll(line1, line2))) {
1311.1Scgd			read1 = read2 = 1;
1321.1Scgd			if (col3)
1331.1Scgd				(void)printf("%s%s", col3, line1);
1341.1Scgd			continue;
1351.1Scgd		}
1361.1Scgd
1371.1Scgd		/* lines are different */
1381.1Scgd		if (comp < 0) {
1391.1Scgd			read1 = 1;
1401.1Scgd			read2 = 0;
1411.1Scgd			if (col1)
1421.1Scgd				(void)printf("%s%s", col1, line1);
1431.1Scgd		} else {
1441.1Scgd			read1 = 0;
1451.1Scgd			read2 = 1;
1461.1Scgd			if (col2)
1471.1Scgd				(void)printf("%s%s", col2, line2);
1481.1Scgd		}
1491.1Scgd	}
1501.1Scgd	exit(0);
1511.1Scgd}
1521.1Scgd
1531.3Sjtcvoid
1541.1Scgdshow(fp, offset, buf)
1551.1Scgd	FILE *fp;
1561.1Scgd	char *offset, *buf;
1571.1Scgd{
1581.1Scgd	do {
1591.1Scgd		(void)printf("%s%s", offset, buf);
1601.1Scgd	} while (fgets(buf, MAXLINELEN, fp));
1611.1Scgd}
1621.1Scgd
1631.1ScgdFILE *
1641.1Scgdfile(name)
1651.3Sjtc	const char *name;
1661.1Scgd{
1671.1Scgd	FILE *fp;
1681.1Scgd
1691.1Scgd	if (!strcmp(name, "-"))
1701.1Scgd		return(stdin);
1711.1Scgd	if (!(fp = fopen(name, "r"))) {
1721.1Scgd		(void)fprintf(stderr, "comm: can't read %s.\n", name);
1731.1Scgd		exit(1);
1741.1Scgd	}
1751.1Scgd	return(fp);
1761.1Scgd}
1771.1Scgd
1781.3Sjtcvoid
1791.1Scgdusage()
1801.1Scgd{
1811.3Sjtc	(void)fprintf(stderr, "usage: comm [-123] file1 file2\n");
1821.1Scgd	exit(1);
1831.1Scgd}
184