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
36 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)uniq.c 8.3 (Berkeley) 5/4/95
38 * $FreeBSD: src/usr.bin/uniq/uniq.c,v 1.11.2.3 2002/06/28 08:02:19 tjr Exp $
39 * $DragonFly: src/usr.bin/uniq/uniq.c,v 1.5 2008/10/16 01:52:34 swildner Exp $
51 #define MAXLINELEN (LINE_MAX + 1)
53 int cflag
, dflag
, uflag
;
54 int numchars
, numfields
, repeats
;
56 FILE *file(const char *, const char *);
57 char *getline(char *, size_t, FILE *);
58 void show(FILE *, char *);
60 void obsolete(char *[]);
61 static void usage(void);
62 int stricoll(char *, char*);
65 main(int argc
, char **argv
)
70 char *prevline
, *thisline
, *p
;
73 (void) setlocale(LC_ALL
, "");
76 while ((ch
= getopt(argc
, argv
, "cdif:s:u")) != -1)
88 numfields
= strtol(optarg
, &p
, 10);
89 if (numfields
< 0 || *p
)
90 errx(1, "illegal field skip value: %s", optarg
);
93 numchars
= strtol(optarg
, &p
, 10);
94 if (numchars
< 0 || *p
)
95 errx(1, "illegal character skip value: %s", optarg
);
108 /* If no flags are set, default is -d -u. */
112 } else if (!dflag
&& !uflag
)
120 if (argc
> 0 && strcmp(argv
[0], "-") != 0)
121 ifp
= file(argv
[0], "r");
123 ofp
= file(argv
[1], "w");
125 prevline
= malloc(MAXLINELEN
);
126 thisline
= malloc(MAXLINELEN
);
127 if (prevline
== NULL
|| thisline
== NULL
)
130 if (getline(prevline
, MAXLINELEN
, ifp
) == NULL
)
133 while (getline(thisline
, MAXLINELEN
, ifp
)) {
134 /* If requested get the chosen fields + character offsets. */
135 if (numfields
|| numchars
) {
143 /* If different, print; set previous to new value. */
145 comp
= stricoll(t1
, t2
);
147 comp
= strcoll(t1
, t2
);
163 getline(char *buf
, size_t buflen
, FILE *fp
)
169 while (bufpos
+ 2 != buflen
&& (ch
= getc(fp
)) != EOF
&& ch
!= '\n')
171 if (bufpos
+ 1 != buflen
)
173 while (ch
!= EOF
&& ch
!= '\n')
176 return (bufpos
!= 0 || ch
== '\n' ? buf
: NULL
);
181 * Output a line depending on the flags and number of repetitions
185 show(FILE *ofp
, char *str
)
189 (void)fprintf(ofp
, "%4d %s\n", repeats
+ 1, str
);
190 if ((dflag
&& repeats
) || (uflag
&& !repeats
))
191 (void)fprintf(ofp
, "%s\n", str
);
199 for (nfields
= 0; *str
!= '\0' && nfields
++ != numfields
; ) {
200 while (isblank((unsigned char)*str
))
202 while (*str
!= '\0' && !isblank((unsigned char)*str
))
205 for (nchars
= numchars
; nchars
-- && *str
; ++str
);
210 file(const char *name
, const char *mode
)
214 if ((fp
= fopen(name
, mode
)) == NULL
)
220 obsolete(char **argv
)
223 char *ap
, *p
, *start
;
225 while ((ap
= *++argv
)) {
226 /* Return if "--" or not an option of any form. */
230 } else if (ap
[1] == '-')
232 if (!isdigit((unsigned char)ap
[1]))
235 * Digit signifies an old-style option. Malloc space for dash,
236 * new option and argument.
239 if ((start
= p
= malloc(len
+ 3)) == NULL
)
242 *p
++ = ap
[0] == '+' ? 's' : 'f';
243 (void)strcpy(p
, ap
+ 1);
251 (void)fprintf(stderr
,
252 "usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
257 stricoll(char *s1
, char *s2
)
259 char *p
, line1
[MAXLINELEN
], line2
[MAXLINELEN
];
261 for (p
= line1
; *s1
; s1
++)
262 *p
++ = tolower((unsigned char)*s1
);
264 for (p
= line2
; *s2
; s2
++)
265 *p
++ = tolower((unsigned char)*s2
);
267 return strcoll(line1
, line2
);