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