xref: /openbsd-src/usr.bin/comm/comm.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: comm.c,v 1.4 2000/11/21 18:06:45 aaron Exp $	*/
2 /*	$NetBSD: comm.c,v 1.10 1995/09/05 19:57:43 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Case Larsen.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #ifndef lint
41 static char copyright[] =
42 "@(#) Copyright (c) 1989, 1993, 1994\n\
43 	The Regents of the University of California.  All rights reserved.\n";
44 #endif /* not lint */
45 
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)comm.c	8.4 (Berkeley) 5/4/95";
49 #endif
50 static char rcsid[] = "$OpenBSD: comm.c,v 1.4 2000/11/21 18:06:45 aaron Exp $";
51 #endif /* not lint */
52 
53 #include <err.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((const char *));
66 void	show __P((FILE *, char *, char *));
67 void	usage __P((void));
68 
69 int
70 main(argc, argv)
71 	int argc;
72 	char **argv;
73 {
74 	int comp, file1done, file2done, read1, read2;
75 	int ch, flag1, flag2, flag3;
76 	FILE *fp1, *fp2;
77 	char *col1, *col2, *col3;
78 	char **p, line1[MAXLINELEN], line2[MAXLINELEN];
79 	int (*compare) __P((const char * ,const char *));
80 
81 	setlocale(LC_ALL, "");
82 
83 	flag1 = flag2 = flag3 = 1;
84 	compare = strcoll;
85 	while ((ch = getopt(argc, argv, "123f")) != -1)
86 		switch(ch) {
87 		case '1':
88 			flag1 = 0;
89 			break;
90 		case '2':
91 			flag2 = 0;
92 			break;
93 		case '3':
94 			flag3 = 0;
95 			break;
96 		case 'f':
97 			compare = strcasecmp;
98 			break;
99 		case '?':
100 		default:
101 			usage();
102 		}
103 	argc -= optind;
104 	argv += optind;
105 
106 	if (argc != 2)
107 		usage();
108 
109 	fp1 = file(argv[0]);
110 	fp2 = file(argv[1]);
111 
112 	/* for each column printed, add another tab offset */
113 	p = tabs;
114 	col1 = col2 = col3 = NULL;
115 	if (flag1)
116 		col1 = *p++;
117 	if (flag2)
118 		col2 = *p++;
119 	if (flag3)
120 		col3 = *p;
121 
122 	for (read1 = read2 = 1;;) {
123 		/* read next line, check for EOF */
124 		if (read1)
125 			file1done = !fgets(line1, MAXLINELEN, fp1);
126 		if (read2)
127 			file2done = !fgets(line2, MAXLINELEN, fp2);
128 
129 		/* if one file done, display the rest of the other file */
130 		if (file1done) {
131 			if (!file2done && col2)
132 				show(fp2, col2, line2);
133 			break;
134 		}
135 		if (file2done) {
136 			if (!file1done && col1)
137 				show(fp1, col1, line1);
138 			break;
139 		}
140 
141 		/* lines are the same */
142 		if (!(comp = compare(line1, line2))) {
143 			read1 = read2 = 1;
144 			if (col3)
145 				if (printf("%s%s", col3, line1) < 0)
146 					break;
147 			continue;
148 		}
149 
150 		/* lines are different */
151 		if (comp < 0) {
152 			read1 = 1;
153 			read2 = 0;
154 			if (col1)
155 				if (printf("%s%s", col1, line1) < 0)
156 					break;
157 		} else {
158 			read1 = 0;
159 			read2 = 1;
160 			if (col2)
161 				if (printf("%s%s", col2, line2) < 0)
162 					break;
163 		}
164 	}
165 
166 	if (ferror (stdout) || fclose (stdout) == EOF)
167 		err(1, "stdout");
168 
169 	exit(0);
170 }
171 
172 void
173 show(fp, offset, buf)
174 	FILE *fp;
175 	char *offset, *buf;
176 {
177 	while (printf("%s%s", offset, buf) >= 0 && fgets(buf, MAXLINELEN, fp))
178 		;
179 }
180 
181 FILE *
182 file(name)
183 	const char *name;
184 {
185 	FILE *fp;
186 
187 	if (!strcmp(name, "-"))
188 		return (stdin);
189 	if ((fp = fopen(name, "r")) == NULL)
190 		err(1, "%s", name);
191 	return (fp);
192 }
193 
194 void
195 usage()
196 {
197 	(void)fprintf(stderr, "usage: comm [-123f] file1 file2\n");
198 	exit(1);
199 }
200