1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 static const char copyright[] = 37 "@(#) Copyright (c) 1989, 1993, 1994\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39 #endif 40 41 #if 0 42 #endif 43 44 #include <sys/cdefs.h> 45 #include <err.h> 46 #include <limits.h> 47 #include <locale.h> 48 #include <stdint.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 #include <wchar.h> 54 #include <wctype.h> 55 56 static int iflag; 57 static const char *tabs[] = { "", "\t", "\t\t" }; 58 59 static FILE *file(const char *); 60 static wchar_t *convert(const char *); 61 static void show(FILE *, const char *, const char *, char **, size_t *); 62 static void usage(void); 63 64 int 65 main(int argc, char *argv[]) 66 { 67 int comp, read1, read2; 68 int ch, flag1, flag2, flag3; 69 FILE *fp1, *fp2; 70 const char *col1, *col2, *col3; 71 size_t line1len, line2len; 72 char *line1, *line2; 73 ssize_t n1, n2; 74 wchar_t *tline1, *tline2; 75 const char **p; 76 77 (void) setlocale(LC_ALL, ""); 78 79 flag1 = flag2 = flag3 = 1; 80 81 while ((ch = getopt(argc, argv, "123i")) != -1) 82 switch(ch) { 83 case '1': 84 flag1 = 0; 85 break; 86 case '2': 87 flag2 = 0; 88 break; 89 case '3': 90 flag3 = 0; 91 break; 92 case 'i': 93 iflag = 1; 94 break; 95 case '?': 96 default: 97 usage(); 98 } 99 argc -= optind; 100 argv += optind; 101 102 if (argc != 2) 103 usage(); 104 105 fp1 = file(argv[0]); 106 fp2 = file(argv[1]); 107 108 /* for each column printed, add another tab offset */ 109 p = tabs; 110 col1 = col2 = col3 = NULL; 111 if (flag1) 112 col1 = *p++; 113 if (flag2) 114 col2 = *p++; 115 if (flag3) 116 col3 = *p; 117 118 line1len = line2len = 0; 119 line1 = line2 = NULL; 120 n1 = n2 = -1; 121 122 for (read1 = read2 = 1;;) { 123 /* read next line, check for EOF */ 124 if (read1) { 125 n1 = getline(&line1, &line1len, fp1); 126 if (n1 < 0 && ferror(fp1)) 127 err(1, "%s", argv[0]); 128 if (n1 > 0 && line1[n1 - 1] == '\n') 129 line1[n1 - 1] = '\0'; 130 131 } 132 if (read2) { 133 n2 = getline(&line2, &line2len, fp2); 134 if (n2 < 0 && ferror(fp2)) 135 err(1, "%s", argv[1]); 136 if (n2 > 0 && line2[n2 - 1] == '\n') 137 line2[n2 - 1] = '\0'; 138 } 139 140 /* if one file done, display the rest of the other file */ 141 if (n1 < 0) { 142 if (n2 >= 0 && col2 != NULL) 143 show(fp2, argv[1], col2, &line2, &line2len); 144 break; 145 } 146 if (n2 < 0) { 147 if (n1 >= 0 && col1 != NULL) 148 show(fp1, argv[0], col1, &line1, &line1len); 149 break; 150 } 151 152 tline2 = NULL; 153 if ((tline1 = convert(line1)) != NULL) 154 tline2 = convert(line2); 155 if (tline1 == NULL || tline2 == NULL) 156 comp = strcmp(line1, line2); 157 else 158 comp = wcscoll(tline1, tline2); 159 if (tline1 != NULL) 160 free(tline1); 161 if (tline2 != NULL) 162 free(tline2); 163 164 /* lines are the same */ 165 if (!comp) { 166 read1 = read2 = 1; 167 if (col3 != NULL) 168 (void)printf("%s%s\n", col3, line1); 169 continue; 170 } 171 172 /* lines are different */ 173 if (comp < 0) { 174 read1 = 1; 175 read2 = 0; 176 if (col1 != NULL) 177 (void)printf("%s%s\n", col1, line1); 178 } else { 179 read1 = 0; 180 read2 = 1; 181 if (col2 != NULL) 182 (void)printf("%s%s\n", col2, line2); 183 } 184 } 185 exit(0); 186 } 187 188 static wchar_t * 189 convert(const char *str) 190 { 191 size_t n; 192 wchar_t *buf, *p; 193 194 if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1) 195 return (NULL); 196 if (SIZE_MAX / sizeof(*buf) < n + 1) 197 errx(1, "conversion buffer length overflow"); 198 if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL) 199 err(1, "malloc"); 200 if (mbstowcs(buf, str, n + 1) != n) 201 errx(1, "internal mbstowcs() error"); 202 203 if (iflag) { 204 for (p = buf; *p != L'\0'; p++) 205 *p = towlower(*p); 206 } 207 208 return (buf); 209 } 210 211 static void 212 show(FILE *fp, const char *fn, const char *offset, char **bufp, size_t *buflenp) 213 { 214 ssize_t n; 215 216 do { 217 (void)printf("%s%s\n", offset, *bufp); 218 if ((n = getline(bufp, buflenp, fp)) < 0) 219 break; 220 if (n > 0 && (*bufp)[n - 1] == '\n') 221 (*bufp)[n - 1] = '\0'; 222 } while (1); 223 if (ferror(fp)) 224 err(1, "%s", fn); 225 } 226 227 static FILE * 228 file(const char *name) 229 { 230 FILE *fp; 231 232 if (!strcmp(name, "-")) 233 return (stdin); 234 if ((fp = fopen(name, "r")) == NULL) { 235 err(1, "%s", name); 236 } 237 return (fp); 238 } 239 240 static void 241 usage(void) 242 { 243 (void)fprintf(stderr, "usage: comm [-123i] file1 file2\n"); 244 exit(1); 245 } 246