Annotation of src/usr.bin/comm/comm.c, Revision 1.8
1.8 ! deraadt 1: /* $OpenBSD: comm.c,v 1.7 2003/06/10 22:20:45 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: comm.c,v 1.10 1995/09/05 19:57:43 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1989, 1993, 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Case Larsen.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
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.
1.6 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
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
33: * SUCH DAMAGE.
34: */
35:
36: #include <err.h>
37: #include <limits.h>
38: #include <locale.h>
39: #include <stdio.h>
40: #include <stdlib.h>
41: #include <string.h>
42: #include <unistd.h>
43:
44: #define MAXLINELEN (LINE_MAX + 1)
45:
46: char *tabs[] = { "", "\t", "\t\t" };
47:
1.5 millert 48: FILE *file(const char *);
49: void show(FILE *, char *, char *);
50: void usage(void);
1.1 deraadt 51:
52: int
1.7 deraadt 53: main(int argc, char *argv[])
1.1 deraadt 54: {
55: int comp, file1done, file2done, read1, read2;
56: int ch, flag1, flag2, flag3;
57: FILE *fp1, *fp2;
58: char *col1, *col2, *col3;
59: char **p, line1[MAXLINELEN], line2[MAXLINELEN];
1.5 millert 60: int (*compare)(const char * ,const char *);
1.1 deraadt 61:
62: setlocale(LC_ALL, "");
63:
64: flag1 = flag2 = flag3 = 1;
1.3 deraadt 65: compare = strcoll;
66: while ((ch = getopt(argc, argv, "123f")) != -1)
1.1 deraadt 67: switch(ch) {
68: case '1':
69: flag1 = 0;
70: break;
71: case '2':
72: flag2 = 0;
73: break;
74: case '3':
75: flag3 = 0;
76: break;
1.3 deraadt 77: case 'f':
78: compare = strcasecmp;
79: break;
1.1 deraadt 80: case '?':
81: default:
82: usage();
83: }
84: argc -= optind;
85: argv += optind;
86:
87: if (argc != 2)
88: usage();
89:
90: fp1 = file(argv[0]);
91: fp2 = file(argv[1]);
92:
93: /* for each column printed, add another tab offset */
94: p = tabs;
95: col1 = col2 = col3 = NULL;
96: if (flag1)
97: col1 = *p++;
98: if (flag2)
99: col2 = *p++;
100: if (flag3)
101: col3 = *p;
102:
103: for (read1 = read2 = 1;;) {
104: /* read next line, check for EOF */
105: if (read1)
106: file1done = !fgets(line1, MAXLINELEN, fp1);
107: if (read2)
108: file2done = !fgets(line2, MAXLINELEN, fp2);
109:
110: /* if one file done, display the rest of the other file */
111: if (file1done) {
112: if (!file2done && col2)
113: show(fp2, col2, line2);
114: break;
115: }
116: if (file2done) {
117: if (!file1done && col1)
118: show(fp1, col1, line1);
119: break;
120: }
121:
122: /* lines are the same */
1.3 deraadt 123: if (!(comp = compare(line1, line2))) {
1.1 deraadt 124: read1 = read2 = 1;
125: if (col3)
126: if (printf("%s%s", col3, line1) < 0)
127: break;
128: continue;
129: }
130:
131: /* lines are different */
132: if (comp < 0) {
133: read1 = 1;
134: read2 = 0;
135: if (col1)
136: if (printf("%s%s", col1, line1) < 0)
137: break;
138: } else {
139: read1 = 0;
140: read2 = 1;
141: if (col2)
142: if (printf("%s%s", col2, line2) < 0)
143: break;
144: }
145: }
146:
147: if (ferror (stdout) || fclose (stdout) == EOF)
148: err(1, "stdout");
149:
150: exit(0);
151: }
152:
153: void
1.7 deraadt 154: show(FILE *fp, char *offset, char *buf)
1.1 deraadt 155: {
156: while (printf("%s%s", offset, buf) >= 0 && fgets(buf, MAXLINELEN, fp))
157: ;
158: }
159:
160: FILE *
1.7 deraadt 161: file(const char *name)
1.1 deraadt 162: {
163: FILE *fp;
164:
165: if (!strcmp(name, "-"))
166: return (stdin);
167: if ((fp = fopen(name, "r")) == NULL)
168: err(1, "%s", name);
169: return (fp);
170: }
171:
172: void
1.7 deraadt 173: usage(void)
1.1 deraadt 174: {
1.4 aaron 175: (void)fprintf(stderr, "usage: comm [-123f] file1 file2\n");
1.1 deraadt 176: exit(1);
177: }