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
36 * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
37 * @(#)comm.c 8.4 (Berkeley) 5/4/95
38 * $FreeBSD: src/usr.bin/comm/comm.c,v 1.11.2.3 2002/07/11 00:46:37 tjr Exp $
39 * $DragonFly: src/usr.bin/comm/comm.c,v 1.3 2003/10/02 17:42:27 hmp Exp $
51 #define MAXLINELEN (LINE_MAX + 1)
53 const char *tabs
[] = { "", "\t", "\t\t" };
56 void show(FILE *, const char *, char *);
57 int stricoll(char *, char *);
58 static void usage(void);
61 main(int argc
, char **argv
)
63 int comp
, file1done
= 0, file2done
= 0, read1
, read2
;
64 int ch
, flag1
, flag2
, flag3
, iflag
;
66 const char *col1
, *col2
, *col3
;
67 char line1
[MAXLINELEN
], line2
[MAXLINELEN
];
70 flag1
= flag2
= flag3
= 1;
73 (void) setlocale(LC_ALL
, "");
75 while ((ch
= getopt(argc
, argv
, "123i")) != -1)
102 /* for each column printed, add another tab offset */
104 col1
= col2
= col3
= NULL
;
112 for (read1
= read2
= 1;;) {
113 /* read next line, check for EOF */
115 file1done
= !fgets(line1
, MAXLINELEN
, fp1
);
117 file2done
= !fgets(line2
, MAXLINELEN
, fp2
);
119 /* if one file done, display the rest of the other file */
121 if (!file2done
&& col2
)
122 show(fp2
, col2
, line2
);
126 if (!file1done
&& col1
)
127 show(fp1
, col1
, line1
);
131 /* lines are the same */
133 comp
= stricoll(line1
, line2
);
135 comp
= strcoll(line1
, line2
);
140 (void)printf("%s%s", col3
, line1
);
144 /* lines are different */
149 (void)printf("%s%s", col1
, line1
);
154 (void)printf("%s%s", col2
, line2
);
161 show(FILE *fp
, const char *offset
, char *buf
)
165 (void)printf("%s%s", offset
, buf
);
166 } while (fgets(buf
, MAXLINELEN
, fp
));
174 if (!strcmp(name
, "-"))
176 if ((fp
= fopen(name
, "r")) == NULL
) {
185 (void)fprintf(stderr
, "usage: comm [-123i] file1 file2\n");
190 stricoll(char *s1
, char *s2
)
192 char *p
, line1
[MAXLINELEN
], line2
[MAXLINELEN
];
194 for (p
= line1
; *s1
; s1
++)
195 *p
++ = tolower((unsigned char)*s1
);
197 for (p
= line2
; *s2
; s2
++)
198 *p
++ = tolower((unsigned char)*s2
);
200 return strcoll(line1
, line2
);