xref: /freebsd/usr.bin/comm/comm.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993, 1994\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif
38 
39 #if 0
40 #ifndef lint
41 static char sccsid[] = "From: @(#)comm.c	8.4 (Berkeley) 5/4/95";
42 #endif
43 #endif
44 
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <err.h>
49 #include <limits.h>
50 #include <locale.h>
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <wchar.h>
57 #include <wctype.h>
58 
59 static int iflag;
60 static const char *tabs[] = { "", "\t", "\t\t" };
61 
62 static FILE	*file(const char *);
63 static wchar_t	*convert(const char *);
64 static void	show(FILE *, const char *, const char *, char **, size_t *);
65 static void	usage(void);
66 
67 int
68 main(int argc, char *argv[])
69 {
70 	int comp, read1, read2;
71 	int ch, flag1, flag2, flag3;
72 	FILE *fp1, *fp2;
73 	const char *col1, *col2, *col3;
74 	size_t line1len, line2len;
75 	char *line1, *line2;
76 	ssize_t n1, n2;
77 	wchar_t *tline1, *tline2;
78 	const char **p;
79 
80 	(void) setlocale(LC_ALL, "");
81 
82 	flag1 = flag2 = flag3 = 1;
83 
84 	while ((ch = getopt(argc, argv, "123i")) != -1)
85 		switch(ch) {
86 		case '1':
87 			flag1 = 0;
88 			break;
89 		case '2':
90 			flag2 = 0;
91 			break;
92 		case '3':
93 			flag3 = 0;
94 			break;
95 		case 'i':
96 			iflag = 1;
97 			break;
98 		case '?':
99 		default:
100 			usage();
101 		}
102 	argc -= optind;
103 	argv += optind;
104 
105 	if (argc != 2)
106 		usage();
107 
108 	fp1 = file(argv[0]);
109 	fp2 = file(argv[1]);
110 
111 	/* for each column printed, add another tab offset */
112 	p = tabs;
113 	col1 = col2 = col3 = NULL;
114 	if (flag1)
115 		col1 = *p++;
116 	if (flag2)
117 		col2 = *p++;
118 	if (flag3)
119 		col3 = *p;
120 
121 	line1len = line2len = 0;
122 	line1 = line2 = NULL;
123 	n1 = n2 = -1;
124 
125 	for (read1 = read2 = 1;;) {
126 		/* read next line, check for EOF */
127 		if (read1) {
128 			n1 = getline(&line1, &line1len, fp1);
129 			if (n1 < 0 && ferror(fp1))
130 				err(1, "%s", argv[0]);
131 			if (n1 > 0 && line1[n1 - 1] == '\n')
132 				line1[n1 - 1] = '\0';
133 
134 		}
135 		if (read2) {
136 			n2 = getline(&line2, &line2len, fp2);
137 			if (n2 < 0 && ferror(fp2))
138 				err(1, "%s", argv[1]);
139 			if (n2 > 0 && line2[n2 - 1] == '\n')
140 				line2[n2 - 1] = '\0';
141 		}
142 
143 		/* if one file done, display the rest of the other file */
144 		if (n1 < 0) {
145 			if (n2 >= 0 && col2 != NULL)
146 				show(fp2, argv[1], col2, &line2, &line2len);
147 			break;
148 		}
149 		if (n2 < 0) {
150 			if (n1 >= 0 && col1 != NULL)
151 				show(fp1, argv[0], col1, &line1, &line1len);
152 			break;
153 		}
154 
155 		tline2 = NULL;
156 		if ((tline1 = convert(line1)) != NULL)
157 			tline2 = convert(line2);
158 		if (tline1 == NULL || tline2 == NULL)
159 			comp = strcmp(line1, line2);
160 		else
161 			comp = wcscoll(tline1, tline2);
162 		if (tline1 != NULL)
163 			free(tline1);
164 		if (tline2 != NULL)
165 			free(tline2);
166 
167 		/* lines are the same */
168 		if (!comp) {
169 			read1 = read2 = 1;
170 			if (col3 != NULL)
171 				(void)printf("%s%s\n", col3, line1);
172 			continue;
173 		}
174 
175 		/* lines are different */
176 		if (comp < 0) {
177 			read1 = 1;
178 			read2 = 0;
179 			if (col1 != NULL)
180 				(void)printf("%s%s\n", col1, line1);
181 		} else {
182 			read1 = 0;
183 			read2 = 1;
184 			if (col2 != NULL)
185 				(void)printf("%s%s\n", col2, line2);
186 		}
187 	}
188 	exit(0);
189 }
190 
191 static wchar_t *
192 convert(const char *str)
193 {
194 	size_t n;
195 	wchar_t *buf, *p;
196 
197 	if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
198 		return (NULL);
199 	if (SIZE_MAX / sizeof(*buf) < n + 1)
200 		errx(1, "conversion buffer length overflow");
201 	if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
202 		err(1, "malloc");
203 	if (mbstowcs(buf, str, n + 1) != n)
204 		errx(1, "internal mbstowcs() error");
205 
206 	if (iflag) {
207 		for (p = buf; *p != L'\0'; p++)
208 			*p = towlower(*p);
209 	}
210 
211 	return (buf);
212 }
213 
214 static void
215 show(FILE *fp, const char *fn, const char *offset, char **bufp, size_t *buflenp)
216 {
217 	ssize_t n;
218 
219 	do {
220 		(void)printf("%s%s\n", offset, *bufp);
221 		if ((n = getline(bufp, buflenp, fp)) < 0)
222 			break;
223 		if (n > 0 && (*bufp)[n - 1] == '\n')
224 			(*bufp)[n - 1] = '\0';
225 	} while (1);
226 	if (ferror(fp))
227 		err(1, "%s", fn);
228 }
229 
230 static FILE *
231 file(const char *name)
232 {
233 	FILE *fp;
234 
235 	if (!strcmp(name, "-"))
236 		return (stdin);
237 	if ((fp = fopen(name, "r")) == NULL) {
238 		err(1, "%s", name);
239 	}
240 	return (fp);
241 }
242 
243 static void
244 usage(void)
245 {
246 	(void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
247 	exit(1);
248 }
249