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