2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95";
47 static const char rcsid[] =
62 #define MAXLINELEN (LINE_MAX + 1)
64 int cflag, dflag, uflag;
65 int numchars, numfields, repeats;
67 FILE *file(const char *, const char *);
68 wchar_t *getline(wchar_t *, size_t *, FILE *);
69 void show(FILE *, wchar_t *);
70 wchar_t *skip(wchar_t *);
71 void obsolete(char *[]);
72 static void usage(void);
73 int wcsicoll(wchar_t *, wchar_t *);
76 main (int argc, char *argv[])
81 size_t prevbuflen, thisbuflen;
82 wchar_t *prevline, *thisline;
87 (void) setlocale(LC_ALL, "");
90 while ((ch = getopt(argc, argv, "cdif:s:u")) != -1)
102 numfields = strtol(optarg, &p, 10);
103 if (numfields < 0 || *p)
104 errx(1, "illegal field skip value: %s", optarg);
107 numchars = strtol(optarg, &p, 10);
108 if (numchars < 0 || *p)
109 errx(1, "illegal character skip value: %s", optarg);
122 /* If no flags are set, default is -d -u. */
126 } else if (!dflag && !uflag)
135 if (argc > 0 && strcmp(argv[0], "-") != 0)
136 ifp = file(ifn = argv[0], "r");
138 ofp = file(argv[1], "w");
140 prevbuflen = MAXLINELEN;
141 thisbuflen = MAXLINELEN;
142 prevline = malloc(prevbuflen * sizeof(*prevline));
143 thisline = malloc(thisbuflen * sizeof(*thisline));
144 if (prevline == NULL || thisline == NULL)
147 if ((prevline = getline(prevline, &prevbuflen, ifp)) == NULL) {
152 if (!cflag && uflag && dflag)
155 while ((thisline = getline(thisline, &thisbuflen, ifp)) != NULL) {
156 /* If requested get the chosen fields + character offsets. */
157 if (numfields || numchars) {
165 /* If different, print; set previous to new value. */
167 comp = wcsicoll(t1, t2);
169 comp = wcscoll(t1, t2);
172 if (cflag || !dflag || !uflag)
177 prevbuflen = thisbuflen;
178 if (!cflag && uflag && dflag)
188 if (cflag || !dflag || !uflag)
194 getline(wchar_t *buf, size_t *buflen, FILE *fp)
200 while ((ch = getwc(fp)) != WEOF && ch != '\n') {
201 if (bufpos + 2 >= *buflen) {
202 *buflen = *buflen * 2;
203 buf = reallocf(buf, *buflen * sizeof(*buf));
209 if (bufpos + 1 != *buflen)
212 return (bufpos != 0 || ch == '\n' ? buf : NULL);
217 * Output a line depending on the flags and number of repetitions
221 show(FILE *ofp, wchar_t *str)
225 (void)fprintf(ofp, "%4d %ls\n", repeats + 1, str);
226 if ((dflag && repeats) || (uflag && !repeats))
227 (void)fprintf(ofp, "%ls\n", str);
235 for (nfields = 0; *str != '\0' && nfields++ != numfields; ) {
236 while (iswblank(*str))
238 while (*str != '\0' && !iswblank(*str))
241 for (nchars = numchars; nchars-- && *str; ++str);
246 file(const char *name, const char *mode)
250 if ((fp = fopen(name, mode)) == NULL)
256 obsolete(char *argv[])
259 char *ap, *p, *start;
261 while ((ap = *++argv)) {
262 /* Return if "--" or not an option of any form. */
266 } else if (ap[1] == '-')
268 if (!isdigit((unsigned char)ap[1]))
271 * Digit signifies an old-style option. Malloc space for dash,
272 * new option and argument.
275 if ((start = p = malloc(len + 3)) == NULL)
278 *p++ = ap[0] == '+' ? 's' : 'f';
279 (void)strcpy(p, ap + 1);
287 (void)fprintf(stderr,
288 "usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
292 static size_t wcsicoll_l1_buflen = 0, wcsicoll_l2_buflen = 0;
293 static wchar_t *wcsicoll_l1_buf = NULL, *wcsicoll_l2_buf = NULL;
296 wcsicoll(wchar_t *s1, wchar_t *s2)
300 size_t new_l1_buflen, new_l2_buflen;
304 new_l1_buflen = wcsicoll_l1_buflen;
305 new_l2_buflen = wcsicoll_l2_buflen;
306 while (new_l1_buflen < l1) {
307 if (new_l1_buflen == 0)
308 new_l1_buflen = MAXLINELEN;
312 while (new_l2_buflen < l2) {
313 if (new_l2_buflen == 0)
314 new_l2_buflen = MAXLINELEN;
318 if (new_l1_buflen > wcsicoll_l1_buflen) {
319 wcsicoll_l1_buf = reallocf(wcsicoll_l1_buf, new_l1_buflen * sizeof(*wcsicoll_l1_buf));
320 if (wcsicoll_l1_buf == NULL)
322 wcsicoll_l1_buflen = new_l1_buflen;
324 if (new_l2_buflen > wcsicoll_l2_buflen) {
325 wcsicoll_l2_buf = reallocf(wcsicoll_l2_buf, new_l2_buflen * sizeof(*wcsicoll_l2_buf));
326 if (wcsicoll_l2_buf == NULL)
328 wcsicoll_l2_buflen = new_l2_buflen;
331 for (p = wcsicoll_l1_buf; *s1; s1++)
332 *p++ = towlower(*s1);
334 for (p = wcsicoll_l2_buf; *s2; s2++)
335 *p++ = towlower(*s2);
338 return (wcscoll(wcsicoll_l1_buf, wcsicoll_l2_buf));