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