xref: /freebsd/usr.bin/comm/comm.c (revision 9fd69f37d28cfd7438cac3eeb45fe9dd46b4d7dd)
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
42 
43 #if 0
44 #ifndef lint
45 static char sccsid[] = "From: @(#)comm.c	8.4 (Berkeley) 5/4/95";
46 #endif
47 #endif
48 
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
51 
52 #include <err.h>
53 #include <limits.h>
54 #include <locale.h>
55 #include <stdint.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <wchar.h>
61 #include <wctype.h>
62 
63 #define	INITLINELEN	(LINE_MAX + 1)
64 #define	MAXLINELEN	((SIZE_MAX / sizeof(wchar_t)) / 2)
65 
66 const wchar_t *tabs[] = { L"", L"\t", L"\t\t" };
67 
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);
73 
74 int
75 main(int argc, char *argv[])
76 {
77 	int comp, read1, read2;
78 	int ch, flag1, flag2, flag3, iflag;
79 	FILE *fp1, *fp2;
80 	const wchar_t *col1, *col2, *col3;
81 	size_t line1len, line2len;
82 	wchar_t *line1, *line2;
83 	const wchar_t **p;
84 
85 	flag1 = flag2 = flag3 = 1;
86 	iflag = 0;
87 
88  	line1len = INITLINELEN;
89  	line2len = INITLINELEN;
90  	line1 = malloc(line1len * sizeof(*line1));
91  	line2 = malloc(line2len * sizeof(*line2));
92 	if (line1 == NULL || line2 == NULL)
93 		err(1, "malloc");
94 
95 	(void) setlocale(LC_ALL, "");
96 
97 	while ((ch = getopt(argc, argv, "123i")) != -1)
98 		switch(ch) {
99 		case '1':
100 			flag1 = 0;
101 			break;
102 		case '2':
103 			flag2 = 0;
104 			break;
105 		case '3':
106 			flag3 = 0;
107 			break;
108 		case 'i':
109 			iflag = 1;
110 			break;
111 		case '?':
112 		default:
113 			usage();
114 		}
115 	argc -= optind;
116 	argv += optind;
117 
118 	if (argc != 2)
119 		usage();
120 
121 	fp1 = file(argv[0]);
122 	fp2 = file(argv[1]);
123 
124 	/* for each column printed, add another tab offset */
125 	p = tabs;
126 	col1 = col2 = col3 = NULL;
127 	if (flag1)
128 		col1 = *p++;
129 	if (flag2)
130 		col2 = *p++;
131 	if (flag3)
132 		col3 = *p;
133 
134 	for (read1 = read2 = 1;;) {
135 		/* read next line, check for EOF */
136 		if (read1) {
137 			line1 = getline(line1, &line1len, fp1);
138 			if (line1 == NULL && ferror(fp1))
139 				err(1, "%s", argv[0]);
140 		}
141 		if (read2) {
142 			line2 = getline(line2, &line2len, fp2);
143 			if (line2 == NULL && ferror(fp2))
144 				err(1, "%s", argv[1]);
145 		}
146 
147 		/* if one file done, display the rest of the other file */
148 		if (line1 == NULL) {
149 			if (line2 != NULL && col2 != NULL)
150 				show(fp2, argv[1], col2, line2, &line2len);
151 			break;
152 		}
153 		if (line2 == NULL) {
154 			if (line1 != NULL && col1 != NULL)
155 				show(fp1, argv[0], col1, line1, &line1len);
156 			break;
157 		}
158 
159 		/* lines are the same */
160 		if(iflag)
161 			comp = wcsicoll(line1, line2);
162 		else
163 			comp = wcscoll(line1, line2);
164 
165 		if (!comp) {
166 			read1 = read2 = 1;
167 			if (col3 != NULL)
168 				(void)printf("%ls%ls\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("%ls%ls\n", col1, line1);
178 		} else {
179 			read1 = 0;
180 			read2 = 1;
181 			if (col2 != NULL)
182 				(void)printf("%ls%ls\n", col2, line2);
183 		}
184 	}
185 	exit(0);
186 }
187 
188 wchar_t *
189 getline(wchar_t *buf, size_t *buflen, FILE *fp)
190 {
191 	size_t bufpos;
192 	wint_t ch;
193 
194 	bufpos = 0;
195 	while ((ch = getwc(fp)) != WEOF && ch != '\n') {
196 		if (bufpos + 1 >= *buflen) {
197 			*buflen = *buflen * 2;
198 			if (*buflen > MAXLINELEN)
199 				errx(1,
200 				    "Maximum line buffer length (%zu) exceeded",
201 				    MAXLINELEN);
202 			buf = reallocf(buf, *buflen * sizeof(*buf));
203 			if (buf == NULL)
204 				err(1, "reallocf");
205 		}
206 		buf[bufpos++] = ch;
207 	}
208 	buf[bufpos] = '\0';
209 
210 	return (bufpos != 0 || ch == '\n' ? buf : NULL);
211 }
212 
213 void
214 show(FILE *fp, const char *fn, const wchar_t *offset, wchar_t *buf, size_t *buflen)
215 {
216 
217 	do {
218 		(void)printf("%ls%ls\n", offset, buf);
219 	} while ((buf = getline(buf, buflen, fp)) != NULL);
220 	if (ferror(fp))
221 		err(1, "%s", fn);
222 }
223 
224 FILE *
225 file(const char *name)
226 {
227 	FILE *fp;
228 
229 	if (!strcmp(name, "-"))
230 		return (stdin);
231 	if ((fp = fopen(name, "r")) == NULL) {
232 		err(1, "%s", name);
233 	}
234 	return (fp);
235 }
236 
237 static void
238 usage(void)
239 {
240 	(void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
241 	exit(1);
242 }
243 
244 static size_t wcsicoll_l1_buflen = 0, wcsicoll_l2_buflen = 0;
245 static wchar_t *wcsicoll_l1_buf = NULL, *wcsicoll_l2_buf = NULL;
246 
247 int
248 wcsicoll(const wchar_t *s1, const wchar_t *s2)
249 {
250 	wchar_t *p;
251 	size_t l1, l2;
252 	size_t new_l1_buflen, new_l2_buflen;
253 
254 	l1 = wcslen(s1) + 1;
255 	l2 = wcslen(s2) + 1;
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;
261 		else
262 			new_l1_buflen *= 2;
263 	}
264 	while (new_l2_buflen < l2) {
265 		if (new_l2_buflen == 0)
266 			new_l2_buflen = INITLINELEN;
267 		else
268 			new_l2_buflen *= 2;
269 	}
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)
273                 	err(1, "reallocf");
274 		wcsicoll_l1_buflen = new_l1_buflen;
275 	}
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)
279                 	err(1, "reallocf");
280 		wcsicoll_l2_buflen = new_l2_buflen;
281 	}
282 
283 	for (p = wcsicoll_l1_buf; *s1; s1++)
284 		*p++ = towlower(*s1);
285 	*p = '\0';
286 	for (p = wcsicoll_l2_buf; *s2; s2++)
287 		*p++ = towlower(*s2);
288 	*p = '\0';
289 
290 	return (wcscoll(wcsicoll_l1_buf, wcsicoll_l2_buf));
291 }
292