1 /* $OpenBSD: comm.c,v 1.11 2022/12/04 23:50:47 cheloha 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <err.h>
37 #include <limits.h>
38 #include <locale.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #define MAXLINELEN (LINE_MAX + 1)
45
46 char *tabs[] = { "", "\t", "\t\t" };
47
48 FILE *file(const char *);
49 void show(FILE *, char *, char *);
50 void usage(void);
51
52 int
main(int argc,char * argv[])53 main(int argc, char *argv[])
54 {
55 int comp, file1done, file2done, read1, read2;
56 int ch, flag1, flag2, flag3;
57 FILE *fp1, *fp2;
58 char *col1, *col2, *col3;
59 char **p, line1[MAXLINELEN], line2[MAXLINELEN];
60 int (*compare)(const char * ,const char *);
61
62 setlocale(LC_ALL, "");
63
64 if (pledge("stdio rpath", NULL) == -1)
65 err(1, "pledge");
66
67 flag1 = flag2 = flag3 = 1;
68 compare = strcoll;
69 while ((ch = getopt(argc, argv, "123f")) != -1)
70 switch(ch) {
71 case '1':
72 flag1 = 0;
73 break;
74 case '2':
75 flag2 = 0;
76 break;
77 case '3':
78 flag3 = 0;
79 break;
80 case 'f':
81 compare = strcasecmp;
82 break;
83 default:
84 usage();
85 }
86 argc -= optind;
87 argv += optind;
88
89 if (argc != 2)
90 usage();
91
92 fp1 = file(argv[0]);
93 fp2 = file(argv[1]);
94
95 /* for each column printed, add another tab offset */
96 p = tabs;
97 col1 = col2 = col3 = NULL;
98 if (flag1)
99 col1 = *p++;
100 if (flag2)
101 col2 = *p++;
102 if (flag3)
103 col3 = *p;
104
105 for (read1 = read2 = 1;;) {
106 /* read next line, check for EOF */
107 if (read1)
108 file1done = !fgets(line1, MAXLINELEN, fp1);
109 if (read2)
110 file2done = !fgets(line2, MAXLINELEN, fp2);
111
112 /* if one file done, display the rest of the other file */
113 if (file1done) {
114 if (!file2done && col2)
115 show(fp2, col2, line2);
116 break;
117 }
118 if (file2done) {
119 if (!file1done && col1)
120 show(fp1, col1, line1);
121 break;
122 }
123
124 /* lines are the same */
125 if (!(comp = compare(line1, line2))) {
126 read1 = read2 = 1;
127 if (col3)
128 if (printf("%s%s", col3, line1) < 0)
129 break;
130 continue;
131 }
132
133 /* lines are different */
134 if (comp < 0) {
135 read1 = 1;
136 read2 = 0;
137 if (col1)
138 if (printf("%s%s", col1, line1) < 0)
139 break;
140 } else {
141 read1 = 0;
142 read2 = 1;
143 if (col2)
144 if (printf("%s%s", col2, line2) < 0)
145 break;
146 }
147 }
148
149 if (ferror (stdout) || fclose (stdout) == EOF)
150 err(1, "stdout");
151
152 exit(0);
153 }
154
155 void
show(FILE * fp,char * offset,char * buf)156 show(FILE *fp, char *offset, char *buf)
157 {
158 while (printf("%s%s", offset, buf) >= 0 && fgets(buf, MAXLINELEN, fp))
159 ;
160 }
161
162 FILE *
file(const char * name)163 file(const char *name)
164 {
165 FILE *fp;
166
167 if (!strcmp(name, "-"))
168 return (stdin);
169 if ((fp = fopen(name, "r")) == NULL)
170 err(1, "%s", name);
171 return (fp);
172 }
173
174 void
usage(void)175 usage(void)
176 {
177 (void)fprintf(stderr, "usage: comm [-123f] file1 file2\n");
178 exit(1);
179 }
180