1 /* $NetBSD: comm.c,v 1.12 1999/02/11 00:53:24 hubertf Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Case Larsen. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\ 42 The Regents of the University of California. All rights reserved.\n"); 43 #endif /* not lint */ 44 45 #ifndef lint 46 #if 0 47 static char sccsid[] = "@(#)comm.c 8.4 (Berkeley) 5/4/95"; 48 #endif 49 __RCSID("$NetBSD: comm.c,v 1.12 1999/02/11 00:53:24 hubertf Exp $"); 50 #endif /* not lint */ 51 52 #include <err.h> 53 #include <limits.h> 54 #include <locale.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 60 #define MAXLINELEN (LINE_MAX + 1) 61 62 char *tabs[] = { "", "\t", "\t\t" }; 63 64 FILE *file __P((const char *)); 65 int main __P((int, char **)); 66 void show __P((FILE *, char *, char *)); 67 void usage __P((void)); 68 69 int 70 main(argc, argv) 71 int argc; 72 char **argv; 73 { 74 int comp, file1done, file2done, read1, read2; 75 int ch, flag1, flag2, flag3; 76 FILE *fp1, *fp2; 77 char *col1, *col2, *col3; 78 char **p, line1[MAXLINELEN], line2[MAXLINELEN]; 79 int (*compare)(const char*,const char*); 80 81 (void)setlocale(LC_ALL, ""); 82 83 file1done = file2done = 0; 84 flag1 = flag2 = flag3 = 1; 85 compare = strcoll; 86 while ((ch = getopt(argc, argv, "123f")) != -1) 87 switch(ch) { 88 case '1': 89 flag1 = 0; 90 break; 91 case '2': 92 flag2 = 0; 93 break; 94 case '3': 95 flag3 = 0; 96 break; 97 case 'f': 98 compare = strcasecmp; 99 break; 100 case '?': 101 default: 102 usage(); 103 } 104 argc -= optind; 105 argv += optind; 106 107 if (argc != 2) 108 usage(); 109 110 fp1 = file(argv[0]); 111 fp2 = file(argv[1]); 112 113 /* for each column printed, add another tab offset */ 114 p = tabs; 115 col1 = col2 = col3 = NULL; 116 if (flag1) 117 col1 = *p++; 118 if (flag2) 119 col2 = *p++; 120 if (flag3) 121 col3 = *p; 122 123 for (read1 = read2 = 1;;) { 124 /* read next line, check for EOF */ 125 if (read1) 126 file1done = !fgets(line1, MAXLINELEN, fp1); 127 if (read2) 128 file2done = !fgets(line2, MAXLINELEN, fp2); 129 130 /* if one file done, display the rest of the other file */ 131 if (file1done) { 132 if (!file2done && col2) 133 show(fp2, col2, line2); 134 break; 135 } 136 if (file2done) { 137 if (!file1done && col1) 138 show(fp1, col1, line1); 139 break; 140 } 141 142 /* lines are the same */ 143 if (!(comp = compare(line1, line2))) { 144 read1 = read2 = 1; 145 if (col3) 146 if (printf("%s%s", col3, line1) < 0) 147 break; 148 continue; 149 } 150 151 /* lines are different */ 152 if (comp < 0) { 153 read1 = 1; 154 read2 = 0; 155 if (col1) 156 if (printf("%s%s", col1, line1) < 0) 157 break; 158 } else { 159 read1 = 0; 160 read2 = 1; 161 if (col2) 162 if (printf("%s%s", col2, line2) < 0) 163 break; 164 } 165 } 166 167 if (ferror (stdout) || fclose (stdout) == EOF) 168 err(1, "stdout"); 169 170 exit(0); 171 } 172 173 void 174 show(fp, offset, buf) 175 FILE *fp; 176 char *offset, *buf; 177 { 178 while (printf("%s%s", offset, buf) >= 0 && fgets(buf, MAXLINELEN, fp)) 179 ; 180 } 181 182 FILE * 183 file(name) 184 const char *name; 185 { 186 FILE *fp; 187 188 if (!strcmp(name, "-")) 189 return (stdin); 190 if ((fp = fopen(name, "r")) == NULL) 191 err(1, "%s", name); 192 return (fp); 193 } 194 195 void 196 usage() 197 { 198 199 (void)fprintf(stderr, "usage: comm [-123] file1 file2\n"); 200 exit(1); 201 } 202