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