xref: /freebsd/usr.bin/uniq/uniq.c (revision df7f5d4de4592a8948a25ce01e5bddfbb7ce39dc)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Case Larsen.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 static char sccsid[] = "@(#)uniq.c	8.3 (Berkeley) 5/4/95";
45 #endif /* not lint */
46 
47 #include <errno.h>
48 #include <stdio.h>
49 #include <ctype.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 #define	MAXLINELEN	(8 * 1024)
55 
56 int cflag, dflag, uflag;
57 int numchars, numfields, repeats;
58 
59 void	 err __P((const char *, ...));
60 FILE	*file __P((char *, char *));
61 void	 show __P((FILE *, char *));
62 char	*skip __P((char *));
63 void	 obsolete __P((char *[]));
64 void	 usage __P((void));
65 
66 int
67 main (argc, argv)
68 	int argc;
69 	char *argv[];
70 {
71 	register char *t1, *t2;
72 	FILE *ifp, *ofp;
73 	int ch;
74 	char *prevline, *thisline, *p;
75 
76 	obsolete(argv);
77 	while ((ch = getopt(argc, argv, "-cdf:s:u")) != EOF)
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 				err("illegal field skip value: %s", optarg);
92 			break;
93 		case 's':
94 			numchars = strtol(optarg, &p, 10);
95 			if (numchars < 0 || *p)
96 				err("illegal character skip value: %s", optarg);
97 			break;
98 		case 'u':
99 			uflag = 1;
100 			break;
101 		case '?':
102 		default:
103 			usage();
104 	}
105 
106 done:	argc -= optind;
107 	argv +=optind;
108 
109 	/* If no flags are set, default is -d -u. */
110 	if (cflag) {
111 		if (dflag || uflag)
112 			usage();
113 	} else if (!dflag && !uflag)
114 		dflag = uflag = 1;
115 
116 	switch(argc) {
117 	case 0:
118 		ifp = stdin;
119 		ofp = stdout;
120 		break;
121 	case 1:
122 		ifp = file(argv[0], "r");
123 		ofp = stdout;
124 		break;
125 	case 2:
126 		ifp = file(argv[0], "r");
127 		ofp = file(argv[1], "w");
128 		break;
129 	default:
130 		usage();
131 	}
132 
133 	prevline = malloc(MAXLINELEN);
134 	thisline = malloc(MAXLINELEN);
135 	if (prevline == NULL || thisline == NULL)
136 		err("%s", strerror(errno));
137 
138 	if (fgets(prevline, MAXLINELEN, ifp) == NULL)
139 		exit(0);
140 
141 	while (fgets(thisline, MAXLINELEN, ifp)) {
142 		/* If requested get the chosen fields + character offsets. */
143 		if (numfields || numchars) {
144 			t1 = skip(thisline);
145 			t2 = skip(prevline);
146 		} else {
147 			t1 = thisline;
148 			t2 = prevline;
149 		}
150 
151 		/* If different, print; set previous to new value. */
152 		if (strcmp(t1, t2)) {
153 			show(ofp, prevline);
154 			t1 = prevline;
155 			prevline = thisline;
156 			thisline = t1;
157 			repeats = 0;
158 		} else
159 			++repeats;
160 	}
161 	show(ofp, prevline);
162 	exit(0);
163 }
164 
165 /*
166  * show --
167  *	Output a line depending on the flags and number of repetitions
168  *	of the line.
169  */
170 void
171 show(ofp, str)
172 	FILE *ofp;
173 	char *str;
174 {
175 
176 	if (cflag && *str)
177 		(void)fprintf(ofp, "%4d %s", repeats + 1, str);
178 	if (dflag && repeats || uflag && !repeats)
179 		(void)fprintf(ofp, "%s", str);
180 }
181 
182 char *
183 skip(str)
184 	register char *str;
185 {
186 	register int infield, nchars, nfields;
187 
188 	for (nfields = numfields, infield = 0; nfields && *str; ++str)
189 		if (isspace(*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 	return(str);
198 }
199 
200 FILE *
201 file(name, mode)
202 	char *name, *mode;
203 {
204 	FILE *fp;
205 
206 	if ((fp = fopen(name, mode)) == NULL)
207 		err("%s: %s", name, strerror(errno));
208 	return(fp);
209 }
210 
211 void
212 obsolete(argv)
213 	char *argv[];
214 {
215 	int len;
216 	char *ap, *p, *start;
217 
218 	while (ap = *++argv) {
219 		/* Return if "--" or not an option of any form. */
220 		if (ap[0] != '-') {
221 			if (ap[0] != '+')
222 				return;
223 		} else if (ap[1] == '-')
224 			return;
225 		if (!isdigit(ap[1]))
226 			continue;
227 		/*
228 		 * Digit signifies an old-style option.  Malloc space for dash,
229 		 * new option and argument.
230 		 */
231 		len = strlen(ap);
232 		if ((start = p = malloc(len + 3)) == NULL)
233 			err("%s", strerror(errno));
234 		*p++ = '-';
235 		*p++ = ap[0] == '+' ? 's' : 'f';
236 		(void)strcpy(p, ap + 1);
237 		*argv = start;
238 	}
239 }
240 
241 void
242 usage()
243 {
244 	(void)fprintf(stderr,
245 	    "usage: uniq [-c | -du] [-f fields] [-s chars] [input [output]]\n");
246 	exit(1);
247 }
248 
249 #if __STDC__
250 #include <stdarg.h>
251 #else
252 #include <varargs.h>
253 #endif
254 
255 void
256 #if __STDC__
257 err(const char *fmt, ...)
258 #else
259 err(fmt, va_alist)
260 	char *fmt;
261         va_dcl
262 #endif
263 {
264 	va_list ap;
265 #if __STDC__
266 	va_start(ap, fmt);
267 #else
268 	va_start(ap);
269 #endif
270 	(void)fprintf(stderr, "uniq: ");
271 	(void)vfprintf(stderr, fmt, ap);
272 	va_end(ap);
273 	(void)fprintf(stderr, "\n");
274 	exit(1);
275 	/* NOTREACHED */
276 }
277