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