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. 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
32 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)uniq.c 8.3 (Berkeley) 5/4/95
34 * $FreeBSD: src/usr.bin/uniq/uniq.c,v 1.11.2.3 2002/06/28 08:02:19 tjr Exp $
35 * $DragonFly: src/usr.bin/uniq/uniq.c,v 1.5 2008/10/16 01:52:34 swildner Exp $
47 #define MAXLINELEN (LINE_MAX + 1)
49 int cflag
, dflag
, uflag
;
50 int numchars
, numfields
, repeats
;
52 FILE *file(const char *, const char *);
53 char *getline(char *, size_t, FILE *);
54 void show(FILE *, char *);
56 void obsolete(char *[]);
57 static void usage(void);
58 int stricoll(char *, char*);
61 main(int argc
, char **argv
)
66 char *prevline
, *thisline
, *p
;
69 (void) setlocale(LC_ALL
, "");
72 while ((ch
= getopt(argc
, argv
, "cdif:s:u")) != -1)
84 numfields
= strtol(optarg
, &p
, 10);
85 if (numfields
< 0 || *p
)
86 errx(1, "illegal field skip value: %s", optarg
);
89 numchars
= strtol(optarg
, &p
, 10);
90 if (numchars
< 0 || *p
)
91 errx(1, "illegal character skip value: %s", optarg
);
104 /* If no flags are set, default is -d -u. */
108 } else if (!dflag
&& !uflag
)
116 if (argc
> 0 && strcmp(argv
[0], "-") != 0)
117 ifp
= file(argv
[0], "r");
119 ofp
= file(argv
[1], "w");
121 prevline
= malloc(MAXLINELEN
);
122 thisline
= malloc(MAXLINELEN
);
123 if (prevline
== NULL
|| thisline
== NULL
)
126 if (getline(prevline
, MAXLINELEN
, ifp
) == NULL
)
129 while (getline(thisline
, MAXLINELEN
, ifp
)) {
130 /* If requested get the chosen fields + character offsets. */
131 if (numfields
|| numchars
) {
139 /* If different, print; set previous to new value. */
141 comp
= stricoll(t1
, t2
);
143 comp
= strcoll(t1
, t2
);
159 getline(char *buf
, size_t buflen
, FILE *fp
)
165 while (bufpos
+ 2 != buflen
&& (ch
= getc(fp
)) != EOF
&& ch
!= '\n')
167 if (bufpos
+ 1 != buflen
)
169 while (ch
!= EOF
&& ch
!= '\n')
172 return (bufpos
!= 0 || ch
== '\n' ? buf
: NULL
);
177 * Output a line depending on the flags and number of repetitions
181 show(FILE *ofp
, char *str
)
185 (void)fprintf(ofp
, "%4d %s\n", repeats
+ 1, str
);
186 if ((dflag
&& repeats
) || (uflag
&& !repeats
))
187 (void)fprintf(ofp
, "%s\n", str
);
195 for (nfields
= 0; *str
!= '\0' && nfields
++ != numfields
; ) {
196 while (isblank((unsigned char)*str
))
198 while (*str
!= '\0' && !isblank((unsigned char)*str
))
201 for (nchars
= numchars
; nchars
-- && *str
; ++str
);
206 file(const char *name
, const char *mode
)
210 if ((fp
= fopen(name
, mode
)) == NULL
)
216 obsolete(char **argv
)
219 char *ap
, *p
, *start
;
221 while ((ap
= *++argv
)) {
222 /* Return if "--" or not an option of any form. */
226 } else if (ap
[1] == '-')
228 if (!isdigit((unsigned char)ap
[1]))
231 * Digit signifies an old-style option. Malloc space for dash,
232 * new option and argument.
235 if ((start
= p
= malloc(len
+ 3)) == NULL
)
238 *p
++ = ap
[0] == '+' ? 's' : 'f';
239 (void)strcpy(p
, ap
+ 1);
247 (void)fprintf(stderr
,
248 "usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
253 stricoll(char *s1
, char *s2
)
255 char *p
, line1
[MAXLINELEN
], line2
[MAXLINELEN
];
257 for (p
= line1
; *s1
; s1
++)
258 *p
++ = tolower((unsigned char)*s1
);
260 for (p
= line2
; *s2
; s2
++)
261 *p
++ = tolower((unsigned char)*s2
);
263 return strcoll(line1
, line2
);