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