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$");
65 const char *tabs[] = { "", "\t", "\t\t" };
67 FILE *file(const char *);
68 wchar_t *convert(const char *);
69 void show(FILE *, const char *, const char *, char **, size_t *);
70 static void usage(void);
73 main(int argc, char *argv[])
75 int comp, read1, read2;
76 int ch, flag1, flag2, flag3;
78 const char *col1, *col2, *col3;
79 size_t line1len, line2len;
82 wchar_t *tline1, *tline2;
85 (void) setlocale(LC_ALL, "");
87 flag1 = flag2 = flag3 = 1;
89 while ((ch = getopt(argc, argv, "123i")) != -1)
116 /* for each column printed, add another tab offset */
118 col1 = col2 = col3 = NULL;
126 line1len = line2len = 0;
127 line1 = line2 = NULL;
130 for (read1 = read2 = 1;;) {
131 /* read next line, check for EOF */
133 n1 = getline(&line1, &line1len, fp1);
134 if (n1 < 0 && ferror(fp1))
135 err(1, "%s", argv[0]);
136 if (n1 > 0 && line1[n1 - 1] == '\n')
137 line1[n1 - 1] = '\0';
141 n2 = getline(&line2, &line2len, fp2);
142 if (n2 < 0 && ferror(fp2))
143 err(1, "%s", argv[1]);
144 if (n2 > 0 && line2[n2 - 1] == '\n')
145 line2[n2 - 1] = '\0';
148 /* if one file done, display the rest of the other file */
150 if (n2 >= 0 && col2 != NULL)
151 show(fp2, argv[1], col2, &line2, &line2len);
155 if (n1 >= 0 && col1 != NULL)
156 show(fp1, argv[0], col1, &line1, &line1len);
161 if ((tline1 = convert(line1)) != NULL)
162 tline2 = convert(line2);
163 if (tline1 == NULL || tline2 == NULL)
164 comp = strcmp(line1, line2);
166 comp = wcscoll(tline1, tline2);
172 /* lines are the same */
176 (void)printf("%s%s\n", col3, line1);
180 /* lines are different */
185 (void)printf("%s%s\n", col1, line1);
190 (void)printf("%s%s\n", col2, line2);
197 convert(const char *str)
202 if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
204 if (SIZE_MAX / sizeof(*buf) < n + 1)
205 errx(1, "conversion buffer length overflow");
206 if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
208 if (mbstowcs(buf, str, n + 1) != n)
209 errx(1, "internal mbstowcs() error");
212 for (p = buf; *p != L'\0'; p++)
220 show(FILE *fp, const char *fn, const char *offset, char **bufp, size_t *buflenp)
225 (void)printf("%s%s\n", offset, *bufp);
226 if ((n = getline(bufp, buflenp, fp)) < 0)
228 if (n > 0 && (*bufp)[n - 1] == '\n')
229 (*bufp)[n - 1] = '\0';
236 file(const char *name)
240 if (!strcmp(name, "-"))
242 if ((fp = fopen(name, "r")) == NULL) {
251 (void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");