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