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 * 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.
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
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95";
43 static const char rcsid[] =
47 #include <sys/capsicum.h>
64 static int cflag, dflag, uflag, iflag;
65 static int numchars, numfields, repeats;
67 static FILE *file(const char *, const char *);
68 static wchar_t *convert(const char *);
69 static int inlcmp(const char *, const char *);
70 static void show(FILE *, const char *);
71 static wchar_t *skip(wchar_t *);
72 static void obsolete(char *[]);
73 static void usage(void);
80 * Cache NLS data before entering capability mode.
81 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
83 (void)catopen("libc", NL_CAT_LOCALE);
87 main (int argc, char *argv[])
89 wchar_t *tprev, *tthis;
92 size_t prevbuflen, thisbuflen, b1;
93 char *prevline, *thisline, *p;
97 (void) setlocale(LC_ALL, "");
100 while ((ch = getopt(argc, argv, "cdif:s:u")) != -1)
112 numfields = strtol(optarg, &p, 10);
113 if (numfields < 0 || *p)
114 errx(1, "illegal field skip value: %s", optarg);
117 numchars = strtol(optarg, &p, 10);
118 if (numchars < 0 || *p)
119 errx(1, "illegal character skip value: %s", optarg);
132 /* If no flags are set, default is -d -u. */
136 } else if (!dflag && !uflag)
145 if (argc > 0 && strcmp(argv[0], "-") != 0)
146 ifp = file(ifn = argv[0], "r");
147 cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
148 if (cap_rights_limit(fileno(ifp), &rights) < 0 && errno != ENOSYS)
149 err(1, "unable to limit rights for %s", ifn);
150 cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE);
152 ofp = file(argv[1], "w");
154 cap_rights_set(&rights, CAP_IOCTL);
155 if (cap_rights_limit(fileno(ofp), &rights) < 0 && errno != ENOSYS) {
156 err(1, "unable to limit rights for %s",
157 argc > 1 ? argv[1] : "stdout");
159 if (cap_rights_is_set(&rights, CAP_IOCTL)) {
162 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
164 if (cap_ioctls_limit(fileno(ofp), &cmd, 1) < 0 &&
166 err(1, "unable to limit ioctls for %s",
167 argc > 1 ? argv[1] : "stdout");
172 if (cap_enter() < 0 && errno != ENOSYS)
173 err(1, "unable to enter capability mode");
175 prevbuflen = thisbuflen = 0;
176 prevline = thisline = NULL;
178 if (getline(&prevline, &prevbuflen, ifp) < 0) {
183 tprev = convert(prevline);
185 if (!cflag && uflag && dflag)
189 while (getline(&thisline, &thisbuflen, ifp) >= 0) {
192 tthis = convert(thisline);
194 if (tthis == NULL && tprev == NULL)
195 comp = inlcmp(thisline, prevline);
196 else if (tthis == NULL || tprev == NULL)
199 comp = wcscoll(tthis, tprev);
202 /* If different, print; set previous to new value. */
203 if (cflag || !dflag || !uflag)
208 prevbuflen = thisbuflen;
212 if (!cflag && uflag && dflag)
223 if (cflag || !dflag || !uflag)
229 convert(const char *str)
232 wchar_t *buf, *ret, *p;
234 if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
236 if (SIZE_MAX / sizeof(*buf) < n + 1)
237 errx(1, "conversion buffer length overflow");
238 if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
240 if (mbstowcs(buf, str, n + 1) != n)
241 errx(1, "internal mbstowcs() error");
242 /* The last line may not end with \n. */
243 if (n > 0 && buf[n - 1] == L'\n')
246 /* If requested get the chosen fields + character offsets. */
247 if (numfields || numchars) {
248 if ((ret = wcsdup(skip(buf))) == NULL)
255 for (p = ret; *p != L'\0'; p++)
263 inlcmp(const char *s1, const char *s2)
270 c1 = (unsigned char)*s1;
271 c2 = (unsigned char)*(s2 - 1);
272 /* The last line may not end with \n. */
282 * Output a line depending on the flags and number of repetitions
286 show(FILE *ofp, const char *str)
290 (void)fprintf(ofp, "%4d %s", repeats + 1, str);
291 if ((dflag && repeats) || (uflag && !repeats))
292 (void)fprintf(ofp, "%s", str);
300 for (nfields = 0; *str != L'\0' && nfields++ != numfields; ) {
301 while (iswblank(*str))
303 while (*str != L'\0' && !iswblank(*str))
306 for (nchars = numchars; nchars-- && *str != L'\0'; ++str)
312 file(const char *name, const char *mode)
316 if ((fp = fopen(name, mode)) == NULL)
322 obsolete(char *argv[])
325 char *ap, *p, *start;
327 while ((ap = *++argv)) {
328 /* Return if "--" or not an option of any form. */
332 } else if (ap[1] == '-')
334 if (!isdigit((unsigned char)ap[1]))
337 * Digit signifies an old-style option. Malloc space for dash,
338 * new option and argument.
341 if ((start = p = malloc(len + 3)) == NULL)
344 *p++ = ap[0] == '+' ? 's' : 'f';
345 (void)strcpy(p, ap + 1);
353 (void)fprintf(stderr,
354 "usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");