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