xref: /freebsd/usr.bin/uniq/uniq.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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 			} else if (dflag && !cflag) {
229 				if (repeats == 0)
230 					show(ofp, prevline);
231 			}
232 			++repeats;
233 			if (Dflag)
234 				show(ofp, thisline);
235 		}
236 	}
237 	if (ferror(ifp))
238 		err(1, "%s", ifn);
239 	if (!cflag && !Dflag && !dflag && !uflag)
240 		/* already printed */ ;
241 	else if (!Dflag &&
242 	    (!dflag || (cflag && repeats > 0)) &&
243 	    (!uflag || repeats == 0))
244 		show(ofp, prevline);
245 	if (fflush(ofp) != 0)
246 		err(1, "%s", ofn);
247 	exit(0);
248 }
249 
250 static wchar_t *
251 convert(const char *str)
252 {
253 	size_t n;
254 	wchar_t *buf, *ret, *p;
255 
256 	if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
257 		return (NULL);
258 	if (SIZE_MAX / sizeof(*buf) < n + 1)
259 		errx(1, "conversion buffer length overflow");
260 	if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
261 		err(1, "malloc");
262 	if (mbstowcs(buf, str, n + 1) != n)
263 		errx(1, "internal mbstowcs() error");
264 	/* The last line may not end with \n. */
265 	if (n > 0 && buf[n - 1] == L'\n')
266 		buf[n - 1] = L'\0';
267 
268 	/* If requested get the chosen fields + character offsets. */
269 	if (numfields || numchars) {
270 		if ((ret = wcsdup(skip(buf))) == NULL)
271 			err(1, "wcsdup");
272 		free(buf);
273 	} else
274 		ret = buf;
275 
276 	if (iflag) {
277 		for (p = ret; *p != L'\0'; p++)
278 			*p = towlower(*p);
279 	}
280 
281 	return (ret);
282 }
283 
284 static int
285 inlcmp(const char *s1, const char *s2)
286 {
287 	int c1, c2;
288 
289 	while (*s1 == *s2++)
290 		if (*s1++ == '\0')
291 			return (0);
292 	c1 = (unsigned char)*s1;
293 	c2 = (unsigned char)*(s2 - 1);
294 	/* The last line may not end with \n. */
295 	if (c1 == '\n')
296 		c1 = '\0';
297 	if (c2 == '\n')
298 		c2 = '\0';
299 	return (c1 - c2);
300 }
301 
302 /*
303  * show --
304  *	Output a line depending on the flags and number of repetitions
305  *	of the line.
306  */
307 static void
308 show(FILE *ofp, const char *str)
309 {
310 	if (cflag)
311 		(void)fprintf(ofp, "%4lld %s", repeats + 1, str);
312 	else
313 		(void)fprintf(ofp, "%s", str);
314 }
315 
316 static wchar_t *
317 skip(wchar_t *str)
318 {
319 	long long nchars, nfields;
320 
321 	for (nfields = 0; *str != L'\0' && nfields++ != numfields; ) {
322 		while (iswblank(*str))
323 			str++;
324 		while (*str != L'\0' && !iswblank(*str))
325 			str++;
326 	}
327 	for (nchars = numchars; nchars-- && *str != L'\0'; ++str)
328 		;
329 	return(str);
330 }
331 
332 static FILE *
333 file(const char *name, const char *mode)
334 {
335 	FILE *fp;
336 
337 	if ((fp = fopen(name, mode)) == NULL)
338 		err(1, "%s", name);
339 	return(fp);
340 }
341 
342 static void
343 obsolete(char *argv[])
344 {
345 	char *ap, *p;
346 
347 	while ((ap = *++argv)) {
348 		/* Return if "--" or not an option of any form. */
349 		if (ap[0] != '-') {
350 			if (ap[0] != '+')
351 				return;
352 		} else if (ap[1] == '-') {
353 			return;
354 		}
355 		if (!isdigit((unsigned char)ap[1]))
356 			continue;
357 		/*
358 		 * Digit signifies an old-style option.  Malloc space for dash,
359 		 * new option and argument.
360 		 */
361 		if (asprintf(&p, "-%c%s", ap[0] == '+' ? 's' : 'f', ap + 1) < 0)
362 			err(1, "malloc");
363 		*argv = p;
364 	}
365 }
366 
367 static void
368 usage(void)
369 {
370 	(void)fprintf(stderr, "usage: uniq [-cdiu] [-D[septype]] "
371 	    "[-f fields] [-s chars] [input [output]]\n");
372 	exit(1);
373 }
374