uniq.c revision 1.11 1 /* $NetBSD: uniq.c,v 1.11 2003/08/07 11:16:56 agc 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.11 2003/08/07 11:16:56 agc 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 int cflag, dflag, uflag;
59 int numchars, numfields, repeats;
60
61 FILE *file __P((char *, char *));
62 int main __P((int, char **));
63 void show __P((FILE *, char *));
64 char *skip __P((char *));
65 void obsolete __P((char *[]));
66 void usage __P((void));
67
68 int
69 main (argc, argv)
70 int argc;
71 char *argv[];
72 {
73 char *t1, *t2;
74 FILE *ifp, *ofp;
75 int ch;
76 char *prevline, *thisline, *p;
77
78 ifp = ofp = NULL;
79 obsolete(argv);
80 while ((ch = getopt(argc, argv, "-cdf:s:u")) != -1)
81 switch (ch) {
82 case '-':
83 --optind;
84 goto done;
85 case 'c':
86 cflag = 1;
87 break;
88 case 'd':
89 dflag = 1;
90 break;
91 case 'f':
92 numfields = strtol(optarg, &p, 10);
93 if (numfields < 0 || *p)
94 errx(1, "illegal field skip value: %s", optarg);
95 break;
96 case 's':
97 numchars = strtol(optarg, &p, 10);
98 if (numchars < 0 || *p)
99 errx(1, "illegal character skip value: %s",
100 optarg);
101 break;
102 case 'u':
103 uflag = 1;
104 break;
105 case '?':
106 default:
107 usage();
108 }
109
110 done: argc -= optind;
111 argv +=optind;
112
113 /* If no flags are set, default is -d -u. */
114 if (cflag) {
115 if (dflag || uflag)
116 usage();
117 } else if (!dflag && !uflag)
118 dflag = uflag = 1;
119
120 switch(argc) {
121 case 0:
122 ifp = stdin;
123 ofp = stdout;
124 break;
125 case 1:
126 ifp = file(argv[0], "r");
127 ofp = stdout;
128 break;
129 case 2:
130 ifp = file(argv[0], "r");
131 ofp = file(argv[1], "w");
132 break;
133 default:
134 usage();
135 }
136
137 prevline = malloc(MAXLINELEN);
138 thisline = malloc(MAXLINELEN);
139 if (prevline == NULL || thisline == NULL)
140 err(1, "malloc");
141
142 if (fgets(prevline, MAXLINELEN, ifp) == NULL)
143 exit(0);
144
145 while (fgets(thisline, MAXLINELEN, ifp)) {
146 /* If requested get the chosen fields + character offsets. */
147 if (numfields || numchars) {
148 t1 = skip(thisline);
149 t2 = skip(prevline);
150 } else {
151 t1 = thisline;
152 t2 = prevline;
153 }
154
155 /* If different, print; set previous to new value. */
156 if (strcmp(t1, t2)) {
157 show(ofp, prevline);
158 t1 = prevline;
159 prevline = thisline;
160 thisline = t1;
161 repeats = 0;
162 } else
163 ++repeats;
164 }
165 show(ofp, prevline);
166 exit(0);
167 }
168
169 /*
170 * show --
171 * Output a line depending on the flags and number of repetitions
172 * of the line.
173 */
174 void
175 show(ofp, str)
176 FILE *ofp;
177 char *str;
178 {
179
180 if (cflag && *str)
181 (void)fprintf(ofp, "%4d %s", repeats + 1, str);
182 if ((dflag && repeats) || (uflag && !repeats))
183 (void)fprintf(ofp, "%s", str);
184 }
185
186 char *
187 skip(str)
188 char *str;
189 {
190 int infield, nchars, nfields;
191
192 for (nfields = numfields, infield = 0; nfields && *str; ++str)
193 if (isspace((unsigned char)*str)) {
194 if (infield) {
195 infield = 0;
196 --nfields;
197 }
198 } else if (!infield)
199 infield = 1;
200 for (nchars = numchars; nchars-- && *str; ++str);
201 return(str);
202 }
203
204 FILE *
205 file(name, mode)
206 char *name, *mode;
207 {
208 FILE *fp;
209
210 if ((fp = fopen(name, mode)) == NULL)
211 err(1, "%s", name);
212 return(fp);
213 }
214
215 void
216 obsolete(argv)
217 char *argv[];
218 {
219 char *ap, *p, *start;
220
221 while ((ap = *++argv) != NULL) {
222 /* Return if "--" or not an option of any form. */
223 if (ap[0] != '-') {
224 if (ap[0] != '+')
225 return;
226 } else if (ap[1] == '-')
227 return;
228 if (!isdigit((unsigned char)ap[1]))
229 continue;
230 /*
231 * Digit signifies an old-style option. Malloc space for dash,
232 * new option and argument.
233 */
234 asprintf(&p, "-%c%s", ap[0] == '+' ? 's' : 'f', ap + 1);
235 if (!p)
236 err(1, "malloc");
237 start = p;
238 *argv = start;
239 }
240 }
241
242 void
243 usage()
244 {
245 (void)fprintf(stderr,
246 "usage: uniq [-c | -du] [-f fields] [-s chars] [input [output]]\n");
247 exit(1);
248 }
249