xref: /freebsd/usr.bin/comm/comm.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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 <err.h>
52 #include <errno.h>
53 #include <limits.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 #define	MAXLINELEN	(LINE_MAX + 1)
60 
61 char *tabs[] = { "", "\t", "\t\t" };
62 
63 FILE   *file __P((char *));
64 void	show __P((FILE *, char *, char *));
65 static void	usage __P((void));
66 
67 int
68 main(argc, argv)
69 	int argc;
70 	char *argv[];
71 {
72 	int comp, file1done = 0, file2done = 0, read1, read2;
73 	int ch, flag1, flag2, flag3, iflag;
74 	FILE *fp1, *fp2;
75 	char *col1, *col2, *col3;
76 	char **p, line1[MAXLINELEN], line2[MAXLINELEN];
77 
78 	flag1 = flag2 = flag3 = 1;
79 	iflag = 0;
80 
81 	while ((ch = getopt(argc, argv, "-123i")) != -1)
82 		switch(ch) {
83 		case '-':
84 			--optind;
85 			goto done;
86 		case '1':
87 			flag1 = 0;
88 			break;
89 		case '2':
90 			flag2 = 0;
91 			break;
92 		case '3':
93 			flag3 = 0;
94 			break;
95 		case 'i':
96 			iflag = 1;
97 			break;
98 		case '?':
99 		default:
100 			usage();
101 		}
102 done:	argc -= optind;
103 	argv += optind;
104 
105 	if (argc != 2)
106 		usage();
107 
108 	fp1 = file(argv[0]);
109 	fp2 = file(argv[1]);
110 
111 	/* for each column printed, add another tab offset */
112 	p = tabs;
113 	col1 = col2 = col3 = NULL;
114 	if (flag1)
115 		col1 = *p++;
116 	if (flag2)
117 		col2 = *p++;
118 	if (flag3)
119 		col3 = *p;
120 
121 	for (read1 = read2 = 1;;) {
122 		/* read next line, check for EOF */
123 		if (read1)
124 			file1done = !fgets(line1, MAXLINELEN, fp1);
125 		if (read2)
126 			file2done = !fgets(line2, MAXLINELEN, fp2);
127 
128 		/* if one file done, display the rest of the other file */
129 		if (file1done) {
130 			if (!file2done && col2)
131 				show(fp2, col2, line2);
132 			break;
133 		}
134 		if (file2done) {
135 			if (!file1done && col1)
136 				show(fp1, col1, line1);
137 			break;
138 		}
139 
140 		/* lines are the same */
141 		if(iflag)
142 			comp = strcasecmp(line1, line2);
143 		else
144 			comp = strcmp(line1, line2);
145 
146 		if (!comp) {
147 			read1 = read2 = 1;
148 			if (col3)
149 				(void)printf("%s%s", col3, line1);
150 			continue;
151 		}
152 
153 		/* lines are different */
154 		if (comp < 0) {
155 			read1 = 1;
156 			read2 = 0;
157 			if (col1)
158 				(void)printf("%s%s", col1, line1);
159 		} else {
160 			read1 = 0;
161 			read2 = 1;
162 			if (col2)
163 				(void)printf("%s%s", col2, line2);
164 		}
165 	}
166 	exit(0);
167 }
168 
169 void
170 show(fp, offset, buf)
171 	FILE *fp;
172 	char *offset, *buf;
173 {
174 
175 	do {
176 		(void)printf("%s%s", offset, buf);
177 	} while (fgets(buf, MAXLINELEN, fp));
178 }
179 
180 FILE *
181 file(name)
182 	char *name;
183 {
184 	FILE *fp;
185 
186 	if (!strcmp(name, "-"))
187 		return (stdin);
188 	if ((fp = fopen(name, "r")) == NULL) {
189 		err(1, "%s", name);
190 	}
191 	return (fp);
192 }
193 
194 static void
195 usage()
196 {
197 	(void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
198 	exit(1);
199 }
200