comm.c revision 1.4
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.4Sjtcstatic char rcsid[] = "$Id: comm.c,v 1.4 1993/10/13 18:33:45 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.1Scgd 551.1Scgd#define MAXLINELEN (_POSIX2_LINE_MAX + 1) 561.1Scgd 571.1Scgdchar *tabs[] = { "", "\t", "\t\t" }; 581.1Scgd 591.3SjtcFILE *file __P((const char *)); 601.3Sjtcvoid show __P((FILE *, char *, char *)); 611.3Sjtcvoid usage __P((void)); 621.3Sjtc 631.3Sjtcint 641.1Scgdmain(argc,argv) 651.1Scgd int argc; 661.3Sjtc char **argv; 671.1Scgd{ 681.1Scgd register int comp, file1done, file2done, read1, read2; 691.1Scgd register char *col1, *col2, *col3; 701.1Scgd int ch, flag1, flag2, flag3; 711.3Sjtc FILE *fp1, *fp2; 721.1Scgd char **p, line1[MAXLINELEN], line2[MAXLINELEN]; 731.3Sjtc 741.3Sjtc setlocale(LC_ALL, ""); 751.1Scgd 761.1Scgd flag1 = flag2 = flag3 = 1; 771.3Sjtc while ((ch = getopt(argc, argv, "123")) != -1) 781.1Scgd switch(ch) { 791.1Scgd case '1': 801.1Scgd flag1 = 0; 811.1Scgd break; 821.1Scgd case '2': 831.1Scgd flag2 = 0; 841.1Scgd break; 851.1Scgd case '3': 861.1Scgd flag3 = 0; 871.1Scgd break; 881.1Scgd case '?': 891.1Scgd default: 901.1Scgd usage(); 911.1Scgd } 921.3Sjtc argc -= optind; 931.1Scgd argv += optind; 941.1Scgd 951.1Scgd if (argc != 2) 961.1Scgd usage(); 971.1Scgd 981.1Scgd fp1 = file(argv[0]); 991.1Scgd fp2 = file(argv[1]); 1001.1Scgd 1011.1Scgd /* for each column printed, add another tab offset */ 1021.1Scgd p = tabs; 1031.1Scgd col1 = col2 = col3 = NULL; 1041.1Scgd if (flag1) 1051.1Scgd col1 = *p++; 1061.1Scgd if (flag2) 1071.1Scgd col2 = *p++; 1081.1Scgd if (flag3) 1091.1Scgd col3 = *p; 1101.1Scgd 1111.1Scgd for (read1 = read2 = 1;;) { 1121.1Scgd /* read next line, check for EOF */ 1131.1Scgd if (read1) 1141.1Scgd file1done = !fgets(line1, MAXLINELEN, fp1); 1151.1Scgd if (read2) 1161.1Scgd file2done = !fgets(line2, MAXLINELEN, fp2); 1171.1Scgd 1181.1Scgd /* if one file done, display the rest of the other file */ 1191.1Scgd if (file1done) { 1201.1Scgd if (!file2done && col2) 1211.1Scgd show(fp2, col2, line2); 1221.1Scgd break; 1231.1Scgd } 1241.1Scgd if (file2done) { 1251.1Scgd if (!file1done && col1) 1261.1Scgd show(fp1, col1, line1); 1271.1Scgd break; 1281.1Scgd } 1291.1Scgd 1301.1Scgd /* lines are the same */ 1311.3Sjtc if (!(comp = strcoll(line1, line2))) { 1321.1Scgd read1 = read2 = 1; 1331.1Scgd if (col3) 1341.1Scgd (void)printf("%s%s", col3, line1); 1351.1Scgd continue; 1361.1Scgd } 1371.1Scgd 1381.1Scgd /* lines are different */ 1391.1Scgd if (comp < 0) { 1401.1Scgd read1 = 1; 1411.1Scgd read2 = 0; 1421.1Scgd if (col1) 1431.1Scgd (void)printf("%s%s", col1, line1); 1441.1Scgd } else { 1451.1Scgd read1 = 0; 1461.1Scgd read2 = 1; 1471.1Scgd if (col2) 1481.1Scgd (void)printf("%s%s", col2, line2); 1491.1Scgd } 1501.1Scgd } 1511.1Scgd exit(0); 1521.1Scgd} 1531.1Scgd 1541.3Sjtcvoid 1551.1Scgdshow(fp, offset, buf) 1561.1Scgd FILE *fp; 1571.1Scgd char *offset, *buf; 1581.1Scgd{ 1591.1Scgd do { 1601.1Scgd (void)printf("%s%s", offset, buf); 1611.1Scgd } while (fgets(buf, MAXLINELEN, fp)); 1621.1Scgd} 1631.1Scgd 1641.1ScgdFILE * 1651.1Scgdfile(name) 1661.3Sjtc const char *name; 1671.1Scgd{ 1681.1Scgd FILE *fp; 1691.1Scgd 1701.1Scgd if (!strcmp(name, "-")) 1711.1Scgd return(stdin); 1721.1Scgd if (!(fp = fopen(name, "r"))) { 1731.1Scgd (void)fprintf(stderr, "comm: can't read %s.\n", name); 1741.1Scgd exit(1); 1751.1Scgd } 1761.1Scgd return(fp); 1771.1Scgd} 1781.1Scgd 1791.3Sjtcvoid 1801.1Scgdusage() 1811.1Scgd{ 1821.3Sjtc (void)fprintf(stderr, "usage: comm [-123] file1 file2\n"); 1831.1Scgd exit(1); 1841.1Scgd} 185