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 /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "From: @(#)comm.c 8.4 (Berkeley) 5/4/95"; 46 #endif 47 static const char rcsid[] = 48 "$FreeBSD$"; 49 #endif /* not lint */ 50 51 #include <ctype.h> 52 #include <err.h> 53 #include <limits.h> 54 #include <locale.h> 55 #include <stdio.h> 56 #include <string.h> 57 #include <unistd.h> 58 59 #define MAXLINELEN (LINE_MAX + 1) 60 61 char *tabs[] = { "", "\t", "\t\t" }; 62 63 FILE *file __P((char *)); 64 void show __P((FILE *, char *, char *)); 65 int stricoll __P((char *, char *)); 66 static void usage __P((void)); 67 68 int 69 main(argc, argv) 70 int argc; 71 char *argv[]; 72 { 73 int comp, file1done = 0, file2done = 0, read1, read2; 74 int ch, flag1, flag2, flag3, iflag; 75 FILE *fp1, *fp2; 76 char *col1, *col2, *col3; 77 char **p, line1[MAXLINELEN], line2[MAXLINELEN]; 78 79 flag1 = flag2 = flag3 = 1; 80 iflag = 0; 81 82 (void) setlocale(LC_CTYPE, ""); 83 84 while ((ch = getopt(argc, argv, "-123i")) != -1) 85 switch(ch) { 86 case '-': 87 --optind; 88 goto done; 89 case '1': 90 flag1 = 0; 91 break; 92 case '2': 93 flag2 = 0; 94 break; 95 case '3': 96 flag3 = 0; 97 break; 98 case 'i': 99 iflag = 1; 100 break; 101 case '?': 102 default: 103 usage(); 104 } 105 done: argc -= optind; 106 argv += optind; 107 108 if (argc != 2) 109 usage(); 110 111 fp1 = file(argv[0]); 112 fp2 = file(argv[1]); 113 114 /* for each column printed, add another tab offset */ 115 p = tabs; 116 col1 = col2 = col3 = NULL; 117 if (flag1) 118 col1 = *p++; 119 if (flag2) 120 col2 = *p++; 121 if (flag3) 122 col3 = *p; 123 124 for (read1 = read2 = 1;;) { 125 /* read next line, check for EOF */ 126 if (read1) 127 file1done = !fgets(line1, MAXLINELEN, fp1); 128 if (read2) 129 file2done = !fgets(line2, MAXLINELEN, fp2); 130 131 /* if one file done, display the rest of the other file */ 132 if (file1done) { 133 if (!file2done && col2) 134 show(fp2, col2, line2); 135 break; 136 } 137 if (file2done) { 138 if (!file1done && col1) 139 show(fp1, col1, line1); 140 break; 141 } 142 143 /* lines are the same */ 144 if(iflag) 145 comp = stricoll(line1, line2); 146 else 147 comp = strcoll(line1, line2); 148 149 if (!comp) { 150 read1 = read2 = 1; 151 if (col3) 152 (void)printf("%s%s", col3, line1); 153 continue; 154 } 155 156 /* lines are different */ 157 if (comp < 0) { 158 read1 = 1; 159 read2 = 0; 160 if (col1) 161 (void)printf("%s%s", col1, line1); 162 } else { 163 read1 = 0; 164 read2 = 1; 165 if (col2) 166 (void)printf("%s%s", col2, line2); 167 } 168 } 169 exit(0); 170 } 171 172 void 173 show(fp, offset, buf) 174 FILE *fp; 175 char *offset, *buf; 176 { 177 178 do { 179 (void)printf("%s%s", offset, buf); 180 } while (fgets(buf, MAXLINELEN, fp)); 181 } 182 183 FILE * 184 file(name) 185 char *name; 186 { 187 FILE *fp; 188 189 if (!strcmp(name, "-")) 190 return (stdin); 191 if ((fp = fopen(name, "r")) == NULL) { 192 err(1, "%s", name); 193 } 194 return (fp); 195 } 196 197 static void 198 usage() 199 { 200 (void)fprintf(stderr, "usage: comm [-123i] file1 file2\n"); 201 exit(1); 202 } 203 204 int 205 stricoll(s1, s2) 206 char *s1, *s2; 207 { 208 char *p, line1[MAXLINELEN], line2[MAXLINELEN]; 209 210 for (p = line1; *s1; s1++) 211 *p++ = tolower((unsigned char)*s1); 212 *p = '\0'; 213 for (p = line2; *s2; s2++) 214 *p++ = tolower((unsigned char)*s2); 215 *p = '\0'; 216 return strcoll(line1, line2); 217 } 218