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