uniq.c revision 1.12 1 /* $NetBSD: uniq.c,v 1.12 2007/01/05 23:17:32 christos Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Case Larsen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
38 The Regents of the University of California. All rights reserved.\n");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95";
44 #endif
45 __RCSID("$NetBSD: uniq.c,v 1.12 2007/01/05 23:17:32 christos Exp $");
46 #endif /* not lint */
47
48 #include <err.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <ctype.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #define MAXLINELEN (8 * 1024)
57
58 static int cflag, dflag, uflag;
59 static int numchars, numfields, repeats;
60
61 static FILE *file(const char *, const char *);
62 static void show(FILE *, const char *);
63 static const char *skip(const char *);
64 static void obsolete(char *[]);
65 static void usage(void) __attribute__((__noreturn__));
66
67 int
68 main (int argc, char *argv[])
69 {
70 const char *t1, *t2;
71 FILE *ifp, *ofp;
72 int ch;
73 char *prevline, *thisline, *p;
74
75 setprogname(argv[0]);
76 ifp = ofp = NULL;
77 obsolete(argv);
78 while ((ch = getopt(argc, argv, "-cdf:s:u")) != -1)
79 switch (ch) {
80 case '-':
81 --optind;
82 goto done;
83 case 'c':
84 cflag = 1;
85 break;
86 case 'd':
87 dflag = 1;
88 break;
89 case 'f':
90 numfields = strtol(optarg, &p, 10);
91 if (numfields < 0 || *p)
92 errx(1, "illegal field skip value: %s", optarg);
93 break;
94 case 's':
95 numchars = strtol(optarg, &p, 10);
96 if (numchars < 0 || *p)
97 errx(1, "illegal character skip value: %s",
98 optarg);
99 break;
100 case 'u':
101 uflag = 1;
102 break;
103 case '?':
104 default:
105 usage();
106 }
107
108 done: argc -= optind;
109 argv +=optind;
110
111 /* If no flags are set, default is -d -u. */
112 if (cflag) {
113 if (dflag || uflag)
114 usage();
115 } else if (!dflag && !uflag)
116 dflag = uflag = 1;
117
118 switch(argc) {
119 case 0:
120 ifp = stdin;
121 ofp = stdout;
122 break;
123 case 1:
124 ifp = file(argv[0], "r");
125 ofp = stdout;
126 break;
127 case 2:
128 ifp = file(argv[0], "r");
129 ofp = file(argv[1], "w");
130 break;
131 default:
132 usage();
133 }
134
135 prevline = malloc(MAXLINELEN);
136 thisline = malloc(MAXLINELEN);
137 if (prevline == NULL || thisline == NULL)
138 err(1, "malloc");
139
140 if (fgets(prevline, MAXLINELEN, ifp) == NULL)
141 return 0;
142
143 while (fgets(thisline, MAXLINELEN, ifp)) {
144 /* If requested get the chosen fields + character offsets. */
145 if (numfields || numchars) {
146 t1 = skip(thisline);
147 t2 = skip(prevline);
148 } else {
149 t1 = thisline;
150 t2 = prevline;
151 }
152
153 /* If different, print; set previous to new value. */
154 if (strcmp(t1, t2)) {
155 char *t;
156 show(ofp, prevline);
157 t = prevline;
158 prevline = thisline;
159 thisline = t;
160 repeats = 0;
161 } else
162 ++repeats;
163 }
164 show(ofp, prevline);
165 return 0;
166 }
167
168 /*
169 * show --
170 * Output a line depending on the flags and number of repetitions
171 * of the line.
172 */
173 static void
174 show(FILE *ofp, const char *str)
175 {
176
177 if (cflag && *str)
178 (void)fprintf(ofp, "%4d %s", repeats + 1, str);
179 if ((dflag && repeats) || (uflag && !repeats))
180 (void)fprintf(ofp, "%s", str);
181 }
182
183 static const char *
184 skip(const char *str)
185 {
186 int infield, nchars, nfields;
187
188 for (nfields = numfields, infield = 0; nfields && *str; ++str)
189 if (isspace((unsigned char)*str)) {
190 if (infield) {
191 infield = 0;
192 --nfields;
193 }
194 } else if (!infield)
195 infield = 1;
196 for (nchars = numchars; nchars-- && *str; ++str)
197 continue;
198 return str;
199 }
200
201 static FILE *
202 file(const char *name, const char *mode)
203 {
204 FILE *fp;
205
206 if ((fp = fopen(name, mode)) == NULL)
207 err(1, "%s", name);
208 return(fp);
209 }
210
211 static void
212 obsolete(char *argv[])
213 {
214 char *ap, *p, *start;
215
216 while ((ap = *++argv) != NULL) {
217 /* Return if "--" or not an option of any form. */
218 if (ap[0] != '-') {
219 if (ap[0] != '+')
220 return;
221 } else if (ap[1] == '-')
222 return;
223 if (!isdigit((unsigned char)ap[1]))
224 continue;
225 /*
226 * Digit signifies an old-style option. Malloc space for dash,
227 * new option and argument.
228 */
229 (void)asprintf(&p, "-%c%s", ap[0] == '+' ? 's' : 'f', ap + 1);
230 if (!p)
231 err(1, "malloc");
232 start = p;
233 *argv = start;
234 }
235 }
236
237 static void
238 usage(void)
239 {
240 (void)fprintf(stderr, "Usage: %s [-c | -du] [-f fields] [-s chars] "
241 "[input [output]]\n", getprogname());
242 exit(1);
243 }
244