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[] =
48 "$FreeBSD: src/usr.bin/uniq/uniq.c,v 1.11.2.3 2002/06/28 08:02:19 tjr Exp $";
60 #define MAXLINELEN (LINE_MAX + 1)
62 int cflag, dflag, uflag;
63 int numchars, numfields, repeats;
65 FILE *file(const char *, const char *);
66 char *getline(char *, size_t, FILE *);
67 void show(FILE *, char *);
69 void obsolete(char *[]);
70 static void usage(void);
71 int stricoll(char *, char*);
78 register char *t1, *t2;
81 char *prevline, *thisline, *p;
84 (void) setlocale(LC_ALL, "");
87 while ((ch = getopt(argc, argv, "cdif:s:u")) != -1)
99 numfields = strtol(optarg, &p, 10);
100 if (numfields < 0 || *p)
101 errx(1, "illegal field skip value: %s", optarg);
104 numchars = strtol(optarg, &p, 10);
105 if (numchars < 0 || *p)
106 errx(1, "illegal character skip value: %s", optarg);
119 /* If no flags are set, default is -d -u. */
123 } else if (!dflag && !uflag)
131 if (argc > 0 && strcmp(argv[0], "-") != 0)
132 ifp = file(argv[0], "r");
134 ofp = file(argv[1], "w");
136 prevline = malloc(MAXLINELEN);
137 thisline = malloc(MAXLINELEN);
138 if (prevline == NULL || thisline == NULL)
141 if (getline(prevline, MAXLINELEN, ifp) == NULL)
144 while (getline(thisline, MAXLINELEN, ifp)) {
145 /* If requested get the chosen fields + character offsets. */
146 if (numfields || numchars) {
154 /* If different, print; set previous to new value. */
156 comp = stricoll(t1, t2);
158 comp = strcoll(t1, t2);
174 getline(char *buf, size_t buflen, FILE *fp)
180 while (bufpos + 2 != buflen && (ch = getc(fp)) != EOF && ch != '\n')
182 if (bufpos + 1 != buflen)
184 while (ch != EOF && ch != '\n')
187 return (bufpos != 0 || ch == '\n' ? buf : NULL);
192 * Output a line depending on the flags and number of repetitions
202 (void)fprintf(ofp, "%4d %s\n", repeats + 1, str);
203 if ((dflag && repeats) || (uflag && !repeats))
204 (void)fprintf(ofp, "%s\n", str);
211 register int nchars, nfields;
213 for (nfields = 0; *str != '\0' && nfields++ != numfields; ) {
214 while (isblank((unsigned char)*str))
216 while (*str != '\0' && !isblank((unsigned char)*str))
219 for (nchars = numchars; nchars-- && *str; ++str);
225 const char *name, *mode;
229 if ((fp = fopen(name, mode)) == NULL)
239 char *ap, *p, *start;
241 while ((ap = *++argv)) {
242 /* Return if "--" or not an option of any form. */
246 } else if (ap[1] == '-')
248 if (!isdigit((unsigned char)ap[1]))
251 * Digit signifies an old-style option. Malloc space for dash,
252 * new option and argument.
255 if ((start = p = malloc(len + 3)) == NULL)
258 *p++ = ap[0] == '+' ? 's' : 'f';
259 (void)strcpy(p, ap + 1);
267 (void)fprintf(stderr,
268 "usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
276 char *p, line1[MAXLINELEN], line2[MAXLINELEN];
278 for (p = line1; *s1; s1++)
279 *p++ = tolower((unsigned char)*s1);
281 for (p = line2; *s2; s2++)
282 *p++ = tolower((unsigned char)*s2);
284 return strcoll(line1, line2);