xref: /freebsd/usr.bin/comm/comm.c (revision 54ab3ed82b639b593fb0fe6db845e38a9d18970e)
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 #include <sys/cdefs.h>
38 
39 __FBSDID("$FreeBSD$");
40 
41 #ifndef lint
42 static const char copyright[] =
43 "@(#) Copyright (c) 1989, 1993, 1994\n\
44 	The Regents of the University of California.  All rights reserved.\n";
45 #endif
46 
47 #ifndef lint
48 static const char sccsid[] = "From: @(#)comm.c	8.4 (Berkeley) 5/4/95";
49 #endif
50 
51 #include <ctype.h>
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 
60 #define	MAXLINELEN	(LINE_MAX + 1)
61 
62 const char *tabs[] = { "", "\t", "\t\t" };
63 
64 FILE   *file __P((char *));
65 void	show __P((FILE *, const char *, char *));
66 int     stricoll __P((char *, char *));
67 static void	usage __P((void));
68 
69 int
70 main(argc, argv)
71 	int argc;
72 	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 char *col1, *col2, *col3;
78 	char line1[MAXLINELEN], line2[MAXLINELEN];
79 	const char **p;
80 
81 	flag1 = flag2 = flag3 = 1;
82 	iflag = 0;
83 
84 	(void) setlocale(LC_CTYPE, "");
85 
86 	while ((ch = getopt(argc, argv, "-123i")) != -1)
87 		switch(ch) {
88 		case '-':
89 			--optind;
90 			goto done;
91 		case '1':
92 			flag1 = 0;
93 			break;
94 		case '2':
95 			flag2 = 0;
96 			break;
97 		case '3':
98 			flag3 = 0;
99 			break;
100 		case 'i':
101 			iflag = 1;
102 			break;
103 		case '?':
104 		default:
105 			usage();
106 		}
107 done:	argc -= optind;
108 	argv += optind;
109 
110 	if (argc != 2)
111 		usage();
112 
113 	fp1 = file(argv[0]);
114 	fp2 = file(argv[1]);
115 
116 	/* for each column printed, add another tab offset */
117 	p = tabs;
118 	col1 = col2 = col3 = NULL;
119 	if (flag1)
120 		col1 = *p++;
121 	if (flag2)
122 		col2 = *p++;
123 	if (flag3)
124 		col3 = *p;
125 
126 	for (read1 = read2 = 1;;) {
127 		/* read next line, check for EOF */
128 		if (read1)
129 			file1done = !fgets(line1, MAXLINELEN, fp1);
130 		if (read2)
131 			file2done = !fgets(line2, MAXLINELEN, fp2);
132 
133 		/* if one file done, display the rest of the other file */
134 		if (file1done) {
135 			if (!file2done && col2)
136 				show(fp2, col2, line2);
137 			break;
138 		}
139 		if (file2done) {
140 			if (!file1done && col1)
141 				show(fp1, col1, line1);
142 			break;
143 		}
144 
145 		/* lines are the same */
146 		if(iflag)
147 			comp = stricoll(line1, line2);
148 		else
149 			comp = strcoll(line1, line2);
150 
151 		if (!comp) {
152 			read1 = read2 = 1;
153 			if (col3)
154 				(void)printf("%s%s", col3, line1);
155 			continue;
156 		}
157 
158 		/* lines are different */
159 		if (comp < 0) {
160 			read1 = 1;
161 			read2 = 0;
162 			if (col1)
163 				(void)printf("%s%s", col1, line1);
164 		} else {
165 			read1 = 0;
166 			read2 = 1;
167 			if (col2)
168 				(void)printf("%s%s", col2, line2);
169 		}
170 	}
171 	exit(0);
172 }
173 
174 void
175 show(fp, offset, buf)
176 	FILE *fp;
177 	const char *offset;
178 	char *buf;
179 {
180 
181 	do {
182 		(void)printf("%s%s", offset, buf);
183 	} while (fgets(buf, MAXLINELEN, fp));
184 }
185 
186 FILE *
187 file(name)
188 	char *name;
189 {
190 	FILE *fp;
191 
192 	if (!strcmp(name, "-"))
193 		return (stdin);
194 	if ((fp = fopen(name, "r")) == NULL) {
195 		err(1, "%s", name);
196 	}
197 	return (fp);
198 }
199 
200 static void
201 usage()
202 {
203 	(void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
204 	exit(1);
205 }
206 
207 int
208 stricoll(s1, s2)
209 	char *s1, *s2;
210 {
211 	char *p, line1[MAXLINELEN], line2[MAXLINELEN];
212 
213 	for (p = line1; *s1; s1++)
214 		*p++ = tolower((unsigned char)*s1);
215 	*p = '\0';
216 	for (p = line2; *s2; s2++)
217 		*p++ = tolower((unsigned char)*s2);
218 	*p = '\0';
219 	return strcoll(line1, line2);
220 }
221