comm.c revision 1.5
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.5Sjtcstatic char rcsid[] = "$Id: comm.c,v 1.5 1994/11/08 18:37:10 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.4Sjtc#include <unistd.h> 541.5Sjtc#include <err.h> 551.1Scgd 561.1Scgd#define MAXLINELEN (_POSIX2_LINE_MAX + 1) 571.1Scgd 581.1Scgdchar *tabs[] = { "", "\t", "\t\t" }; 591.1Scgd 601.3SjtcFILE *file __P((const char *)); 611.3Sjtcvoid show __P((FILE *, char *, char *)); 621.3Sjtcvoid usage __P((void)); 631.3Sjtc 641.3Sjtcint 651.1Scgdmain(argc,argv) 661.1Scgd int argc; 671.3Sjtc char **argv; 681.1Scgd{ 691.1Scgd register int comp, file1done, file2done, read1, read2; 701.1Scgd register char *col1, *col2, *col3; 711.1Scgd int ch, flag1, flag2, flag3; 721.3Sjtc FILE *fp1, *fp2; 731.1Scgd char **p, line1[MAXLINELEN], line2[MAXLINELEN]; 741.3Sjtc 751.3Sjtc setlocale(LC_ALL, ""); 761.1Scgd 771.1Scgd flag1 = flag2 = flag3 = 1; 781.3Sjtc while ((ch = getopt(argc, argv, "123")) != -1) 791.1Scgd switch(ch) { 801.1Scgd case '1': 811.1Scgd flag1 = 0; 821.1Scgd break; 831.1Scgd case '2': 841.1Scgd flag2 = 0; 851.1Scgd break; 861.1Scgd case '3': 871.1Scgd flag3 = 0; 881.1Scgd break; 891.1Scgd case '?': 901.1Scgd default: 911.1Scgd usage(); 921.1Scgd } 931.3Sjtc argc -= optind; 941.1Scgd argv += optind; 951.1Scgd 961.1Scgd if (argc != 2) 971.1Scgd usage(); 981.1Scgd 991.1Scgd fp1 = file(argv[0]); 1001.1Scgd fp2 = file(argv[1]); 1011.1Scgd 1021.1Scgd /* for each column printed, add another tab offset */ 1031.1Scgd p = tabs; 1041.1Scgd col1 = col2 = col3 = NULL; 1051.1Scgd if (flag1) 1061.1Scgd col1 = *p++; 1071.1Scgd if (flag2) 1081.1Scgd col2 = *p++; 1091.1Scgd if (flag3) 1101.1Scgd col3 = *p; 1111.1Scgd 1121.1Scgd for (read1 = read2 = 1;;) { 1131.1Scgd /* read next line, check for EOF */ 1141.1Scgd if (read1) 1151.1Scgd file1done = !fgets(line1, MAXLINELEN, fp1); 1161.1Scgd if (read2) 1171.1Scgd file2done = !fgets(line2, MAXLINELEN, fp2); 1181.1Scgd 1191.1Scgd /* if one file done, display the rest of the other file */ 1201.1Scgd if (file1done) { 1211.1Scgd if (!file2done && col2) 1221.1Scgd show(fp2, col2, line2); 1231.1Scgd break; 1241.1Scgd } 1251.1Scgd if (file2done) { 1261.1Scgd if (!file1done && col1) 1271.1Scgd show(fp1, col1, line1); 1281.1Scgd break; 1291.1Scgd } 1301.1Scgd 1311.1Scgd /* lines are the same */ 1321.3Sjtc if (!(comp = strcoll(line1, line2))) { 1331.1Scgd read1 = read2 = 1; 1341.1Scgd if (col3) 1351.5Sjtc if (printf("%s%s", col3, line1) == EOF) 1361.5Sjtc break; 1371.1Scgd continue; 1381.1Scgd } 1391.1Scgd 1401.1Scgd /* lines are different */ 1411.1Scgd if (comp < 0) { 1421.1Scgd read1 = 1; 1431.1Scgd read2 = 0; 1441.1Scgd if (col1) 1451.5Sjtc if (printf("%s%s", col1, line1) == EOF) 1461.5Sjtc break; 1471.1Scgd } else { 1481.1Scgd read1 = 0; 1491.1Scgd read2 = 1; 1501.1Scgd if (col2) 1511.5Sjtc if (printf("%s%s", col2, line2) == EOF) 1521.5Sjtc break; 1531.1Scgd } 1541.1Scgd } 1551.5Sjtc 1561.5Sjtc if (ferror (stdout) || fclose (stdout) == EOF) 1571.5Sjtc err(1, "stdout"); 1581.5Sjtc 1591.1Scgd exit(0); 1601.1Scgd} 1611.1Scgd 1621.3Sjtcvoid 1631.1Scgdshow(fp, offset, buf) 1641.1Scgd FILE *fp; 1651.1Scgd char *offset, *buf; 1661.1Scgd{ 1671.5Sjtc while (printf("%s%s", offset, buf) != EOF && fgets(buf, MAXLINELEN, fp)) 1681.5Sjtc ; 1691.1Scgd} 1701.1Scgd 1711.1ScgdFILE * 1721.1Scgdfile(name) 1731.3Sjtc const char *name; 1741.1Scgd{ 1751.1Scgd FILE *fp; 1761.1Scgd 1771.1Scgd if (!strcmp(name, "-")) 1781.1Scgd return(stdin); 1791.1Scgd if (!(fp = fopen(name, "r"))) { 1801.1Scgd (void)fprintf(stderr, "comm: can't read %s.\n", name); 1811.1Scgd exit(1); 1821.1Scgd } 1831.1Scgd return(fp); 1841.1Scgd} 1851.1Scgd 1861.3Sjtcvoid 1871.1Scgdusage() 1881.1Scgd{ 1891.3Sjtc (void)fprintf(stderr, "usage: comm [-123] file1 file2\n"); 1901.1Scgd exit(1); 1911.1Scgd} 192