No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / comm / comm.c
blobd25616607a681973ae96eee38d090b98ca974f1c
1 /* $NetBSD: comm.c,v 1.17 2009/04/11 12:18:45 lukem Exp $ */
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Case Larsen.
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.
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.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
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.17 2009/04/11 12:18:45 lukem Exp $");
46 #endif /* not lint */
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>
56 #define MAXLINELEN (LINE_MAX + 1)
58 const char *tabs[] = { "", "\t", "\t\t" };
60 FILE *file(const char *);
61 void show(FILE *, const char *, char *);
62 void usage(void);
63 char *getnextln(char *buf, FILE *);
65 int
66 main(int argc, char **argv)
68 int comp, file1done, file2done, read1, read2;
69 int ch, flag1, flag2, flag3;
70 FILE *fp1, *fp2;
71 const char *col1, *col2, *col3, **p;
72 char line1[MAXLINELEN], line2[MAXLINELEN];
73 int (*compare)(const char*,const char*);
75 (void)setlocale(LC_ALL, "");
77 file1done = file2done = 0;
78 flag1 = flag2 = flag3 = 1;
79 compare = strcoll;
80 while ((ch = getopt(argc, argv, "123f")) != -1)
81 switch(ch) {
82 case '1':
83 flag1 = 0;
84 break;
85 case '2':
86 flag2 = 0;
87 break;
88 case '3':
89 flag3 = 0;
90 break;
91 case 'f':
92 compare = strcasecmp;
93 break;
94 case '?':
95 default:
96 usage();
98 argc -= optind;
99 argv += optind;
101 if (argc != 2)
102 usage();
104 fp1 = file(argv[0]);
105 fp2 = file(argv[1]);
107 /* for each column printed, add another tab offset */
108 p = tabs;
109 col1 = col2 = col3 = NULL;
110 if (flag1)
111 col1 = *p++;
112 if (flag2)
113 col2 = *p++;
114 if (flag3)
115 col3 = *p;
117 for (read1 = read2 = 1;;) {
118 /* read next line, check for EOF */
119 if (read1)
120 file1done = !getnextln(line1, fp1);
121 if (read2)
122 file2done = !getnextln(line2, fp2);
124 /* if one file done, display the rest of the other file */
125 if (file1done) {
126 if (!file2done && col2)
127 show(fp2, col2, line2);
128 break;
130 if (file2done) {
131 if (!file1done && col1)
132 show(fp1, col1, line1);
133 break;
136 /* lines are the same */
137 if (!(comp = compare(line1, line2))) {
138 read1 = read2 = 1;
139 if (col3)
140 if (printf("%s%s\n", col3, line1) < 0)
141 break;
142 continue;
145 /* lines are different */
146 if (comp < 0) {
147 read1 = 1;
148 read2 = 0;
149 if (col1)
150 if (printf("%s%s\n", col1, line1) < 0)
151 break;
152 } else {
153 read1 = 0;
154 read2 = 1;
155 if (col2)
156 if (printf("%s%s\n", col2, line2) < 0)
157 break;
161 if (ferror (stdout) || fclose (stdout) == EOF)
162 err(1, "stdout");
164 exit(0);
167 void
168 show(FILE *fp, const char *offset, char *buf)
170 while (printf("%s%s\n", offset, buf) >= 0 && getnextln(buf, fp))
174 FILE *
175 file(const char *name)
177 FILE *fp;
179 if (!strcmp(name, "-"))
180 return (stdin);
181 if ((fp = fopen(name, "r")) == NULL)
182 err(1, "%s", name);
183 return (fp);
186 void
187 usage(void)
190 (void)fprintf(stderr, "usage: comm [-123f] file1 file2\n");
191 exit(1);
194 char *
195 getnextln(char *buf, FILE *fp)
197 size_t i = 0;
198 int c;
200 while ((c = fgetc(fp)) != '\n' && c != EOF) {
201 buf[i++] = c;
203 if (i >= MAXLINELEN)
204 i--; /* consumes extra characters till newline */
207 if (c == EOF && !i)
208 return NULL;
210 buf[i] = 0;
211 return buf;