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$");
62 #define MAXLINELEN (LINE_MAX + 1)
64 const wchar_t *tabs[] = { L"", L"\t", L"\t\t" };
66 FILE *file(const char *);
67 wchar_t *getline(wchar_t *, size_t *, FILE *);
68 void show(FILE *, const char *, const wchar_t *, wchar_t *, size_t *);
69 int wcsicoll(const wchar_t *, const wchar_t *);
70 static void usage(void);
73 main(int argc, char *argv[])
75 int comp, read1, read2;
76 int ch, flag1, flag2, flag3, iflag;
78 const wchar_t *col1, *col2, *col3;
79 size_t line1len, line2len;
80 wchar_t *line1, *line2;
83 flag1 = flag2 = flag3 = 1;
86 line1len = MAXLINELEN;
87 line2len = MAXLINELEN;
88 line1 = malloc(line1len * sizeof(*line1));
89 line2 = malloc(line2len * sizeof(*line2));
90 if (line1 == NULL || line2 == NULL)
93 (void) setlocale(LC_ALL, "");
95 while ((ch = getopt(argc, argv, "123i")) != -1)
122 /* for each column printed, add another tab offset */
124 col1 = col2 = col3 = NULL;
132 for (read1 = read2 = 1;;) {
133 /* read next line, check for EOF */
135 line1 = getline(line1, &line1len, fp1);
136 if (line1 == NULL && ferror(fp1))
137 err(1, "%s", argv[0]);
140 line2 = getline(line2, &line2len, fp2);
141 if (line2 == NULL && ferror(fp2))
142 err(1, "%s", argv[1]);
145 /* if one file done, display the rest of the other file */
147 if (line2 != NULL && col2 != NULL)
148 show(fp2, argv[1], col2, line2, &line2len);
152 if (line1 != NULL && col1 != NULL)
153 show(fp1, argv[0], col1, line1, &line1len);
157 /* lines are the same */
159 comp = wcsicoll(line1, line2);
161 comp = wcscoll(line1, line2);
166 (void)printf("%ls%ls", col3, line1);
170 /* lines are different */
175 (void)printf("%ls%ls", col1, line1);
180 (void)printf("%ls%ls", col2, line2);
187 getline(wchar_t *buf, size_t *buflen, FILE *fp)
194 if ((ch = getwc(fp)) != WEOF) {
195 if (bufpos + 2 >= *buflen) {
196 *buflen = *buflen * 2;
197 buf = reallocf(buf, *buflen * sizeof(*buf));
203 } while (ch != WEOF && ch != '\n');
204 if (bufpos + 1 != *buflen)
207 return (bufpos != 0 || ch == '\n' ? buf : NULL);
211 show(FILE *fp, const char *fn, const wchar_t *offset, wchar_t *buf, size_t *buflen)
215 (void)printf("%ls%ls", offset, buf);
216 } while ((buf = getline(buf, buflen, fp)) != NULL);
222 file(const char *name)
226 if (!strcmp(name, "-"))
228 if ((fp = fopen(name, "r")) == NULL) {
237 (void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
241 static size_t wcsicoll_l1_buflen = 0, wcsicoll_l2_buflen = 0;
242 static wchar_t *wcsicoll_l1_buf = NULL, *wcsicoll_l2_buf = NULL;
245 wcsicoll(const wchar_t *s1, const wchar_t *s2)
249 size_t new_l1_buflen, new_l2_buflen;
253 new_l1_buflen = wcsicoll_l1_buflen;
254 new_l2_buflen = wcsicoll_l2_buflen;
255 while (new_l1_buflen < l1) {
256 if (new_l1_buflen == 0)
257 new_l1_buflen = MAXLINELEN;
261 while (new_l2_buflen < l2) {
262 if (new_l2_buflen == 0)
263 new_l2_buflen = MAXLINELEN;
267 if (new_l1_buflen > wcsicoll_l1_buflen) {
268 wcsicoll_l1_buf = reallocf(wcsicoll_l1_buf, new_l1_buflen * sizeof(*wcsicoll_l1_buf));
269 if (wcsicoll_l1_buf == NULL)
271 wcsicoll_l1_buflen = new_l1_buflen;
273 if (new_l2_buflen > wcsicoll_l2_buflen) {
274 wcsicoll_l2_buf = reallocf(wcsicoll_l2_buf, new_l2_buflen * sizeof(*wcsicoll_l2_buf));
275 if (wcsicoll_l2_buf == NULL)
277 wcsicoll_l2_buflen = new_l2_buflen;
280 for (p = wcsicoll_l1_buf; *s1; s1++)
281 *p++ = towlower(*s1);
283 for (p = wcsicoll_l2_buf; *s2; s2++)
284 *p++ = towlower(*s2);
287 return (wcscoll(wcsicoll_l1_buf, wcsicoll_l2_buf));