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