1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Case Larsen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95";
44 #endif
45 #endif /* not lint */
46
47 #include <sys/capsicum.h>
48
49 #include <capsicum_helpers.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <getopt.h>
54 #include <limits.h>
55 #include <locale.h>
56 #include <nl_types.h>
57 #include <stdbool.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <termios.h>
63 #include <unistd.h>
64 #include <wchar.h>
65 #include <wctype.h>
66
67 static enum { DF_NONE, DF_NOSEP, DF_PRESEP, DF_POSTSEP } Dflag;
68 static bool cflag, dflag, uflag, iflag;
69 static long long numchars, numfields, repeats;
70
71 static const struct option long_opts[] =
72 {
73 {"all-repeated",optional_argument, NULL, 'D'},
74 {"count", no_argument, NULL, 'c'},
75 {"repeated", no_argument, NULL, 'd'},
76 {"skip-fields", required_argument, NULL, 'f'},
77 {"ignore-case", no_argument, NULL, 'i'},
78 {"skip-chars", required_argument, NULL, 's'},
79 {"unique", no_argument, NULL, 'u'},
80 {NULL, no_argument, NULL, 0}
81 };
82
83 static FILE *file(const char *, const char *);
84 static wchar_t *convert(const char *);
85 static int inlcmp(const char *, const char *);
86 static void show(FILE *, const char *);
87 static wchar_t *skip(wchar_t *);
88 static void obsolete(char *[]);
89 static void usage(void);
90
91 int
main(int argc,char * argv[])92 main (int argc, char *argv[])
93 {
94 wchar_t *tprev, *tthis;
95 FILE *ifp, *ofp;
96 int ch, comp;
97 size_t prevbuflen, thisbuflen, b1;
98 char *prevline, *thisline, *p;
99 const char *errstr, *ifn, *ofn;
100 cap_rights_t rights;
101
102 (void) setlocale(LC_ALL, "");
103
104 obsolete(argv);
105 while ((ch = getopt_long(argc, argv, "+D::cdif:s:u", long_opts,
106 NULL)) != -1)
107 switch (ch) {
108 case 'D':
109 if (optarg == NULL || strcasecmp(optarg, "none") == 0)
110 Dflag = DF_NOSEP;
111 else if (strcasecmp(optarg, "prepend") == 0)
112 Dflag = DF_PRESEP;
113 else if (strcasecmp(optarg, "separate") == 0)
114 Dflag = DF_POSTSEP;
115 else
116 usage();
117 break;
118 case 'c':
119 cflag = true;
120 break;
121 case 'd':
122 dflag = true;
123 break;
124 case 'i':
125 iflag = true;
126 break;
127 case 'f':
128 numfields = strtonum(optarg, 0, INT_MAX, &errstr);
129 if (errstr)
130 errx(1, "field skip value is %s: %s", errstr, optarg);
131 break;
132 case 's':
133 numchars = strtonum(optarg, 0, INT_MAX, &errstr);
134 if (errstr != NULL)
135 errx(1, "character skip value is %s: %s", errstr, optarg);
136 break;
137 case 'u':
138 uflag = true;
139 break;
140 case '?':
141 default:
142 usage();
143 }
144
145 argc -= optind;
146 argv += optind;
147
148 if (argc > 2)
149 usage();
150
151 if (Dflag && dflag)
152 dflag = false;
153
154 ifp = stdin;
155 ifn = "stdin";
156 ofp = stdout;
157 ofn = "stdout";
158 if (argc > 0 && strcmp(argv[0], "-") != 0)
159 ifp = file(ifn = argv[0], "r");
160 cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
161 if (caph_rights_limit(fileno(ifp), &rights) < 0)
162 err(1, "unable to limit rights for %s", ifn);
163 cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
164 if (argc > 1)
165 ofp = file(ofn = argv[1], "w");
166 else
167 cap_rights_set(&rights, CAP_IOCTL);
168 if (caph_rights_limit(fileno(ofp), &rights) < 0) {
169 err(1, "unable to limit rights for %s",
170 argc > 1 ? argv[1] : "stdout");
171 }
172 if (cap_rights_is_set(&rights, CAP_IOCTL)) {
173 unsigned long cmd;
174
175 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
176
177 if (caph_ioctls_limit(fileno(ofp), &cmd, 1) < 0) {
178 err(1, "unable to limit ioctls for %s",
179 argc > 1 ? argv[1] : "stdout");
180 }
181 }
182
183 caph_cache_catpages();
184 if (caph_enter() < 0)
185 err(1, "unable to enter capability mode");
186
187 prevbuflen = thisbuflen = 0;
188 prevline = thisline = NULL;
189
190 if (getline(&prevline, &prevbuflen, ifp) < 0) {
191 if (ferror(ifp))
192 err(1, "%s", ifn);
193 exit(0);
194 }
195 if (!cflag && !Dflag && !dflag && !uflag)
196 show(ofp, prevline);
197 tprev = convert(prevline);
198
199 tthis = NULL;
200 while (getline(&thisline, &thisbuflen, ifp) >= 0) {
201 if (tthis != NULL)
202 free(tthis);
203 tthis = convert(thisline);
204
205 if (tthis == NULL && tprev == NULL)
206 comp = inlcmp(thisline, prevline);
207 else if (tthis == NULL || tprev == NULL)
208 comp = 1;
209 else
210 comp = wcscoll(tthis, tprev);
211
212 if (comp) {
213 /* If different, print; set previous to new value. */
214 if (Dflag == DF_POSTSEP && repeats > 0)
215 fputc('\n', ofp);
216 if (!cflag && !Dflag && !dflag && !uflag)
217 show(ofp, thisline);
218 else if (!Dflag &&
219 (!dflag || (cflag && repeats > 0)) &&
220 (!uflag || repeats == 0))
221 show(ofp, prevline);
222 p = prevline;
223 b1 = prevbuflen;
224 prevline = thisline;
225 prevbuflen = thisbuflen;
226 if (tprev != NULL)
227 free(tprev);
228 tprev = tthis;
229 thisline = p;
230 thisbuflen = b1;
231 tthis = NULL;
232 repeats = 0;
233 } else {
234 if (Dflag) {
235 if (repeats == 0) {
236 if (Dflag == DF_PRESEP)
237 fputc('\n', ofp);
238 show(ofp, prevline);
239 }
240 } else if (dflag && !cflag) {
241 if (repeats == 0)
242 show(ofp, prevline);
243 }
244 ++repeats;
245 if (Dflag)
246 show(ofp, thisline);
247 }
248 }
249 if (ferror(ifp))
250 err(1, "%s", ifn);
251 if (!cflag && !Dflag && !dflag && !uflag)
252 /* already printed */ ;
253 else if (!Dflag &&
254 (!dflag || (cflag && repeats > 0)) &&
255 (!uflag || repeats == 0))
256 show(ofp, prevline);
257 if (fflush(ofp) != 0)
258 err(1, "%s", ofn);
259 exit(0);
260 }
261
262 static wchar_t *
convert(const char * str)263 convert(const char *str)
264 {
265 size_t n;
266 wchar_t *buf, *ret, *p;
267
268 if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
269 return (NULL);
270 if (SIZE_MAX / sizeof(*buf) < n + 1)
271 errx(1, "conversion buffer length overflow");
272 if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
273 err(1, "malloc");
274 if (mbstowcs(buf, str, n + 1) != n)
275 errx(1, "internal mbstowcs() error");
276 /* The last line may not end with \n. */
277 if (n > 0 && buf[n - 1] == L'\n')
278 buf[n - 1] = L'\0';
279
280 /* If requested get the chosen fields + character offsets. */
281 if (numfields || numchars) {
282 if ((ret = wcsdup(skip(buf))) == NULL)
283 err(1, "wcsdup");
284 free(buf);
285 } else
286 ret = buf;
287
288 if (iflag) {
289 for (p = ret; *p != L'\0'; p++)
290 *p = towlower(*p);
291 }
292
293 return (ret);
294 }
295
296 static int
inlcmp(const char * s1,const char * s2)297 inlcmp(const char *s1, const char *s2)
298 {
299 int c1, c2;
300
301 while (*s1 == *s2++)
302 if (*s1++ == '\0')
303 return (0);
304 c1 = (unsigned char)*s1;
305 c2 = (unsigned char)*(s2 - 1);
306 /* The last line may not end with \n. */
307 if (c1 == '\n')
308 c1 = '\0';
309 if (c2 == '\n')
310 c2 = '\0';
311 return (c1 - c2);
312 }
313
314 /*
315 * show --
316 * Output a line depending on the flags and number of repetitions
317 * of the line.
318 */
319 static void
show(FILE * ofp,const char * str)320 show(FILE *ofp, const char *str)
321 {
322 if (cflag)
323 (void)fprintf(ofp, "%4lld %s", repeats + 1, str);
324 else
325 (void)fprintf(ofp, "%s", str);
326 }
327
328 static wchar_t *
skip(wchar_t * str)329 skip(wchar_t *str)
330 {
331 long long nchars, nfields;
332
333 for (nfields = 0; *str != L'\0' && nfields++ != numfields; ) {
334 while (iswblank(*str))
335 str++;
336 while (*str != L'\0' && !iswblank(*str))
337 str++;
338 }
339 for (nchars = numchars; nchars-- && *str != L'\0'; ++str)
340 ;
341 return(str);
342 }
343
344 static FILE *
file(const char * name,const char * mode)345 file(const char *name, const char *mode)
346 {
347 FILE *fp;
348
349 if ((fp = fopen(name, mode)) == NULL)
350 err(1, "%s", name);
351 return(fp);
352 }
353
354 static void
obsolete(char * argv[])355 obsolete(char *argv[])
356 {
357 char *ap, *p;
358
359 while ((ap = *++argv)) {
360 /* Return if "--" or not an option of any form. */
361 if (ap[0] != '-') {
362 if (ap[0] != '+')
363 return;
364 } else if (ap[1] == '-') {
365 return;
366 }
367 if (!isdigit((unsigned char)ap[1]))
368 continue;
369 /*
370 * Digit signifies an old-style option. Malloc space for dash,
371 * new option and argument.
372 */
373 if (asprintf(&p, "-%c%s", ap[0] == '+' ? 's' : 'f', ap + 1) < 0)
374 err(1, "malloc");
375 *argv = p;
376 }
377 }
378
379 static void
usage(void)380 usage(void)
381 {
382 (void)fprintf(stderr, "usage: uniq [-cdiu] [-D[septype]] "
383 "[-f fields] [-s chars] [input [output]]\n");
384 exit(1);
385 }
386