1 /* $NetBSD: uniq.c,v 1.17 2010/10/06 07:59:18 wiz Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95";
45 __RCSID("$NetBSD: uniq.c,v 1.17 2010/10/06 07:59:18 wiz Exp $");
56 static int cflag
, dflag
, uflag
;
57 static int numchars
, numfields
, repeats
;
59 static FILE *file(const char *, const char *);
60 static void show(FILE *, const char *);
61 static const char *skip(const char *);
62 static void obsolete(char *[]);
63 static void usage(void) __dead
;
66 main (int argc
, char *argv
[])
71 char *prevline
, *thisline
, *p
;
72 size_t prevlinesize
, thislinesize
, psize
;
77 while ((ch
= getopt(argc
, argv
, "-cdf:s:u")) != -1)
89 numfields
= strtol(optarg
, &p
, 10);
90 if (numfields
< 0 || *p
)
91 errx(1, "illegal field skip value: %s", optarg
);
94 numchars
= strtol(optarg
, &p
, 10);
95 if (numchars
< 0 || *p
)
96 errx(1, "illegal character skip value: %s",
107 done
: argc
-= optind
;
116 ifp
= file(argv
[0], "r");
120 ifp
= file(argv
[0], "r");
121 ofp
= file(argv
[1], "w");
127 if ((p
= fgetln(ifp
, &psize
)) == NULL
)
129 prevlinesize
= psize
;
130 if ((prevline
= malloc(prevlinesize
+ 1)) == NULL
)
132 (void)memcpy(prevline
, p
, prevlinesize
);
133 prevline
[prevlinesize
] = '\0';
135 thislinesize
= psize
;
136 if ((thisline
= malloc(thislinesize
+ 1)) == NULL
)
139 while ((p
= fgetln(ifp
, &psize
)) != NULL
) {
140 if (psize
> thislinesize
) {
141 if ((thisline
= realloc(thisline
, psize
+ 1)) == NULL
)
143 thislinesize
= psize
;
145 (void)memcpy(thisline
, p
, psize
);
146 thisline
[psize
] = '\0';
148 /* If requested get the chosen fields + character offsets. */
149 if (numfields
|| numchars
) {
157 /* If different, print; set previous to new value. */
158 if (strcmp(t1
, t2
)) {
167 prevlinesize
= thislinesize
;
181 * Output a line depending on the flags and number of repetitions
185 show(FILE *ofp
, const char *str
)
188 if ((dflag
&& repeats
== 0) || (uflag
&& repeats
> 0))
191 (void)fprintf(ofp
, "%4d %s", repeats
+ 1, str
);
193 (void)fprintf(ofp
, "%s", str
);
198 skip(const char *str
)
200 int infield
, nchars
, nfields
;
202 for (nfields
= numfields
, infield
= 0; nfields
&& *str
; ++str
)
203 if (isspace((unsigned char)*str
)) {
210 for (nchars
= numchars
; nchars
-- && *str
; ++str
)
216 file(const char *name
, const char *mode
)
220 if ((fp
= fopen(name
, mode
)) == NULL
)
226 obsolete(char *argv
[])
228 char *ap
, *p
, *start
;
230 while ((ap
= *++argv
) != NULL
) {
231 /* Return if "--" or not an option of any form. */
235 } else if (ap
[1] == '-')
237 if (!isdigit((unsigned char)ap
[1]))
240 * Digit signifies an old-style option. Malloc space for dash,
241 * new option and argument.
243 (void)asprintf(&p
, "-%c%s", ap
[0] == '+' ? 's' : 'f', ap
+ 1);
254 (void)fprintf(stderr
, "Usage: %s [-cdu] [-f fields] [-s chars] "
255 "[input [output]]\n", getprogname());