hammer2 - Fix kmalloc pool blowout on low-memory machines
[dragonfly.git] / usr.bin / comm / comm.c
blob06717570173efe22beb29687aa7f4664a466ceeb
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: head/usr.bin/comm/comm.c 303526 2016-07-30 01:07:47Z bapt $
37 #include <err.h>
38 #include <limits.h>
39 #include <locale.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <wchar.h>
46 #include <wctype.h>
48 static int iflag;
49 static const char *tabs[] = { "", "\t", "\t\t" };
51 static FILE *file(const char *);
52 static wchar_t *convert(const char *);
53 static void show(FILE *, const char *, const char *, char **, size_t *);
54 static void usage(void);
56 int
57 main(int argc, char *argv[])
59 int comp, read1, read2;
60 int ch, flag1, flag2, flag3;
61 FILE *fp1, *fp2;
62 const char *col1, *col2, *col3;
63 size_t line1len, line2len;
64 char *line1, *line2;
65 ssize_t n1, n2;
66 wchar_t *tline1, *tline2;
67 const char **p;
69 (void) setlocale(LC_ALL, "");
71 flag1 = flag2 = flag3 = 1;
73 while ((ch = getopt(argc, argv, "123i")) != -1)
74 switch(ch) {
75 case '1':
76 flag1 = 0;
77 break;
78 case '2':
79 flag2 = 0;
80 break;
81 case '3':
82 flag3 = 0;
83 break;
84 case 'i':
85 iflag = 1;
86 break;
87 case '?':
88 default:
89 usage();
91 argc -= optind;
92 argv += optind;
94 if (argc != 2)
95 usage();
97 fp1 = file(argv[0]);
98 fp2 = file(argv[1]);
100 /* for each column printed, add another tab offset */
101 p = tabs;
102 col1 = col2 = col3 = NULL;
103 if (flag1)
104 col1 = *p++;
105 if (flag2)
106 col2 = *p++;
107 if (flag3)
108 col3 = *p;
110 line1len = line2len = 0;
111 line1 = line2 = NULL;
112 n1 = n2 = -1;
114 for (read1 = read2 = 1;;) {
115 /* read next line, check for EOF */
116 if (read1) {
117 n1 = getline(&line1, &line1len, fp1);
118 if (n1 < 0 && ferror(fp1))
119 err(1, "%s", argv[0]);
120 if (n1 > 0 && line1[n1 - 1] == '\n')
121 line1[n1 - 1] = '\0';
124 if (read2) {
125 n2 = getline(&line2, &line2len, fp2);
126 if (n2 < 0 && ferror(fp2))
127 err(1, "%s", argv[1]);
128 if (n2 > 0 && line2[n2 - 1] == '\n')
129 line2[n2 - 1] = '\0';
132 /* if one file done, display the rest of the other file */
133 if (n1 < 0) {
134 if (n2 >= 0 && col2 != NULL)
135 show(fp2, argv[1], col2, &line2, &line2len);
136 break;
138 if (n2 < 0) {
139 if (n1 >= 0 && col1 != NULL)
140 show(fp1, argv[0], col1, &line1, &line1len);
141 break;
144 tline2 = NULL;
145 if ((tline1 = convert(line1)) != NULL)
146 tline2 = convert(line2);
147 if (tline1 == NULL || tline2 == NULL)
148 comp = strcmp(line1, line2);
149 else
150 comp = wcscoll(tline1, tline2);
151 if (tline1 != NULL)
152 free(tline1);
153 if (tline2 != NULL)
154 free(tline2);
156 /* lines are the same */
157 if (!comp) {
158 read1 = read2 = 1;
159 if (col3 != NULL)
160 (void)printf("%s%s\n", col3, line1);
161 continue;
164 /* lines are different */
165 if (comp < 0) {
166 read1 = 1;
167 read2 = 0;
168 if (col1 != NULL)
169 (void)printf("%s%s\n", col1, line1);
170 } else {
171 read1 = 0;
172 read2 = 1;
173 if (col2 != NULL)
174 (void)printf("%s%s\n", col2, line2);
177 exit(0);
180 static wchar_t *
181 convert(const char *str)
183 size_t n;
184 wchar_t *buf, *p;
186 if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
187 return (NULL);
188 if (SIZE_MAX / sizeof(*buf) < n + 1)
189 errx(1, "conversion buffer length overflow");
190 if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
191 err(1, "malloc");
192 if (mbstowcs(buf, str, n + 1) != n)
193 errx(1, "internal mbstowcs() error");
195 if (iflag) {
196 for (p = buf; *p != L'\0'; p++)
197 *p = towlower(*p);
200 return (buf);
203 static void
204 show(FILE *fp, const char *fn, const char *offset, char **bufp, size_t *buflenp)
206 ssize_t n;
208 do {
209 (void)printf("%s%s\n", offset, *bufp);
210 if ((n = getline(bufp, buflenp, fp)) < 0)
211 break;
212 if (n > 0 && (*bufp)[n - 1] == '\n')
213 (*bufp)[n - 1] = '\0';
214 } while (1);
215 if (ferror(fp))
216 err(1, "%s", fn);
219 static FILE *
220 file(const char *name)
222 FILE *fp;
224 if (!strcmp(name, "-"))
225 return (stdin);
226 if ((fp = fopen(name, "r")) == NULL) {
227 err(1, "%s", name);
229 return (fp);
232 static void
233 usage(void)
235 (void)fprintf(stderr, "usage: comm [-123i] file1 file2\n");
236 exit(1);