arcmsr: Make callout locking more robust
[dragonfly.git] / usr.bin / comm / comm.c
blobed3641dbc40d1061b2927d2611816ab2f1a76152
1 /*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Case Larsen.
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
33 * @(#)comm.c 8.4 (Berkeley) 5/4/95
34 * $FreeBSD: src/usr.bin/comm/comm.c,v 1.11.2.3 2002/07/11 00:46:37 tjr Exp $
35 * $DragonFly: src/usr.bin/comm/comm.c,v 1.3 2003/10/02 17:42:27 hmp Exp $
38 #include <ctype.h>
39 #include <err.h>
40 #include <limits.h>
41 #include <locale.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 #define MAXLINELEN (LINE_MAX + 1)
49 const char *tabs[] = { "", "\t", "\t\t" };
51 FILE *file(char *);
52 void show(FILE *, const char *, char *);
53 int stricoll(char *, char *);
54 static void usage(void);
56 int
57 main(int argc, char **argv)
59 int comp, file1done = 0, file2done = 0, read1, read2;
60 int ch, flag1, flag2, flag3, iflag;
61 FILE *fp1, *fp2;
62 const char *col1, *col2, *col3;
63 char line1[MAXLINELEN], line2[MAXLINELEN];
64 const char **p;
66 flag1 = flag2 = flag3 = 1;
67 iflag = 0;
69 (void) setlocale(LC_ALL, "");
71 while ((ch = getopt(argc, argv, "123i")) != -1)
72 switch(ch) {
73 case '1':
74 flag1 = 0;
75 break;
76 case '2':
77 flag2 = 0;
78 break;
79 case '3':
80 flag3 = 0;
81 break;
82 case 'i':
83 iflag = 1;
84 break;
85 case '?':
86 default:
87 usage();
89 argc -= optind;
90 argv += optind;
92 if (argc != 2)
93 usage();
95 fp1 = file(argv[0]);
96 fp2 = file(argv[1]);
98 /* for each column printed, add another tab offset */
99 p = tabs;
100 col1 = col2 = col3 = NULL;
101 if (flag1)
102 col1 = *p++;
103 if (flag2)
104 col2 = *p++;
105 if (flag3)
106 col3 = *p;
108 for (read1 = read2 = 1;;) {
109 /* read next line, check for EOF */
110 if (read1)
111 file1done = !fgets(line1, MAXLINELEN, fp1);
112 if (read2)
113 file2done = !fgets(line2, MAXLINELEN, fp2);
115 /* if one file done, display the rest of the other file */
116 if (file1done) {
117 if (!file2done && col2)
118 show(fp2, col2, line2);
119 break;
121 if (file2done) {
122 if (!file1done && col1)
123 show(fp1, col1, line1);
124 break;
127 /* lines are the same */
128 if(iflag)
129 comp = stricoll(line1, line2);
130 else
131 comp = strcoll(line1, line2);
133 if (!comp) {
134 read1 = read2 = 1;
135 if (col3)
136 (void)printf("%s%s", col3, line1);
137 continue;
140 /* lines are different */
141 if (comp < 0) {
142 read1 = 1;
143 read2 = 0;
144 if (col1)
145 (void)printf("%s%s", col1, line1);
146 } else {
147 read1 = 0;
148 read2 = 1;
149 if (col2)
150 (void)printf("%s%s", col2, line2);
153 exit(0);
156 void
157 show(FILE *fp, const char *offset, char *buf)
160 do {
161 (void)printf("%s%s", offset, buf);
162 } while (fgets(buf, MAXLINELEN, fp));
165 FILE *
166 file(char *name)
168 FILE *fp;
170 if (!strcmp(name, "-"))
171 return (stdin);
172 if ((fp = fopen(name, "r")) == NULL) {
173 err(1, "%s", name);
175 return (fp);
178 static void
179 usage(void)
181 (void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
182 exit(1);
186 stricoll(char *s1, char *s2)
188 char *p, line1[MAXLINELEN], line2[MAXLINELEN];
190 for (p = line1; *s1; s1++)
191 *p++ = tolower((unsigned char)*s1);
192 *p = '\0';
193 for (p = line2; *s2; s2++)
194 *p++ = tolower((unsigned char)*s2);
195 *p = '\0';
196 return strcoll(line1, line2);