2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static const char copyright
[] = "@(#) Copyright (c) 1989, 1993, 1994\n The Regents of the University of California. All rights reserved.\n";
43 static char sccsid
[] = "From: @(#)comm.c 8.4 (Berkeley) 5/4/95";
48 //__FBSDID("$FreeBSD: /repoman/r/ncvs/src/usr.bin/comm/comm.c,v 1.20 2002/07/28 15:28:38 dwmalone Exp $");
58 #define LINE_MAX 99999
59 #define MAXLINELEN (LINE_MAX + 1)
61 const char *tabs
[] = { "", "\t", "\t\t" };
63 FILE *file(const char *);
64 void show(FILE *, const char *, char *);
65 int stricoll(const char *, const char *);
66 static void usage(void);
67 static void printversion(void);
70 main(int argc
, char *argv
[])
72 int comp
, file1done
= 0, file2done
= 0, read1
, read2
;
73 int ch
, flag1
, flag2
, flag3
, iflag
;
75 const char *col1
, *col2
, *col3
;
76 char line1
[MAXLINELEN
], line2
[MAXLINELEN
];
79 flag1
= flag2
= flag3
= 1;
82 (void) setlocale(LC_ALL
, "");
84 while ((ch
= getopt(argc
, argv
, "123ivh")) != -1)
114 /* for each column printed, add another tab offset */
116 col1
= col2
= col3
= NULL
;
124 for (read1
= read2
= 1;;) {
125 /* read next line, check for EOF */
127 file1done
= !fgets(line1
, MAXLINELEN
, fp1
);
129 file2done
= !fgets(line2
, MAXLINELEN
, fp2
);
131 /* if one file done, display the rest of the other file */
133 if (!file2done
&& col2
)
134 show(fp2
, col2
, line2
);
138 if (!file1done
&& col1
)
139 show(fp1
, col1
, line1
);
143 /* lines are the same */
145 comp
= stricoll(line1
, line2
);
147 comp
= strcoll(line1
, line2
);
152 (void)printf("%s%s", col3
, line1
);
156 /* lines are different */
161 (void)printf("%s%s", col1
, line1
);
166 (void)printf("%s%s", col2
, line2
);
173 show(FILE *fp
, const char *offset
, char *buf
)
177 (void)printf("%s%s", offset
, buf
);
178 } while (fgets(buf
, MAXLINELEN
, fp
));
182 file(const char *name
)
186 if (!strcmp(name
, "-"))
188 if ((fp
= fopen(name
, "r")) == NULL
) {
197 (void)fprintf(stderr
, "Usage: /bin/comm [OPTION]... LEFT_FILE RIGHT_FILE
198 Compare sorted files LEFT_FILE and RIGHT_FILE line by line.
200 -1 suppress lines unique to left file
201 -2 suppress lines unique to right file
202 -3 suppress lines unique to both files
203 -help display this help and exit
204 -version output version information and exit
212 (void)fprintf(stderr
, copyright
);
213 (void)fprintf(stderr
, "\nVersion : %s\n", sccsid
);
218 stricoll(const char *s1
, const char *s2
)
220 char *p
, line1
[MAXLINELEN
], line2
[MAXLINELEN
];
222 for (p
= line1
; *s1
; s1
++)
223 *p
++ = tolower((unsigned char)*s1
);
225 for (p
= line2
; *s2
; s2
++)
226 *p
++ = tolower((unsigned char)*s2
);
228 return strcoll(line1
, line2
);