comm.c revision 1.18
11.18Sdarcy/*	$NetBSD: comm.c,v 1.18 2009/11/28 03:56:38 darcy Exp $	*/
21.6Sglass
31.1Scgd/*
41.6Sglass * Copyright (c) 1989, 1993, 1994
51.6Sglass *	The Regents of the University of California.  All rights reserved.
61.1Scgd *
71.1Scgd * This code is derived from software contributed to Berkeley by
81.1Scgd * Case Larsen.
91.1Scgd *
101.1Scgd * Redistribution and use in source and binary forms, with or without
111.1Scgd * modification, are permitted provided that the following conditions
121.1Scgd * are met:
131.1Scgd * 1. Redistributions of source code must retain the above copyright
141.1Scgd *    notice, this list of conditions and the following disclaimer.
151.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
161.1Scgd *    notice, this list of conditions and the following disclaimer in the
171.1Scgd *    documentation and/or other materials provided with the distribution.
181.13Sagc * 3. Neither the name of the University nor the names of its contributors
191.1Scgd *    may be used to endorse or promote products derived from this software
201.1Scgd *    without specific prior written permission.
211.1Scgd *
221.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321.1Scgd * SUCH DAMAGE.
331.1Scgd */
341.1Scgd
351.11Slukem#include <sys/cdefs.h>
361.1Scgd#ifndef lint
371.16Slukem__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
381.16Slukem The Regents of the University of California.  All rights reserved.");
391.1Scgd#endif /* not lint */
401.1Scgd
411.1Scgd#ifndef lint
421.6Sglass#if 0
431.9Sjtcstatic char sccsid[] = "@(#)comm.c	8.4 (Berkeley) 5/4/95";
441.6Sglass#endif
451.18Sdarcy__RCSID("$NetBSD: comm.c,v 1.18 2009/11/28 03:56:38 darcy Exp $");
461.1Scgd#endif /* not lint */
471.1Scgd
481.8Sglass#include <err.h>
491.8Sglass#include <limits.h>
501.8Sglass#include <locale.h>
511.3Sjtc#include <stdio.h>
521.3Sjtc#include <stdlib.h>
531.3Sjtc#include <string.h>
541.4Sjtc#include <unistd.h>
551.1Scgd
561.6Sglass#define	MAXLINELEN	(LINE_MAX + 1)
571.1Scgd
581.17Slukemconst char *tabs[] = { "", "\t", "\t\t" };
591.1Scgd
601.15SxtraemeFILE   *file(const char *);
611.17Slukemvoid	show(FILE *, const char *, char *);
621.15Sxtraemevoid	usage(void);
631.18Sdarcychar   *getnextln(char *buf, FILE *);
641.3Sjtc
651.3Sjtcint
661.15Sxtraememain(int argc, char **argv)
671.1Scgd{
681.6Sglass	int comp, file1done, file2done, read1, read2;
691.1Scgd	int ch, flag1, flag2, flag3;
701.3Sjtc	FILE *fp1, *fp2;
711.17Slukem	const char *col1, *col2, *col3, **p;
721.17Slukem	char line1[MAXLINELEN], line2[MAXLINELEN];
731.12Shubertf	int (*compare)(const char*,const char*);
741.3Sjtc
751.12Shubertf	(void)setlocale(LC_ALL, "");
761.1Scgd
771.11Slukem	file1done = file2done = 0;
781.1Scgd	flag1 = flag2 = flag3 = 1;
791.12Shubertf	compare = strcoll;
801.12Shubertf	while ((ch = getopt(argc, argv, "123f")) != -1)
811.1Scgd		switch(ch) {
821.1Scgd		case '1':
831.1Scgd			flag1 = 0;
841.1Scgd			break;
851.1Scgd		case '2':
861.1Scgd			flag2 = 0;
871.1Scgd			break;
881.1Scgd		case '3':
891.1Scgd			flag3 = 0;
901.1Scgd			break;
911.12Shubertf		case 'f':
921.12Shubertf			compare = strcasecmp;
931.12Shubertf			break;
941.1Scgd		case '?':
951.1Scgd		default:
961.1Scgd			usage();
971.1Scgd		}
981.3Sjtc	argc -= optind;
991.1Scgd	argv += optind;
1001.1Scgd
1011.1Scgd	if (argc != 2)
1021.1Scgd		usage();
1031.1Scgd
1041.1Scgd	fp1 = file(argv[0]);
1051.1Scgd	fp2 = file(argv[1]);
1061.1Scgd
1071.1Scgd	/* for each column printed, add another tab offset */
1081.1Scgd	p = tabs;
1091.1Scgd	col1 = col2 = col3 = NULL;
1101.1Scgd	if (flag1)
1111.1Scgd		col1 = *p++;
1121.1Scgd	if (flag2)
1131.1Scgd		col2 = *p++;
1141.1Scgd	if (flag3)
1151.1Scgd		col3 = *p;
1161.1Scgd
1171.1Scgd	for (read1 = read2 = 1;;) {
1181.1Scgd		/* read next line, check for EOF */
1191.1Scgd		if (read1)
1201.18Sdarcy			file1done = !getnextln(line1, fp1);
1211.1Scgd		if (read2)
1221.18Sdarcy			file2done = !getnextln(line2, fp2);
1231.1Scgd
1241.1Scgd		/* if one file done, display the rest of the other file */
1251.1Scgd		if (file1done) {
1261.1Scgd			if (!file2done && col2)
1271.1Scgd				show(fp2, col2, line2);
1281.1Scgd			break;
1291.1Scgd		}
1301.1Scgd		if (file2done) {
1311.1Scgd			if (!file1done && col1)
1321.1Scgd				show(fp1, col1, line1);
1331.1Scgd			break;
1341.1Scgd		}
1351.1Scgd
1361.1Scgd		/* lines are the same */
1371.12Shubertf		if (!(comp = compare(line1, line2))) {
1381.1Scgd			read1 = read2 = 1;
1391.1Scgd			if (col3)
1401.18Sdarcy				if (printf("%s%s\n", col3, line1) < 0)
1411.5Sjtc					break;
1421.1Scgd			continue;
1431.1Scgd		}
1441.1Scgd
1451.1Scgd		/* lines are different */
1461.1Scgd		if (comp < 0) {
1471.1Scgd			read1 = 1;
1481.1Scgd			read2 = 0;
1491.1Scgd			if (col1)
1501.18Sdarcy				if (printf("%s%s\n", col1, line1) < 0)
1511.5Sjtc					break;
1521.1Scgd		} else {
1531.1Scgd			read1 = 0;
1541.1Scgd			read2 = 1;
1551.1Scgd			if (col2)
1561.18Sdarcy				if (printf("%s%s\n", col2, line2) < 0)
1571.5Sjtc					break;
1581.1Scgd		}
1591.1Scgd	}
1601.5Sjtc
1611.5Sjtc	if (ferror (stdout) || fclose (stdout) == EOF)
1621.5Sjtc		err(1, "stdout");
1631.5Sjtc
1641.1Scgd	exit(0);
1651.1Scgd}
1661.1Scgd
1671.3Sjtcvoid
1681.17Slukemshow(FILE *fp, const char *offset, char *buf)
1691.1Scgd{
1701.18Sdarcy	while (printf("%s%s\n", offset, buf) >= 0 && getnextln(buf, fp))
1711.5Sjtc		;
1721.1Scgd}
1731.1Scgd
1741.1ScgdFILE *
1751.15Sxtraemefile(const char *name)
1761.1Scgd{
1771.1Scgd	FILE *fp;
1781.1Scgd
1791.1Scgd	if (!strcmp(name, "-"))
1801.6Sglass		return (stdin);
1811.8Sglass	if ((fp = fopen(name, "r")) == NULL)
1821.8Sglass		err(1, "%s", name);
1831.6Sglass	return (fp);
1841.1Scgd}
1851.1Scgd
1861.3Sjtcvoid
1871.15Sxtraemeusage(void)
1881.1Scgd{
1891.6Sglass
1901.14Swiz	(void)fprintf(stderr, "usage: comm [-123f] file1 file2\n");
1911.1Scgd	exit(1);
1921.1Scgd}
1931.18Sdarcy
1941.18Sdarcychar *
1951.18Sdarcygetnextln(char *buf, FILE *fp)
1961.18Sdarcy{
1971.18Sdarcy	size_t i = 0;
1981.18Sdarcy	int c;
1991.18Sdarcy
2001.18Sdarcy	while ((c = fgetc(fp)) != '\n' && c != EOF) {
2011.18Sdarcy		buf[i++] = c;
2021.18Sdarcy
2031.18Sdarcy		if (i >= MAXLINELEN)
2041.18Sdarcy			i--; /* consumes extra characters till newline */
2051.18Sdarcy	}
2061.18Sdarcy
2071.18Sdarcy	if (c == EOF && !i)
2081.18Sdarcy		return NULL;
2091.18Sdarcy
2101.18Sdarcy	buf[i] = 0;
2111.18Sdarcy	return buf;
2121.18Sdarcy}
2131.18Sdarcy
214