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