1 /* $OpenBSD: comm.c,v 1.10 2015/10/09 01:37:07 deraadt Exp $ */
2 /* $NetBSD: comm.c,v 1.10 1995/09/05 19:57:43 jtc Exp $ */
5 * Copyright (c) 1989, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
44 #define MAXLINELEN (LINE_MAX + 1)
46 char *tabs
[] = { "", "\t", "\t\t" };
48 FILE *file(const char *);
49 void show(FILE *, char *, char *);
53 main(int argc
, char *argv
[])
55 int comp
, file1done
, file2done
, read1
, read2
;
56 int ch
, flag1
, flag2
, flag3
;
58 char *col1
, *col2
, *col3
;
59 char **p
, line1
[MAXLINELEN
], line2
[MAXLINELEN
];
60 int (*compare
)(const char * ,const char *);
62 setlocale(LC_ALL
, "");
64 if (pledge("stdio rpath", NULL
) == -1)
67 flag1
= flag2
= flag3
= 1;
69 while ((ch
= getopt(argc
, argv
, "123f")) != -1)
96 /* for each column printed, add another tab offset */
98 col1
= col2
= col3
= NULL
;
106 for (read1
= read2
= 1;;) {
107 /* read next line, check for EOF */
109 file1done
= !fgets(line1
, MAXLINELEN
, fp1
);
111 file2done
= !fgets(line2
, MAXLINELEN
, fp2
);
113 /* if one file done, display the rest of the other file */
115 if (!file2done
&& col2
)
116 show(fp2
, col2
, line2
);
120 if (!file1done
&& col1
)
121 show(fp1
, col1
, line1
);
125 /* lines are the same */
126 if (!(comp
= compare(line1
, line2
))) {
129 if (printf("%s%s", col3
, line1
) < 0)
134 /* lines are different */
139 if (printf("%s%s", col1
, line1
) < 0)
145 if (printf("%s%s", col2
, line2
) < 0)
150 if (ferror (stdout
) || fclose (stdout
) == EOF
)
157 show(FILE *fp
, char *offset
, char *buf
)
159 while (printf("%s%s", offset
, buf
) >= 0 && fgets(buf
, MAXLINELEN
, fp
))
164 file(const char *name
)
168 if (!strcmp(name
, "-"))
170 if ((fp
= fopen(name
, "r")) == NULL
)
178 (void)fprintf(stderr
, "usage: comm [-123f] file1 file2\n");