1 /* $NetBSD: comm.c,v 1.17 2009/04/11 12:18:45 lukem Exp $ */
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)comm.c 8.4 (Berkeley) 5/4/95";
45 __RCSID("$NetBSD: comm.c,v 1.17 2009/04/11 12:18:45 lukem Exp $");
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 *);
63 char *getnextln(char *buf
, FILE *);
66 main(int argc
, char **argv
)
68 int comp
, file1done
, file2done
, read1
, read2
;
69 int ch
, flag1
, flag2
, flag3
;
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;
80 while ((ch
= getopt(argc
, argv
, "123f")) != -1)
107 /* for each column printed, add another tab offset */
109 col1
= col2
= col3
= NULL
;
117 for (read1
= read2
= 1;;) {
118 /* read next line, check for EOF */
120 file1done
= !getnextln(line1
, fp1
);
122 file2done
= !getnextln(line2
, fp2
);
124 /* if one file done, display the rest of the other file */
126 if (!file2done
&& col2
)
127 show(fp2
, col2
, line2
);
131 if (!file1done
&& col1
)
132 show(fp1
, col1
, line1
);
136 /* lines are the same */
137 if (!(comp
= compare(line1
, line2
))) {
140 if (printf("%s%s\n", col3
, line1
) < 0)
145 /* lines are different */
150 if (printf("%s%s\n", col1
, line1
) < 0)
156 if (printf("%s%s\n", col2
, line2
) < 0)
161 if (ferror (stdout
) || fclose (stdout
) == EOF
)
168 show(FILE *fp
, const char *offset
, char *buf
)
170 while (printf("%s%s\n", offset
, buf
) >= 0 && getnextln(buf
, fp
))
175 file(const char *name
)
179 if (!strcmp(name
, "-"))
181 if ((fp
= fopen(name
, "r")) == NULL
)
190 (void)fprintf(stderr
, "usage: comm [-123f] file1 file2\n");
195 getnextln(char *buf
, FILE *fp
)
200 while ((c
= fgetc(fp
)) != '\n' && c
!= EOF
) {
204 i
--; /* consumes extra characters till newline */