xref: /freebsd/usr.bin/uniq/uniq.c (revision 4ed925457ab06e83238a5db33e89ccc94b99a713)
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)uniq.c	8.3 (Berkeley) 5/4/95";
46 #endif
47 static const char rcsid[] =
48   "$FreeBSD$";
49 #endif /* not lint */
50 
51 #include <ctype.h>
52 #include <err.h>
53 #include <limits.h>
54 #include <locale.h>
55 #include <stdint.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <wchar.h>
61 #include <wctype.h>
62 
63 #define	INITLINELEN	(LINE_MAX + 1)
64 #define	MAXLINELEN	((SIZE_MAX / sizeof(wchar_t)) / 2)
65 
66 int cflag, dflag, uflag;
67 int numchars, numfields, repeats;
68 
69 FILE	*file(const char *, const char *);
70 wchar_t	*getline(wchar_t *, size_t *, FILE *);
71 void	 show(FILE *, wchar_t *);
72 wchar_t	*skip(wchar_t *);
73 void	 obsolete(char *[]);
74 static void	 usage(void);
75 int      wcsicoll(wchar_t *, wchar_t *);
76 
77 int
78 main (int argc, char *argv[])
79 {
80 	wchar_t *t1, *t2;
81 	FILE *ifp, *ofp;
82 	int ch, b1;
83 	size_t prevbuflen, thisbuflen;
84 	wchar_t *prevline, *thisline;
85 	char *p;
86 	const char *ifn;
87 	int iflag = 0, comp;
88 
89 	(void) setlocale(LC_ALL, "");
90 
91 	obsolete(argv);
92 	while ((ch = getopt(argc, argv, "cdif:s:u")) != -1)
93 		switch (ch) {
94 		case 'c':
95 			cflag = 1;
96 			break;
97 		case 'd':
98 			dflag = 1;
99 			break;
100 		case 'i':
101 			iflag = 1;
102 			break;
103 		case 'f':
104 			numfields = strtol(optarg, &p, 10);
105 			if (numfields < 0 || *p)
106 				errx(1, "illegal field skip value: %s", optarg);
107 			break;
108 		case 's':
109 			numchars = strtol(optarg, &p, 10);
110 			if (numchars < 0 || *p)
111 				errx(1, "illegal character skip value: %s", optarg);
112 			break;
113 		case 'u':
114 			uflag = 1;
115 			break;
116 		case '?':
117 		default:
118 			usage();
119 		}
120 
121 	argc -= optind;
122 	argv += optind;
123 
124 	/* If no flags are set, default is -d -u. */
125 	if (cflag) {
126 		if (dflag || uflag)
127 			usage();
128 	} else if (!dflag && !uflag)
129 		dflag = uflag = 1;
130 
131 	if (argc > 2)
132 		usage();
133 
134 	ifp = stdin;
135 	ifn = "stdin";
136 	ofp = stdout;
137 	if (argc > 0 && strcmp(argv[0], "-") != 0)
138 		ifp = file(ifn = argv[0], "r");
139 	if (argc > 1)
140 		ofp = file(argv[1], "w");
141 
142  	prevbuflen = INITLINELEN;
143  	thisbuflen = INITLINELEN;
144  	prevline = malloc(prevbuflen * sizeof(*prevline));
145  	thisline = malloc(thisbuflen * sizeof(*thisline));
146 	if (prevline == NULL || thisline == NULL)
147 		err(1, "malloc");
148 
149 	if ((prevline = getline(prevline, &prevbuflen, ifp)) == NULL) {
150 		if (ferror(ifp))
151 			err(1, "%s", ifn);
152 		exit(0);
153 	}
154 	if (!cflag && uflag && dflag)
155 		show(ofp, prevline);
156 
157 	while ((thisline = getline(thisline, &thisbuflen, ifp)) != NULL) {
158 		/* If requested get the chosen fields + character offsets. */
159 		if (numfields || numchars) {
160 			t1 = skip(thisline);
161 			t2 = skip(prevline);
162 		} else {
163 			t1 = thisline;
164 			t2 = prevline;
165 		}
166 
167 		/* If different, print; set previous to new value. */
168 		if (iflag)
169 			comp = wcsicoll(t1, t2);
170 		else
171 			comp = wcscoll(t1, t2);
172 
173 		if (comp) {
174 			if (cflag || !dflag || !uflag)
175 				show(ofp, prevline);
176 			t1 = prevline;
177 			b1 = prevbuflen;
178 			prevline = thisline;
179 			prevbuflen = thisbuflen;
180 			if (!cflag && uflag && dflag)
181 				show(ofp, prevline);
182 			thisline = t1;
183 			thisbuflen = b1;
184 			repeats = 0;
185 		} else
186 			++repeats;
187 	}
188 	if (ferror(ifp))
189 		err(1, "%s", ifn);
190 	if (cflag || !dflag || !uflag)
191 		show(ofp, prevline);
192 	exit(0);
193 }
194 
195 wchar_t *
196 getline(wchar_t *buf, size_t *buflen, FILE *fp)
197 {
198 	size_t bufpos;
199 	wint_t ch;
200 
201 	bufpos = 0;
202 	while ((ch = getwc(fp)) != WEOF && ch != '\n') {
203 		if (bufpos + 1 >= *buflen) {
204 			*buflen = *buflen * 2;
205 			if (*buflen > MAXLINELEN)
206 				errx(1,
207 				    "Maximum line buffer length (%zu) exceeded",
208 				    MAXLINELEN);
209 			buf = reallocf(buf, *buflen * sizeof(*buf));
210 			if (buf == NULL)
211 				err(1, "reallocf");
212 		}
213 		buf[bufpos++] = ch;
214 	}
215 	buf[bufpos] = '\0';
216 
217 	return (bufpos != 0 || ch == '\n' ? buf : NULL);
218 }
219 
220 /*
221  * show --
222  *	Output a line depending on the flags and number of repetitions
223  *	of the line.
224  */
225 void
226 show(FILE *ofp, wchar_t *str)
227 {
228 
229 	if (cflag)
230 		(void)fprintf(ofp, "%4d %ls\n", repeats + 1, str);
231 	if ((dflag && repeats) || (uflag && !repeats))
232 		(void)fprintf(ofp, "%ls\n", str);
233 }
234 
235 wchar_t *
236 skip(wchar_t *str)
237 {
238 	int nchars, nfields;
239 
240 	for (nfields = 0; *str != '\0' && nfields++ != numfields; ) {
241 		while (iswblank(*str))
242 			str++;
243 		while (*str != '\0' && !iswblank(*str))
244 			str++;
245 	}
246 	for (nchars = numchars; nchars-- && *str; ++str);
247 	return(str);
248 }
249 
250 FILE *
251 file(const char *name, const char *mode)
252 {
253 	FILE *fp;
254 
255 	if ((fp = fopen(name, mode)) == NULL)
256 		err(1, "%s", name);
257 	return(fp);
258 }
259 
260 void
261 obsolete(char *argv[])
262 {
263 	int len;
264 	char *ap, *p, *start;
265 
266 	while ((ap = *++argv)) {
267 		/* Return if "--" or not an option of any form. */
268 		if (ap[0] != '-') {
269 			if (ap[0] != '+')
270 				return;
271 		} else if (ap[1] == '-')
272 			return;
273 		if (!isdigit((unsigned char)ap[1]))
274 			continue;
275 		/*
276 		 * Digit signifies an old-style option.  Malloc space for dash,
277 		 * new option and argument.
278 		 */
279 		len = strlen(ap);
280 		if ((start = p = malloc(len + 3)) == NULL)
281 			err(1, "malloc");
282 		*p++ = '-';
283 		*p++ = ap[0] == '+' ? 's' : 'f';
284 		(void)strcpy(p, ap + 1);
285 		*argv = start;
286 	}
287 }
288 
289 static void
290 usage(void)
291 {
292 	(void)fprintf(stderr,
293 "usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
294 	exit(1);
295 }
296 
297 static size_t wcsicoll_l1_buflen = 0, wcsicoll_l2_buflen = 0;
298 static wchar_t *wcsicoll_l1_buf = NULL, *wcsicoll_l2_buf = NULL;
299 
300 int
301 wcsicoll(wchar_t *s1, wchar_t *s2)
302 {
303 	wchar_t *p;
304 	size_t l1, l2;
305 	size_t new_l1_buflen, new_l2_buflen;
306 
307 	l1 = wcslen(s1) + 1;
308 	l2 = wcslen(s2) + 1;
309 	new_l1_buflen = wcsicoll_l1_buflen;
310 	new_l2_buflen = wcsicoll_l2_buflen;
311 	while (new_l1_buflen < l1) {
312 		if (new_l1_buflen == 0)
313 			new_l1_buflen = INITLINELEN;
314 		else
315 			new_l1_buflen *= 2;
316 	}
317 	while (new_l2_buflen < l2) {
318 		if (new_l2_buflen == 0)
319 			new_l2_buflen = INITLINELEN;
320 		else
321 			new_l2_buflen *= 2;
322 	}
323 	if (new_l1_buflen > wcsicoll_l1_buflen) {
324 		wcsicoll_l1_buf = reallocf(wcsicoll_l1_buf, new_l1_buflen * sizeof(*wcsicoll_l1_buf));
325 		if (wcsicoll_l1_buf == NULL)
326                 	err(1, "reallocf");
327 		wcsicoll_l1_buflen = new_l1_buflen;
328 	}
329 	if (new_l2_buflen > wcsicoll_l2_buflen) {
330 		wcsicoll_l2_buf = reallocf(wcsicoll_l2_buf, new_l2_buflen * sizeof(*wcsicoll_l2_buf));
331 		if (wcsicoll_l2_buf == NULL)
332                 	err(1, "reallocf");
333 		wcsicoll_l2_buflen = new_l2_buflen;
334 	}
335 
336 	for (p = wcsicoll_l1_buf; *s1; s1++)
337 		*p++ = towlower(*s1);
338 	*p = '\0';
339 	for (p = wcsicoll_l2_buf; *s2; s2++)
340 		*p++ = towlower(*s2);
341 	*p = '\0';
342 
343 	return (wcscoll(wcsicoll_l1_buf, wcsicoll_l2_buf));
344 }
345