xref: /freebsd/usr.bin/comm/comm.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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 && col2 != NULL)
133 				show(fp2, argv[1], col2, &line2, &line2len);
134 			break;
135 		}
136 		if (n2 < 0) {
137 			if (n1 >= 0 && col1 != NULL)
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 	exit(0);
176 }
177 
178 static wchar_t *
179 convert(const char *str)
180 {
181 	size_t n;
182 	wchar_t *buf, *p;
183 
184 	if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
185 		return (NULL);
186 	if (SIZE_MAX / sizeof(*buf) < n + 1)
187 		errx(1, "conversion buffer length overflow");
188 	if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
189 		err(1, "malloc");
190 	if (mbstowcs(buf, str, n + 1) != n)
191 		errx(1, "internal mbstowcs() error");
192 
193 	if (iflag) {
194 		for (p = buf; *p != L'\0'; p++)
195 			*p = towlower(*p);
196 	}
197 
198 	return (buf);
199 }
200 
201 static void
202 show(FILE *fp, const char *fn, const char *offset, char **bufp, size_t *buflenp)
203 {
204 	ssize_t n;
205 
206 	do {
207 		(void)printf("%s%s\n", offset, *bufp);
208 		if ((n = getline(bufp, buflenp, fp)) < 0)
209 			break;
210 		if (n > 0 && (*bufp)[n - 1] == '\n')
211 			(*bufp)[n - 1] = '\0';
212 	} while (1);
213 	if (ferror(fp))
214 		err(1, "%s", fn);
215 }
216 
217 static FILE *
218 file(const char *name)
219 {
220 	FILE *fp;
221 
222 	if (!strcmp(name, "-"))
223 		return (stdin);
224 	if ((fp = fopen(name, "r")) == NULL) {
225 		err(1, "%s", name);
226 	}
227 	return (fp);
228 }
229 
230 static void
231 usage(void)
232 {
233 	(void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
234 	exit(1);
235 }
236