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