xref: /freebsd/usr.bin/comm/comm.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 #ifndef lint
43 static char sccsid[] = "From: @(#)comm.c	8.4 (Berkeley) 5/4/95";
44 #endif
45 #endif
46 
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
49 
50 #include <err.h>
51 #include <limits.h>
52 #include <locale.h>
53 #include <stdint.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <wchar.h>
59 #include <wctype.h>
60 
61 static int iflag;
62 static const char *tabs[] = { "", "\t", "\t\t" };
63 
64 static FILE	*file(const char *);
65 static wchar_t	*convert(const char *);
66 static void	show(FILE *, const char *, const char *, char **, size_t *);
67 static void	usage(void);
68 
69 int
70 main(int argc, char *argv[])
71 {
72 	int comp, read1, read2;
73 	int ch, flag1, flag2, flag3;
74 	FILE *fp1, *fp2;
75 	const char *col1, *col2, *col3;
76 	size_t line1len, line2len;
77 	char *line1, *line2;
78 	ssize_t n1, n2;
79 	wchar_t *tline1, *tline2;
80 	const char **p;
81 
82 	(void) setlocale(LC_ALL, "");
83 
84 	flag1 = flag2 = flag3 = 1;
85 
86 	while ((ch = getopt(argc, argv, "123i")) != -1)
87 		switch(ch) {
88 		case '1':
89 			flag1 = 0;
90 			break;
91 		case '2':
92 			flag2 = 0;
93 			break;
94 		case '3':
95 			flag3 = 0;
96 			break;
97 		case 'i':
98 			iflag = 1;
99 			break;
100 		case '?':
101 		default:
102 			usage();
103 		}
104 	argc -= optind;
105 	argv += optind;
106 
107 	if (argc != 2)
108 		usage();
109 
110 	fp1 = file(argv[0]);
111 	fp2 = file(argv[1]);
112 
113 	/* for each column printed, add another tab offset */
114 	p = tabs;
115 	col1 = col2 = col3 = NULL;
116 	if (flag1)
117 		col1 = *p++;
118 	if (flag2)
119 		col2 = *p++;
120 	if (flag3)
121 		col3 = *p;
122 
123 	line1len = line2len = 0;
124 	line1 = line2 = NULL;
125 	n1 = n2 = -1;
126 
127 	for (read1 = read2 = 1;;) {
128 		/* read next line, check for EOF */
129 		if (read1) {
130 			n1 = getline(&line1, &line1len, fp1);
131 			if (n1 < 0 && ferror(fp1))
132 				err(1, "%s", argv[0]);
133 			if (n1 > 0 && line1[n1 - 1] == '\n')
134 				line1[n1 - 1] = '\0';
135 
136 		}
137 		if (read2) {
138 			n2 = getline(&line2, &line2len, fp2);
139 			if (n2 < 0 && ferror(fp2))
140 				err(1, "%s", argv[1]);
141 			if (n2 > 0 && line2[n2 - 1] == '\n')
142 				line2[n2 - 1] = '\0';
143 		}
144 
145 		/* if one file done, display the rest of the other file */
146 		if (n1 < 0) {
147 			if (n2 >= 0 && col2 != NULL)
148 				show(fp2, argv[1], col2, &line2, &line2len);
149 			break;
150 		}
151 		if (n2 < 0) {
152 			if (n1 >= 0 && col1 != NULL)
153 				show(fp1, argv[0], col1, &line1, &line1len);
154 			break;
155 		}
156 
157 		tline2 = NULL;
158 		if ((tline1 = convert(line1)) != NULL)
159 			tline2 = convert(line2);
160 		if (tline1 == NULL || tline2 == NULL)
161 			comp = strcmp(line1, line2);
162 		else
163 			comp = wcscoll(tline1, tline2);
164 		if (tline1 != NULL)
165 			free(tline1);
166 		if (tline2 != NULL)
167 			free(tline2);
168 
169 		/* lines are the same */
170 		if (!comp) {
171 			read1 = read2 = 1;
172 			if (col3 != NULL)
173 				(void)printf("%s%s\n", col3, line1);
174 			continue;
175 		}
176 
177 		/* lines are different */
178 		if (comp < 0) {
179 			read1 = 1;
180 			read2 = 0;
181 			if (col1 != NULL)
182 				(void)printf("%s%s\n", col1, line1);
183 		} else {
184 			read1 = 0;
185 			read2 = 1;
186 			if (col2 != NULL)
187 				(void)printf("%s%s\n", col2, line2);
188 		}
189 	}
190 	exit(0);
191 }
192 
193 static wchar_t *
194 convert(const char *str)
195 {
196 	size_t n;
197 	wchar_t *buf, *p;
198 
199 	if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
200 		return (NULL);
201 	if (SIZE_MAX / sizeof(*buf) < n + 1)
202 		errx(1, "conversion buffer length overflow");
203 	if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
204 		err(1, "malloc");
205 	if (mbstowcs(buf, str, n + 1) != n)
206 		errx(1, "internal mbstowcs() error");
207 
208 	if (iflag) {
209 		for (p = buf; *p != L'\0'; p++)
210 			*p = towlower(*p);
211 	}
212 
213 	return (buf);
214 }
215 
216 static void
217 show(FILE *fp, const char *fn, const char *offset, char **bufp, size_t *buflenp)
218 {
219 	ssize_t n;
220 
221 	do {
222 		(void)printf("%s%s\n", offset, *bufp);
223 		if ((n = getline(bufp, buflenp, fp)) < 0)
224 			break;
225 		if (n > 0 && (*bufp)[n - 1] == '\n')
226 			(*bufp)[n - 1] = '\0';
227 	} while (1);
228 	if (ferror(fp))
229 		err(1, "%s", fn);
230 }
231 
232 static FILE *
233 file(const char *name)
234 {
235 	FILE *fp;
236 
237 	if (!strcmp(name, "-"))
238 		return (stdin);
239 	if ((fp = fopen(name, "r")) == NULL) {
240 		err(1, "%s", name);
241 	}
242 	return (fp);
243 }
244 
245 static void
246 usage(void)
247 {
248 	(void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
249 	exit(1);
250 }
251