AMD64 - Fix many compile-time warnings. int/ptr type mismatches, %llx, etc.
[dragonfly.git] / usr.bin / comm / comm.c
blob015fb260ac9af192d79e5e34fca0d37454d0c1d5
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. 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.
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.
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.3 2003/10/02 17:42:27 hmp Exp $
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>
51 #define MAXLINELEN (LINE_MAX + 1)
53 const char *tabs[] = { "", "\t", "\t\t" };
55 FILE *file(char *);
56 void show(FILE *, const char *, char *);
57 int stricoll(char *, char *);
58 static void usage(void);
60 int
61 main(int argc, char **argv)
63 int comp, file1done = 0, file2done = 0, read1, read2;
64 int ch, flag1, flag2, flag3, iflag;
65 FILE *fp1, *fp2;
66 const char *col1, *col2, *col3;
67 char line1[MAXLINELEN], line2[MAXLINELEN];
68 const char **p;
70 flag1 = flag2 = flag3 = 1;
71 iflag = 0;
73 (void) setlocale(LC_ALL, "");
75 while ((ch = getopt(argc, argv, "123i")) != -1)
76 switch(ch) {
77 case '1':
78 flag1 = 0;
79 break;
80 case '2':
81 flag2 = 0;
82 break;
83 case '3':
84 flag3 = 0;
85 break;
86 case 'i':
87 iflag = 1;
88 break;
89 case '?':
90 default:
91 usage();
93 argc -= optind;
94 argv += optind;
96 if (argc != 2)
97 usage();
99 fp1 = file(argv[0]);
100 fp2 = file(argv[1]);
102 /* for each column printed, add another tab offset */
103 p = tabs;
104 col1 = col2 = col3 = NULL;
105 if (flag1)
106 col1 = *p++;
107 if (flag2)
108 col2 = *p++;
109 if (flag3)
110 col3 = *p;
112 for (read1 = read2 = 1;;) {
113 /* read next line, check for EOF */
114 if (read1)
115 file1done = !fgets(line1, MAXLINELEN, fp1);
116 if (read2)
117 file2done = !fgets(line2, MAXLINELEN, fp2);
119 /* if one file done, display the rest of the other file */
120 if (file1done) {
121 if (!file2done && col2)
122 show(fp2, col2, line2);
123 break;
125 if (file2done) {
126 if (!file1done && col1)
127 show(fp1, col1, line1);
128 break;
131 /* lines are the same */
132 if(iflag)
133 comp = stricoll(line1, line2);
134 else
135 comp = strcoll(line1, line2);
137 if (!comp) {
138 read1 = read2 = 1;
139 if (col3)
140 (void)printf("%s%s", col3, line1);
141 continue;
144 /* lines are different */
145 if (comp < 0) {
146 read1 = 1;
147 read2 = 0;
148 if (col1)
149 (void)printf("%s%s", col1, line1);
150 } else {
151 read1 = 0;
152 read2 = 1;
153 if (col2)
154 (void)printf("%s%s", col2, line2);
157 exit(0);
160 void
161 show(FILE *fp, const char *offset, char *buf)
164 do {
165 (void)printf("%s%s", offset, buf);
166 } while (fgets(buf, MAXLINELEN, fp));
169 FILE *
170 file(char *name)
172 FILE *fp;
174 if (!strcmp(name, "-"))
175 return (stdin);
176 if ((fp = fopen(name, "r")) == NULL) {
177 err(1, "%s", name);
179 return (fp);
182 static void
183 usage(void)
185 (void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
186 exit(1);
190 stricoll(char *s1, char *s2)
192 char *p, line1[MAXLINELEN], line2[MAXLINELEN];
194 for (p = line1; *s1; s1++)
195 *p++ = tolower((unsigned char)*s1);
196 *p = '\0';
197 for (p = line2; *s2; s2++)
198 *p++ = tolower((unsigned char)*s2);
199 *p = '\0';
200 return strcoll(line1, line2);