xref: /freebsd/usr.bin/uniq/uniq.c (revision a0ee8cc6)
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
34 static 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
41 static char sccsid[] = "@(#)uniq.c	8.3 (Berkeley) 5/4/95";
42 #endif
43 static const char rcsid[] =
44   "$FreeBSD$";
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 
65 static int cflag, dflag, uflag, iflag;
66 static int numchars, numfields, repeats;
67 
68 static FILE	*file(const char *, const char *);
69 static wchar_t	*convert(const char *);
70 static int	 inlcmp(const char *, const char *);
71 static void	 show(FILE *, const char *);
72 static wchar_t	*skip(wchar_t *);
73 static void	 obsolete(char *[]);
74 static void	 usage(void);
75 
76 static void
77 strerror_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 
87 int
88 main (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 no flags are set, default is -d -u. */
134 	if (cflag) {
135 		if (dflag || uflag)
136 			usage();
137 	} else if (!dflag && !uflag)
138 		dflag = uflag = 1;
139 
140 	if (argc > 2)
141 		usage();
142 
143 	ifp = stdin;
144 	ifn = "stdin";
145 	ofp = 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 (cap_rights_limit(fileno(ifp), &rights) < 0 && errno != ENOSYS)
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(argv[1], "w");
154 	else
155 		cap_rights_set(&rights, CAP_IOCTL);
156 	if (cap_rights_limit(fileno(ofp), &rights) < 0 && errno != ENOSYS) {
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 (cap_ioctls_limit(fileno(ofp), &cmd, 1) < 0 &&
166 		    errno != ENOSYS) {
167 			err(1, "unable to limit ioctls for %s",
168 			    argc > 1 ? argv[1] : "stdout");
169 		}
170 	}
171 
172 	strerror_init();
173 	if (cap_enter() < 0 && errno != ENOSYS)
174 		err(1, "unable to enter capability mode");
175 
176 	prevbuflen = thisbuflen = 0;
177 	prevline = thisline = NULL;
178 
179 	if (getline(&prevline, &prevbuflen, ifp) < 0) {
180 		if (ferror(ifp))
181 			err(1, "%s", ifn);
182 		exit(0);
183 	}
184 	tprev = convert(prevline);
185 
186 	if (!cflag && uflag && dflag)
187 		show(ofp, prevline);
188 
189 	tthis = NULL;
190 	while (getline(&thisline, &thisbuflen, ifp) >= 0) {
191 		if (tthis != NULL)
192 			free(tthis);
193 		tthis = convert(thisline);
194 
195 		if (tthis == NULL && tprev == NULL)
196 			comp = inlcmp(thisline, prevline);
197 		else if (tthis == NULL || tprev == NULL)
198 			comp = 1;
199 		else
200 			comp = wcscoll(tthis, tprev);
201 
202 		if (comp) {
203 			/* If different, print; set previous to new value. */
204 			if (cflag || !dflag || !uflag)
205 				show(ofp, prevline);
206 			p = prevline;
207 			b1 = prevbuflen;
208 			prevline = thisline;
209 			prevbuflen = thisbuflen;
210 			if (tprev != NULL)
211 				free(tprev);
212 			tprev = tthis;
213 			if (!cflag && uflag && dflag)
214 				show(ofp, prevline);
215 			thisline = p;
216 			thisbuflen = b1;
217 			tthis = NULL;
218 			repeats = 0;
219 		} else
220 			++repeats;
221 	}
222 	if (ferror(ifp))
223 		err(1, "%s", ifn);
224 	if (cflag || !dflag || !uflag)
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 (cflag)
291 		(void)fprintf(ofp, "%4d %s", repeats + 1, str);
292 	if ((dflag && repeats) || (uflag && !repeats))
293 		(void)fprintf(ofp, "%s", str);
294 }
295 
296 static wchar_t *
297 skip(wchar_t *str)
298 {
299 	int nchars, nfields;
300 
301 	for (nfields = 0; *str != L'\0' && nfields++ != numfields; ) {
302 		while (iswblank(*str))
303 			str++;
304 		while (*str != L'\0' && !iswblank(*str))
305 			str++;
306 	}
307 	for (nchars = numchars; nchars-- && *str != L'\0'; ++str)
308 		;
309 	return(str);
310 }
311 
312 static FILE *
313 file(const char *name, const char *mode)
314 {
315 	FILE *fp;
316 
317 	if ((fp = fopen(name, mode)) == NULL)
318 		err(1, "%s", name);
319 	return(fp);
320 }
321 
322 static void
323 obsolete(char *argv[])
324 {
325 	int len;
326 	char *ap, *p, *start;
327 
328 	while ((ap = *++argv)) {
329 		/* Return if "--" or not an option of any form. */
330 		if (ap[0] != '-') {
331 			if (ap[0] != '+')
332 				return;
333 		} else if (ap[1] == '-')
334 			return;
335 		if (!isdigit((unsigned char)ap[1]))
336 			continue;
337 		/*
338 		 * Digit signifies an old-style option.  Malloc space for dash,
339 		 * new option and argument.
340 		 */
341 		len = strlen(ap);
342 		if ((start = p = malloc(len + 3)) == NULL)
343 			err(1, "malloc");
344 		*p++ = '-';
345 		*p++ = ap[0] == '+' ? 's' : 'f';
346 		(void)strcpy(p, ap + 1);
347 		*argv = start;
348 	}
349 }
350 
351 static void
352 usage(void)
353 {
354 	(void)fprintf(stderr,
355 "usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
356 	exit(1);
357 }
358