Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / comm / comm.c
1 /*
2  * Copyright (c) 1989, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Case Larsen.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  *
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.2 2003/06/17 04:29:25 dillon Exp $
40  */
41
42 #include <ctype.h>
43 #include <err.h>
44 #include <limits.h>
45 #include <locale.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #define MAXLINELEN      (LINE_MAX + 1)
52
53 const char *tabs[] = { "", "\t", "\t\t" };
54
55 FILE   *file(char *);
56 void    show(FILE *, const char *, char *);
57 int     stricoll(char *, char *);
58 static void     usage(void);
59
60 int
61 main(argc, argv)
62         int argc;
63         char *argv[];
64 {
65         int comp, file1done = 0, file2done = 0, read1, read2;
66         int ch, flag1, flag2, flag3, iflag;
67         FILE *fp1, *fp2;
68         const char *col1, *col2, *col3;
69         char line1[MAXLINELEN], line2[MAXLINELEN];
70         const char **p;
71
72         flag1 = flag2 = flag3 = 1;
73         iflag = 0;
74
75         (void) setlocale(LC_ALL, "");
76
77         while ((ch = getopt(argc, argv, "123i")) != -1)
78                 switch(ch) {
79                 case '1':
80                         flag1 = 0;
81                         break;
82                 case '2':
83                         flag2 = 0;
84                         break;
85                 case '3':
86                         flag3 = 0;
87                         break;
88                 case 'i':
89                         iflag = 1;
90                         break;
91                 case '?':
92                 default:
93                         usage();
94                 }
95         argc -= optind;
96         argv += optind;
97
98         if (argc != 2)
99                 usage();
100
101         fp1 = file(argv[0]);
102         fp2 = file(argv[1]);
103
104         /* for each column printed, add another tab offset */
105         p = tabs;
106         col1 = col2 = col3 = NULL;
107         if (flag1)
108                 col1 = *p++;
109         if (flag2)
110                 col2 = *p++;
111         if (flag3)
112                 col3 = *p;
113
114         for (read1 = read2 = 1;;) {
115                 /* read next line, check for EOF */
116                 if (read1)
117                         file1done = !fgets(line1, MAXLINELEN, fp1);
118                 if (read2)
119                         file2done = !fgets(line2, MAXLINELEN, fp2);
120
121                 /* if one file done, display the rest of the other file */
122                 if (file1done) {
123                         if (!file2done && col2)
124                                 show(fp2, col2, line2);
125                         break;
126                 }
127                 if (file2done) {
128                         if (!file1done && col1)
129                                 show(fp1, col1, line1);
130                         break;
131                 }
132
133                 /* lines are the same */
134                 if(iflag)
135                         comp = stricoll(line1, line2);
136                 else
137                         comp = strcoll(line1, line2);
138
139                 if (!comp) {
140                         read1 = read2 = 1;
141                         if (col3)
142                                 (void)printf("%s%s", col3, line1);
143                         continue;
144                 }
145
146                 /* lines are different */
147                 if (comp < 0) {
148                         read1 = 1;
149                         read2 = 0;
150                         if (col1)
151                                 (void)printf("%s%s", col1, line1);
152                 } else {
153                         read1 = 0;
154                         read2 = 1;
155                         if (col2)
156                                 (void)printf("%s%s", col2, line2);
157                 }
158         }
159         exit(0);
160 }
161
162 void
163 show(fp, offset, buf)
164         FILE *fp;
165         const char *offset;
166         char *buf;
167 {
168
169         do {
170                 (void)printf("%s%s", offset, buf);
171         } while (fgets(buf, MAXLINELEN, fp));
172 }
173
174 FILE *
175 file(name)
176         char *name;
177 {
178         FILE *fp;
179
180         if (!strcmp(name, "-"))
181                 return (stdin);
182         if ((fp = fopen(name, "r")) == NULL) {
183                 err(1, "%s", name);
184         }
185         return (fp);
186 }
187
188 static void
189 usage()
190 {
191         (void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
192         exit(1);
193 }
194
195 int
196 stricoll(s1, s2)
197         char *s1, *s2;
198 {
199         char *p, line1[MAXLINELEN], line2[MAXLINELEN];
200
201         for (p = line1; *s1; s1++)
202                 *p++ = tolower((unsigned char)*s1);
203         *p = '\0';
204         for (p = line2; *s2; s2++)
205                 *p++ = tolower((unsigned char)*s2);
206         *p = '\0';
207         return strcoll(line1, line2);
208 }