comm.c revision 1.12
11.12Shubertf/* $NetBSD: comm.c,v 1.12 1999/02/11 00:53:24 hubertf 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.1Scgd * 3. All advertising materials mentioning features or use of this software 191.1Scgd * must display the following acknowledgement: 201.1Scgd * This product includes software developed by the University of 211.1Scgd * California, Berkeley and its contributors. 221.1Scgd * 4. Neither the name of the University nor the names of its contributors 231.1Scgd * may be used to endorse or promote products derived from this software 241.1Scgd * without specific prior written permission. 251.1Scgd * 261.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 271.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 281.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 291.1Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 301.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 311.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 321.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 331.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 341.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 351.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361.1Scgd * SUCH DAMAGE. 371.1Scgd */ 381.1Scgd 391.11Slukem#include <sys/cdefs.h> 401.1Scgd#ifndef lint 411.11Slukem__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\ 421.11Slukem The Regents of the University of California. All rights reserved.\n"); 431.1Scgd#endif /* not lint */ 441.1Scgd 451.1Scgd#ifndef lint 461.6Sglass#if 0 471.9Sjtcstatic char sccsid[] = "@(#)comm.c 8.4 (Berkeley) 5/4/95"; 481.6Sglass#endif 491.12Shubertf__RCSID("$NetBSD: comm.c,v 1.12 1999/02/11 00:53:24 hubertf Exp $"); 501.1Scgd#endif /* not lint */ 511.1Scgd 521.8Sglass#include <err.h> 531.8Sglass#include <limits.h> 541.8Sglass#include <locale.h> 551.3Sjtc#include <stdio.h> 561.3Sjtc#include <stdlib.h> 571.3Sjtc#include <string.h> 581.4Sjtc#include <unistd.h> 591.1Scgd 601.6Sglass#define MAXLINELEN (LINE_MAX + 1) 611.1Scgd 621.1Scgdchar *tabs[] = { "", "\t", "\t\t" }; 631.1Scgd 641.6SglassFILE *file __P((const char *)); 651.11Slukemint main __P((int, char **)); 661.6Sglassvoid show __P((FILE *, char *, char *)); 671.6Sglassvoid usage __P((void)); 681.3Sjtc 691.3Sjtcint 701.6Sglassmain(argc, argv) 711.1Scgd int argc; 721.3Sjtc char **argv; 731.1Scgd{ 741.6Sglass int comp, file1done, file2done, read1, read2; 751.1Scgd int ch, flag1, flag2, flag3; 761.3Sjtc FILE *fp1, *fp2; 771.6Sglass char *col1, *col2, *col3; 781.1Scgd char **p, line1[MAXLINELEN], line2[MAXLINELEN]; 791.12Shubertf int (*compare)(const char*,const char*); 801.3Sjtc 811.12Shubertf (void)setlocale(LC_ALL, ""); 821.1Scgd 831.11Slukem file1done = file2done = 0; 841.1Scgd flag1 = flag2 = flag3 = 1; 851.12Shubertf compare = strcoll; 861.12Shubertf while ((ch = getopt(argc, argv, "123f")) != -1) 871.1Scgd switch(ch) { 881.1Scgd case '1': 891.1Scgd flag1 = 0; 901.1Scgd break; 911.1Scgd case '2': 921.1Scgd flag2 = 0; 931.1Scgd break; 941.1Scgd case '3': 951.1Scgd flag3 = 0; 961.1Scgd break; 971.12Shubertf case 'f': 981.12Shubertf compare = strcasecmp; 991.12Shubertf break; 1001.1Scgd case '?': 1011.1Scgd default: 1021.1Scgd usage(); 1031.1Scgd } 1041.3Sjtc argc -= optind; 1051.1Scgd argv += optind; 1061.1Scgd 1071.1Scgd if (argc != 2) 1081.1Scgd usage(); 1091.1Scgd 1101.1Scgd fp1 = file(argv[0]); 1111.1Scgd fp2 = file(argv[1]); 1121.1Scgd 1131.1Scgd /* for each column printed, add another tab offset */ 1141.1Scgd p = tabs; 1151.1Scgd col1 = col2 = col3 = NULL; 1161.1Scgd if (flag1) 1171.1Scgd col1 = *p++; 1181.1Scgd if (flag2) 1191.1Scgd col2 = *p++; 1201.1Scgd if (flag3) 1211.1Scgd col3 = *p; 1221.1Scgd 1231.1Scgd for (read1 = read2 = 1;;) { 1241.1Scgd /* read next line, check for EOF */ 1251.1Scgd if (read1) 1261.1Scgd file1done = !fgets(line1, MAXLINELEN, fp1); 1271.1Scgd if (read2) 1281.1Scgd file2done = !fgets(line2, MAXLINELEN, fp2); 1291.1Scgd 1301.1Scgd /* if one file done, display the rest of the other file */ 1311.1Scgd if (file1done) { 1321.1Scgd if (!file2done && col2) 1331.1Scgd show(fp2, col2, line2); 1341.1Scgd break; 1351.1Scgd } 1361.1Scgd if (file2done) { 1371.1Scgd if (!file1done && col1) 1381.1Scgd show(fp1, col1, line1); 1391.1Scgd break; 1401.1Scgd } 1411.1Scgd 1421.1Scgd /* lines are the same */ 1431.12Shubertf if (!(comp = compare(line1, line2))) { 1441.1Scgd read1 = read2 = 1; 1451.1Scgd if (col3) 1461.7Sjtc if (printf("%s%s", col3, line1) < 0) 1471.5Sjtc break; 1481.1Scgd continue; 1491.1Scgd } 1501.1Scgd 1511.1Scgd /* lines are different */ 1521.1Scgd if (comp < 0) { 1531.1Scgd read1 = 1; 1541.1Scgd read2 = 0; 1551.1Scgd if (col1) 1561.7Sjtc if (printf("%s%s", col1, line1) < 0) 1571.5Sjtc break; 1581.1Scgd } else { 1591.1Scgd read1 = 0; 1601.1Scgd read2 = 1; 1611.1Scgd if (col2) 1621.7Sjtc if (printf("%s%s", col2, line2) < 0) 1631.5Sjtc break; 1641.1Scgd } 1651.1Scgd } 1661.5Sjtc 1671.5Sjtc if (ferror (stdout) || fclose (stdout) == EOF) 1681.5Sjtc err(1, "stdout"); 1691.5Sjtc 1701.1Scgd exit(0); 1711.1Scgd} 1721.1Scgd 1731.3Sjtcvoid 1741.1Scgdshow(fp, offset, buf) 1751.1Scgd FILE *fp; 1761.1Scgd char *offset, *buf; 1771.1Scgd{ 1781.10Sjtc while (printf("%s%s", offset, buf) >= 0 && fgets(buf, MAXLINELEN, fp)) 1791.5Sjtc ; 1801.1Scgd} 1811.1Scgd 1821.1ScgdFILE * 1831.1Scgdfile(name) 1841.3Sjtc const char *name; 1851.1Scgd{ 1861.1Scgd FILE *fp; 1871.1Scgd 1881.1Scgd if (!strcmp(name, "-")) 1891.6Sglass return (stdin); 1901.8Sglass if ((fp = fopen(name, "r")) == NULL) 1911.8Sglass err(1, "%s", name); 1921.6Sglass return (fp); 1931.1Scgd} 1941.1Scgd 1951.3Sjtcvoid 1961.1Scgdusage() 1971.1Scgd{ 1981.6Sglass 1991.3Sjtc (void)fprintf(stderr, "usage: comm [-123] file1 file2\n"); 2001.1Scgd exit(1); 2011.1Scgd} 202