1 /* $NetBSD: uniq.c,v 1.14 2007/12/15 19:44:53 perry 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.14 2007/12/15 19:44:53 perry 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
;
110 /* If no flags are set, default is -d -u. */
114 } else if (!dflag
&& !uflag
)
123 ifp
= file(argv
[0], "r");
127 ifp
= file(argv
[0], "r");
128 ofp
= file(argv
[1], "w");
134 if ((p
= fgetln(ifp
, &psize
)) == NULL
)
136 prevlinesize
= psize
;
137 if ((prevline
= malloc(prevlinesize
+ 1)) == NULL
)
139 (void)memcpy(prevline
, p
, prevlinesize
);
140 prevline
[prevlinesize
] = '\0';
142 thislinesize
= psize
;
143 if ((thisline
= malloc(thislinesize
+ 1)) == NULL
)
146 while ((p
= fgetln(ifp
, &psize
)) != NULL
) {
147 if (psize
> thislinesize
) {
148 if ((thisline
= realloc(thisline
, psize
+ 1)) == NULL
)
150 thislinesize
= psize
;
152 (void)memcpy(thisline
, p
, psize
);
153 thisline
[psize
] = '\0';
155 /* If requested get the chosen fields + character offsets. */
156 if (numfields
|| numchars
) {
164 /* If different, print; set previous to new value. */
165 if (strcmp(t1
, t2
)) {
174 prevlinesize
= thislinesize
;
188 * Output a line depending on the flags and number of repetitions
192 show(FILE *ofp
, const char *str
)
196 (void)fprintf(ofp
, "%4d %s", repeats
+ 1, str
);
197 if ((dflag
&& repeats
) || (uflag
&& !repeats
))
198 (void)fprintf(ofp
, "%s", str
);
202 skip(const char *str
)
204 int infield
, nchars
, nfields
;
206 for (nfields
= numfields
, infield
= 0; nfields
&& *str
; ++str
)
207 if (isspace((unsigned char)*str
)) {
214 for (nchars
= numchars
; nchars
-- && *str
; ++str
)
220 file(const char *name
, const char *mode
)
224 if ((fp
= fopen(name
, mode
)) == NULL
)
230 obsolete(char *argv
[])
232 char *ap
, *p
, *start
;
234 while ((ap
= *++argv
) != NULL
) {
235 /* Return if "--" or not an option of any form. */
239 } else if (ap
[1] == '-')
241 if (!isdigit((unsigned char)ap
[1]))
244 * Digit signifies an old-style option. Malloc space for dash,
245 * new option and argument.
247 (void)asprintf(&p
, "-%c%s", ap
[0] == '+' ? 's' : 'f', ap
+ 1);
258 (void)fprintf(stderr
, "Usage: %s [-c | -du] [-f fields] [-s chars] "
259 "[input [output]]\n", getprogname());