Commit | Line | Data |
---|---|---|
edb4a1e3 KB |
1 | /* |
2 | * Copyright (c) 1989 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Case Larsen. | |
7 | * | |
f15db449 | 8 | * %sccs.include.redist.c% |
edb4a1e3 KB |
9 | */ |
10 | ||
11 | #ifndef lint | |
12 | char copyright[] = | |
13 | "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ | |
14 | All rights reserved.\n"; | |
15 | #endif /* not lint */ | |
16 | ||
17 | #ifndef lint | |
bd1956e9 | 18 | static char sccsid[] = "@(#)comm.c 5.8 (Berkeley) %G%"; |
edb4a1e3 KB |
19 | #endif /* not lint */ |
20 | ||
bd1956e9 | 21 | #include <fcntl.h> |
eabb7d53 | 22 | #include <limits.h> |
bd1956e9 | 23 | #include <errno.h> |
6fe4541f | 24 | #include <stdio.h> |
bd1956e9 KB |
25 | #include <stdlib.h> |
26 | #include <string.h> | |
6fe4541f | 27 | |
7b82efbd | 28 | #define MAXLINELEN (_POSIX2_LINE_MAX + 1) |
edb4a1e3 | 29 | |
eabb7d53 | 30 | char *tabs[] = { "", "\t", "\t\t" }; |
6fe4541f | 31 | |
6fe4541f | 32 | main(argc,argv) |
edb4a1e3 | 33 | int argc; |
eabb7d53 | 34 | char *argv[]; |
6fe4541f | 35 | { |
edb4a1e3 KB |
36 | register int comp, file1done, file2done, read1, read2; |
37 | register char *col1, *col2, *col3; | |
38 | int ch, flag1, flag2, flag3; | |
eabb7d53 | 39 | FILE *fp1, *fp2, *file(); |
edb4a1e3 | 40 | char **p, line1[MAXLINELEN], line2[MAXLINELEN]; |
eabb7d53 | 41 | extern int optind; |
edb4a1e3 KB |
42 | |
43 | flag1 = flag2 = flag3 = 1; | |
44 | while ((ch = getopt(argc, argv, "-123")) != EOF) | |
45 | switch(ch) { | |
46 | case '-': | |
47 | --optind; | |
48 | goto done; | |
49 | case '1': | |
50 | flag1 = 0; | |
51 | break; | |
52 | case '2': | |
53 | flag2 = 0; | |
54 | break; | |
55 | case '3': | |
56 | flag3 = 0; | |
57 | break; | |
58 | case '?': | |
59 | default: | |
60 | usage(); | |
6fe4541f | 61 | } |
edb4a1e3 KB |
62 | done: argc -= optind; |
63 | argv += optind; | |
64 | ||
65 | if (argc != 2) | |
66 | usage(); | |
67 | ||
68 | fp1 = file(argv[0]); | |
69 | fp2 = file(argv[1]); | |
70 | ||
71 | /* for each column printed, add another tab offset */ | |
72 | p = tabs; | |
aef34388 | 73 | col1 = col2 = col3 = NULL; |
edb4a1e3 KB |
74 | if (flag1) |
75 | col1 = *p++; | |
76 | if (flag2) | |
77 | col2 = *p++; | |
78 | if (flag3) | |
aef34388 | 79 | col3 = *p; |
edb4a1e3 KB |
80 | |
81 | for (read1 = read2 = 1;;) { | |
82 | /* read next line, check for EOF */ | |
83 | if (read1) | |
84 | file1done = !fgets(line1, MAXLINELEN, fp1); | |
85 | if (read2) | |
86 | file2done = !fgets(line2, MAXLINELEN, fp2); | |
87 | ||
88 | /* if one file done, display the rest of the other file */ | |
89 | if (file1done) { | |
90 | if (!file2done && col2) | |
2eae07a1 | 91 | show(fp2, col2, line2); |
edb4a1e3 KB |
92 | break; |
93 | } | |
94 | if (file2done) { | |
95 | if (!file1done && col1) | |
2eae07a1 | 96 | show(fp1, col1, line1); |
edb4a1e3 | 97 | break; |
6fe4541f | 98 | } |
6fe4541f | 99 | |
edb4a1e3 KB |
100 | /* lines are the same */ |
101 | if (!(comp = strcmp(line1, line2))) { | |
102 | read1 = read2 = 1; | |
103 | if (col3) | |
104 | (void)printf("%s%s", col3, line1); | |
105 | continue; | |
106 | } | |
6fe4541f | 107 | |
edb4a1e3 KB |
108 | /* lines are different */ |
109 | if (comp < 0) { | |
110 | read1 = 1; | |
111 | read2 = 0; | |
112 | if (col1) | |
113 | (void)printf("%s%s", col1, line1); | |
114 | } else { | |
115 | read1 = 0; | |
116 | read2 = 1; | |
117 | if (col2) | |
118 | (void)printf("%s%s", col2, line2); | |
6fe4541f | 119 | } |
6fe4541f | 120 | } |
edb4a1e3 | 121 | exit(0); |
6fe4541f BJ |
122 | } |
123 | ||
edb4a1e3 KB |
124 | show(fp, offset, buf) |
125 | FILE *fp; | |
126 | char *offset, *buf; | |
6fe4541f | 127 | { |
edb4a1e3 KB |
128 | do { |
129 | (void)printf("%s%s", offset, buf); | |
130 | } while (fgets(buf, MAXLINELEN, fp)); | |
6fe4541f BJ |
131 | } |
132 | ||
edb4a1e3 KB |
133 | FILE * |
134 | file(name) | |
135 | char *name; | |
6fe4541f | 136 | { |
edb4a1e3 | 137 | FILE *fp; |
6fe4541f | 138 | |
edb4a1e3 KB |
139 | if (!strcmp(name, "-")) |
140 | return(stdin); | |
bd1956e9 KB |
141 | if ((fp = fopen(name, "r")) == NULL) { |
142 | (void)fprintf(stderr, "comm: %s: %s\n", name, strerror(errno)); | |
edb4a1e3 KB |
143 | exit(1); |
144 | } | |
145 | return(fp); | |
6fe4541f BJ |
146 | } |
147 | ||
edb4a1e3 | 148 | usage() |
6fe4541f | 149 | { |
bd1956e9 | 150 | (void)fprintf(stderr, "usage: comm [-123] file1 file2\n"); |
edb4a1e3 | 151 | exit(1); |
6fe4541f | 152 | } |