xref: /freebsd/usr.bin/comm/comm.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
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 <errno.h>
54 #include <limits.h>
55 #include <locale.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 
61 #define	MAXLINELEN	(LINE_MAX + 1)
62 
63 char *tabs[] = { "", "\t", "\t\t" };
64 
65 FILE   *file __P((char *));
66 void	show __P((FILE *, char *, char *));
67 int     stricoll __P((char *, char *));
68 static void	usage __P((void));
69 
70 int
71 main(argc, argv)
72 	int argc;
73 	char *argv[];
74 {
75 	int comp, file1done = 0, file2done = 0, read1, read2;
76 	int ch, flag1, flag2, flag3, iflag;
77 	FILE *fp1, *fp2;
78 	char *col1, *col2, *col3;
79 	char **p, line1[MAXLINELEN], line2[MAXLINELEN];
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 	char *offset, *buf;
178 {
179 
180 	do {
181 		(void)printf("%s%s", offset, buf);
182 	} while (fgets(buf, MAXLINELEN, fp));
183 }
184 
185 FILE *
186 file(name)
187 	char *name;
188 {
189 	FILE *fp;
190 
191 	if (!strcmp(name, "-"))
192 		return (stdin);
193 	if ((fp = fopen(name, "r")) == NULL) {
194 		err(1, "%s", name);
195 	}
196 	return (fp);
197 }
198 
199 static void
200 usage()
201 {
202 	(void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
203 	exit(1);
204 }
205 
206 int
207 stricoll(s1, s2)
208 	char *s1, *s2;
209 {
210 	char *p, line1[MAXLINELEN], line2[MAXLINELEN];
211 
212 	for (p = line1; *s1; s1++)
213 		*p++ = tolower((unsigned char)*s1);
214 	*p = '\0';
215 	for (p = line2; *s2; s2++)
216 		*p++ = tolower((unsigned char)*s2);
217 	*p = '\0';
218 	return strcoll(line1, line2);
219 }
220