2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "From: @(#)comm.c 8.4 (Berkeley) 5/4/95";
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
63 #define INITLINELEN (LINE_MAX + 1)
64 #define MAXLINELEN ((SIZE_MAX / sizeof(wchar_t)) / 2)
66 const wchar_t *tabs[] = { L"", L"\t", L"\t\t" };
68 FILE *file(const char *);
69 wchar_t *getline(wchar_t *, size_t *, FILE *);
70 void show(FILE *, const char *, const wchar_t *, wchar_t *, size_t *);
71 int wcsicoll(const wchar_t *, const wchar_t *);
72 static void usage(void);
75 main(int argc, char *argv[])
77 int comp, read1, read2;
78 int ch, flag1, flag2, flag3, iflag;
80 const wchar_t *col1, *col2, *col3;
81 size_t line1len, line2len;
82 wchar_t *line1, *line2;
85 flag1 = flag2 = flag3 = 1;
88 line1len = INITLINELEN;
89 line2len = INITLINELEN;
90 line1 = malloc(line1len * sizeof(*line1));
91 line2 = malloc(line2len * sizeof(*line2));
92 if (line1 == NULL || line2 == NULL)
95 (void) setlocale(LC_ALL, "");
97 while ((ch = getopt(argc, argv, "123i")) != -1)
124 /* for each column printed, add another tab offset */
126 col1 = col2 = col3 = NULL;
134 for (read1 = read2 = 1;;) {
135 /* read next line, check for EOF */
137 line1 = getline(line1, &line1len, fp1);
138 if (line1 == NULL && ferror(fp1))
139 err(1, "%s", argv[0]);
142 line2 = getline(line2, &line2len, fp2);
143 if (line2 == NULL && ferror(fp2))
144 err(1, "%s", argv[1]);
147 /* if one file done, display the rest of the other file */
149 if (line2 != NULL && col2 != NULL)
150 show(fp2, argv[1], col2, line2, &line2len);
154 if (line1 != NULL && col1 != NULL)
155 show(fp1, argv[0], col1, line1, &line1len);
159 /* lines are the same */
161 comp = wcsicoll(line1, line2);
163 comp = wcscoll(line1, line2);
168 (void)printf("%ls%ls\n", col3, line1);
172 /* lines are different */
177 (void)printf("%ls%ls\n", col1, line1);
182 (void)printf("%ls%ls\n", col2, line2);
189 getline(wchar_t *buf, size_t *buflen, FILE *fp)
195 while ((ch = getwc(fp)) != WEOF && ch != '\n') {
196 if (bufpos + 1 >= *buflen) {
197 *buflen = *buflen * 2;
198 if (*buflen > MAXLINELEN)
200 "Maximum line buffer length (%zu) exceeded",
202 buf = reallocf(buf, *buflen * sizeof(*buf));
210 return (bufpos != 0 || ch == '\n' ? buf : NULL);
214 show(FILE *fp, const char *fn, const wchar_t *offset, wchar_t *buf, size_t *buflen)
218 (void)printf("%ls%ls\n", offset, buf);
219 } while ((buf = getline(buf, buflen, fp)) != NULL);
225 file(const char *name)
229 if (!strcmp(name, "-"))
231 if ((fp = fopen(name, "r")) == NULL) {
240 (void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
244 static size_t wcsicoll_l1_buflen = 0, wcsicoll_l2_buflen = 0;
245 static wchar_t *wcsicoll_l1_buf = NULL, *wcsicoll_l2_buf = NULL;
248 wcsicoll(const wchar_t *s1, const wchar_t *s2)
252 size_t new_l1_buflen, new_l2_buflen;
256 new_l1_buflen = wcsicoll_l1_buflen;
257 new_l2_buflen = wcsicoll_l2_buflen;
258 while (new_l1_buflen < l1) {
259 if (new_l1_buflen == 0)
260 new_l1_buflen = INITLINELEN;
264 while (new_l2_buflen < l2) {
265 if (new_l2_buflen == 0)
266 new_l2_buflen = INITLINELEN;
270 if (new_l1_buflen > wcsicoll_l1_buflen) {
271 wcsicoll_l1_buf = reallocf(wcsicoll_l1_buf, new_l1_buflen * sizeof(*wcsicoll_l1_buf));
272 if (wcsicoll_l1_buf == NULL)
274 wcsicoll_l1_buflen = new_l1_buflen;
276 if (new_l2_buflen > wcsicoll_l2_buflen) {
277 wcsicoll_l2_buf = reallocf(wcsicoll_l2_buf, new_l2_buflen * sizeof(*wcsicoll_l2_buf));
278 if (wcsicoll_l2_buf == NULL)
280 wcsicoll_l2_buflen = new_l2_buflen;
283 for (p = wcsicoll_l1_buf; *s1; s1++)
284 *p++ = towlower(*s1);
286 for (p = wcsicoll_l2_buf; *s2; s2++)
287 *p++ = towlower(*s2);
290 return (wcscoll(wcsicoll_l1_buf, wcsicoll_l2_buf));