xref: /freebsd/usr.bin/uniq/uniq.c (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
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 #include <sys/capsicum.h>
36 
37 #include <capsicum_helpers.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <getopt.h>
42 #include <limits.h>
43 #include <locale.h>
44 #include <nl_types.h>
45 #include <stdbool.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <termios.h>
51 #include <unistd.h>
52 #include <wchar.h>
53 #include <wctype.h>
54 
55 static enum { DF_NONE, DF_NOSEP, DF_PRESEP, DF_POSTSEP } Dflag;
56 static bool cflag, dflag, uflag, iflag;
57 static long long numchars, numfields, repeats;
58 
59 static const struct option long_opts[] =
60 {
61 	{"all-repeated",optional_argument,	NULL, 'D'},
62 	{"count",	no_argument,		NULL, 'c'},
63 	{"repeated",	no_argument,		NULL, 'd'},
64 	{"skip-fields",	required_argument,	NULL, 'f'},
65 	{"ignore-case",	no_argument,		NULL, 'i'},
66 	{"skip-chars",	required_argument,	NULL, 's'},
67 	{"unique",	no_argument,		NULL, 'u'},
68 	{NULL,		no_argument,		NULL, 0}
69 };
70 
71 static FILE	*file(const char *, const char *);
72 static wchar_t	*convert(const char *);
73 static int	 inlcmp(const char *, const char *);
74 static void	 show(FILE *, const char *);
75 static wchar_t	*skip(wchar_t *);
76 static void	 obsolete(char *[]);
77 static void	 usage(void);
78 
79 int
80 main (int argc, char *argv[])
81 {
82 	wchar_t *tprev, *tthis;
83 	FILE *ifp, *ofp;
84 	int ch, comp;
85 	size_t prevbuflen, thisbuflen, b1;
86 	char *prevline, *thisline, *p;
87 	const char *errstr, *ifn, *ofn;
88 	cap_rights_t rights;
89 
90 	(void) setlocale(LC_ALL, "");
91 
92 	obsolete(argv);
93 	while ((ch = getopt_long(argc, argv, "+D::cdif:s:u", long_opts,
94 	    NULL)) != -1)
95 		switch (ch) {
96 		case 'D':
97 			if (optarg == NULL || strcasecmp(optarg, "none") == 0)
98 				Dflag = DF_NOSEP;
99 			else if (strcasecmp(optarg, "prepend") == 0)
100 				Dflag = DF_PRESEP;
101 			else if (strcasecmp(optarg, "separate") == 0)
102 				Dflag = DF_POSTSEP;
103 			else
104 				usage();
105 			break;
106 		case 'c':
107 			cflag = true;
108 			break;
109 		case 'd':
110 			dflag = true;
111 			break;
112 		case 'i':
113 			iflag = true;
114 			break;
115 		case 'f':
116 			numfields = strtonum(optarg, 0, INT_MAX, &errstr);
117 			if (errstr)
118 				errx(1, "field skip value is %s: %s", errstr, optarg);
119 			break;
120 		case 's':
121 			numchars = strtonum(optarg, 0, INT_MAX, &errstr);
122 			if (errstr != NULL)
123 				errx(1, "character skip value is %s: %s", errstr, optarg);
124 			break;
125 		case 'u':
126 			uflag = true;
127 			break;
128 		case '?':
129 		default:
130 			usage();
131 		}
132 
133 	argc -= optind;
134 	argv += optind;
135 
136 	if (argc > 2)
137 		usage();
138 
139 	if (Dflag && dflag)
140 		dflag = false;
141 
142 	ifp = stdin;
143 	ifn = "stdin";
144 	ofp = stdout;
145 	ofn = "stdout";
146 	if (argc > 0 && strcmp(argv[0], "-") != 0)
147 		ifp = file(ifn = argv[0], "r");
148 	cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
149 	if (caph_rights_limit(fileno(ifp), &rights) < 0)
150 		err(1, "unable to limit rights for %s", ifn);
151 	cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
152 	if (argc > 1)
153 		ofp = file(ofn = argv[1], "w");
154 	else
155 		cap_rights_set(&rights, CAP_IOCTL);
156 	if (caph_rights_limit(fileno(ofp), &rights) < 0) {
157 		err(1, "unable to limit rights for %s",
158 		    argc > 1 ? argv[1] : "stdout");
159 	}
160 	if (cap_rights_is_set(&rights, CAP_IOCTL)) {
161 		unsigned long cmd;
162 
163 		cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
164 
165 		if (caph_ioctls_limit(fileno(ofp), &cmd, 1) < 0) {
166 			err(1, "unable to limit ioctls for %s",
167 			    argc > 1 ? argv[1] : "stdout");
168 		}
169 	}
170 
171 	caph_cache_catpages();
172 	if (caph_enter() < 0)
173 		err(1, "unable to enter capability mode");
174 
175 	prevbuflen = thisbuflen = 0;
176 	prevline = thisline = NULL;
177 
178 	if (getline(&prevline, &prevbuflen, ifp) < 0) {
179 		if (ferror(ifp))
180 			err(1, "%s", ifn);
181 		exit(0);
182 	}
183 	if (!cflag && !Dflag && !dflag && !uflag)
184 		show(ofp, prevline);
185 	tprev = convert(prevline);
186 
187 	tthis = NULL;
188 	while (getline(&thisline, &thisbuflen, ifp) >= 0) {
189 		if (tthis != NULL)
190 			free(tthis);
191 		tthis = convert(thisline);
192 
193 		if (tthis == NULL && tprev == NULL)
194 			comp = inlcmp(thisline, prevline);
195 		else if (tthis == NULL || tprev == NULL)
196 			comp = 1;
197 		else
198 			comp = wcscoll(tthis, tprev);
199 
200 		if (comp) {
201 			/* If different, print; set previous to new value. */
202 			if (Dflag == DF_POSTSEP && repeats > 0)
203 				fputc('\n', ofp);
204 			if (!cflag && !Dflag && !dflag && !uflag)
205 				show(ofp, thisline);
206 			else if (!Dflag &&
207 			    (!dflag || (cflag && repeats > 0)) &&
208 			    (!uflag || repeats == 0))
209 				show(ofp, prevline);
210 			p = prevline;
211 			b1 = prevbuflen;
212 			prevline = thisline;
213 			prevbuflen = thisbuflen;
214 			if (tprev != NULL)
215 				free(tprev);
216 			tprev = tthis;
217 			thisline = p;
218 			thisbuflen = b1;
219 			tthis = NULL;
220 			repeats = 0;
221 		} else {
222 			if (Dflag) {
223 				if (repeats == 0) {
224 					if (Dflag == DF_PRESEP)
225 						fputc('\n', ofp);
226 					show(ofp, prevline);
227 				}
228 				show(ofp, thisline);
229 			} else if (dflag && !cflag) {
230 				if (repeats == 0)
231 					show(ofp, prevline);
232 			}
233 			++repeats;
234 		}
235 	}
236 	if (ferror(ifp))
237 		err(1, "%s", ifn);
238 	if (!cflag && !Dflag && !dflag && !uflag)
239 		/* already printed */ ;
240 	else if (!Dflag &&
241 	    (!dflag || (cflag && repeats > 0)) &&
242 	    (!uflag || repeats == 0))
243 		show(ofp, prevline);
244 	if (fflush(ofp) != 0)
245 		err(1, "%s", ofn);
246 	exit(0);
247 }
248 
249 static wchar_t *
250 convert(const char *str)
251 {
252 	size_t n;
253 	wchar_t *buf, *ret, *p;
254 
255 	if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
256 		return (NULL);
257 	if (SIZE_MAX / sizeof(*buf) < n + 1)
258 		errx(1, "conversion buffer length overflow");
259 	if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
260 		err(1, "malloc");
261 	if (mbstowcs(buf, str, n + 1) != n)
262 		errx(1, "internal mbstowcs() error");
263 	/* The last line may not end with \n. */
264 	if (n > 0 && buf[n - 1] == L'\n')
265 		buf[n - 1] = L'\0';
266 
267 	/* If requested get the chosen fields + character offsets. */
268 	if (numfields || numchars) {
269 		if ((ret = wcsdup(skip(buf))) == NULL)
270 			err(1, "wcsdup");
271 		free(buf);
272 	} else
273 		ret = buf;
274 
275 	if (iflag) {
276 		for (p = ret; *p != L'\0'; p++)
277 			*p = towlower(*p);
278 	}
279 
280 	return (ret);
281 }
282 
283 static int
284 inlcmp(const char *s1, const char *s2)
285 {
286 	int c1, c2;
287 
288 	while (*s1 == *s2++)
289 		if (*s1++ == '\0')
290 			return (0);
291 	c1 = (unsigned char)*s1;
292 	c2 = (unsigned char)*(s2 - 1);
293 	/* The last line may not end with \n. */
294 	if (c1 == '\n')
295 		c1 = '\0';
296 	if (c2 == '\n')
297 		c2 = '\0';
298 	return (c1 - c2);
299 }
300 
301 /*
302  * show --
303  *	Output a line depending on the flags and number of repetitions
304  *	of the line.
305  */
306 static void
307 show(FILE *ofp, const char *str)
308 {
309 	if (cflag)
310 		(void)fprintf(ofp, "%4lld %s", repeats + 1, str);
311 	else
312 		(void)fprintf(ofp, "%s", str);
313 }
314 
315 static wchar_t *
316 skip(wchar_t *str)
317 {
318 	long long nchars, nfields;
319 
320 	for (nfields = 0; *str != L'\0' && nfields++ != numfields; ) {
321 		while (iswblank(*str))
322 			str++;
323 		while (*str != L'\0' && !iswblank(*str))
324 			str++;
325 	}
326 	for (nchars = numchars; nchars-- && *str != L'\0'; ++str)
327 		;
328 	return(str);
329 }
330 
331 static FILE *
332 file(const char *name, const char *mode)
333 {
334 	FILE *fp;
335 
336 	if ((fp = fopen(name, mode)) == NULL)
337 		err(1, "%s", name);
338 	return(fp);
339 }
340 
341 static void
342 obsolete(char *argv[])
343 {
344 	char *ap, *p;
345 
346 	while ((ap = *++argv)) {
347 		/* Return if "--" or not an option of any form. */
348 		if (ap[0] != '-') {
349 			if (ap[0] != '+')
350 				return;
351 		} else if (ap[1] == '-') {
352 			return;
353 		}
354 		if (!isdigit((unsigned char)ap[1]))
355 			continue;
356 		/*
357 		 * Digit signifies an old-style option.  Malloc space for dash,
358 		 * new option and argument.
359 		 */
360 		if (asprintf(&p, "-%c%s", ap[0] == '+' ? 's' : 'f', ap + 1) < 0)
361 			err(1, "malloc");
362 		*argv = p;
363 	}
364 }
365 
366 static void
367 usage(void)
368 {
369 	(void)fprintf(stderr,
370 "usage: uniq [-c | -d | -D | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
371 	exit(1);
372 }
373