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