xref: /freebsd/usr.bin/uniq/uniq.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
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 const 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 #if 0
45 static char sccsid[] = "@(#)uniq.c	8.3 (Berkeley) 5/4/95";
46 #endif
47 static const char rcsid[] =
48   "$FreeBSD$";
49 #endif /* not lint */
50 
51 #include <ctype.h>
52 #include <err.h>
53 #include <limits.h>
54 #include <locale.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #define	MAXLINELEN	(LINE_MAX + 1)
61 
62 int cflag, dflag, uflag;
63 int numchars, numfields, repeats;
64 
65 FILE	*file(const char *, const char *);
66 char	*getline(char *, size_t, FILE *);
67 void	 show(FILE *, char *);
68 char	*skip(char *);
69 void	 obsolete(char *[]);
70 static void	 usage(void);
71 int      stricoll(char *, char*);
72 
73 int
74 main (argc, argv)
75 	int argc;
76 	char *argv[];
77 {
78 	register char *t1, *t2;
79 	FILE *ifp, *ofp;
80 	int ch;
81 	char *prevline, *thisline, *p;
82 	int iflag = 0, comp;
83 
84 	(void) setlocale(LC_ALL, "");
85 
86 	obsolete(argv);
87 	while ((ch = getopt(argc, argv, "cdif:s:u")) != -1)
88 		switch (ch) {
89 		case 'c':
90 			cflag = 1;
91 			break;
92 		case 'd':
93 			dflag = 1;
94 			break;
95 		case 'i':
96 			iflag = 1;
97 			break;
98 		case 'f':
99 			numfields = strtol(optarg, &p, 10);
100 			if (numfields < 0 || *p)
101 				errx(1, "illegal field skip value: %s", optarg);
102 			break;
103 		case 's':
104 			numchars = strtol(optarg, &p, 10);
105 			if (numchars < 0 || *p)
106 				errx(1, "illegal character skip value: %s", optarg);
107 			break;
108 		case 'u':
109 			uflag = 1;
110 			break;
111 		case '?':
112 		default:
113 			usage();
114 	}
115 
116 	argc -= optind;
117 	argv +=optind;
118 
119 	/* If no flags are set, default is -d -u. */
120 	if (cflag) {
121 		if (dflag || uflag)
122 			usage();
123 	} else if (!dflag && !uflag)
124 		dflag = uflag = 1;
125 
126 	if (argc > 2)
127 		usage();
128 
129 	ifp = stdin;
130 	ofp = stdout;
131 	if (argc > 0 && strcmp(argv[0], "-") != 0)
132 		ifp = file(argv[0], "r");
133 	if (argc > 1)
134 		ofp = file(argv[1], "w");
135 
136 	prevline = malloc(MAXLINELEN);
137 	thisline = malloc(MAXLINELEN);
138 	if (prevline == NULL || thisline == NULL)
139 		err(1, "malloc");
140 
141 	if (getline(prevline, MAXLINELEN, ifp) == NULL)
142 		exit(0);
143 	if (!cflag && uflag && dflag)
144 		show(ofp, prevline);
145 
146 	while (getline(thisline, MAXLINELEN, ifp)) {
147 		/* If requested get the chosen fields + character offsets. */
148 		if (numfields || numchars) {
149 			t1 = skip(thisline);
150 			t2 = skip(prevline);
151 		} else {
152 			t1 = thisline;
153 			t2 = prevline;
154 		}
155 
156 		/* If different, print; set previous to new value. */
157 		if (iflag)
158 			comp = stricoll(t1, t2);
159 		else
160 			comp = strcoll(t1, t2);
161 
162 		if (comp) {
163 			if (cflag || !dflag || !uflag)
164 				show(ofp, prevline);
165 			t1 = prevline;
166 			prevline = thisline;
167 			if (!cflag && uflag && dflag)
168 				show(ofp, prevline);
169 			thisline = t1;
170 			repeats = 0;
171 		} else
172 			++repeats;
173 	}
174 	if (cflag || !dflag || !uflag)
175 		show(ofp, prevline);
176 	exit(0);
177 }
178 
179 char *
180 getline(char *buf, size_t buflen, FILE *fp)
181 {
182 	size_t bufpos;
183 	int ch;
184 
185 	bufpos = 0;
186 	while (bufpos + 2 != buflen && (ch = getc(fp)) != EOF && ch != '\n')
187 		buf[bufpos++] = ch;
188 	if (bufpos + 1 != buflen)
189 		buf[bufpos] = '\0';
190 	while (ch != EOF && ch != '\n')
191 		ch = getc(fp);
192 
193 	return (bufpos != 0 || ch == '\n' ? buf : NULL);
194 }
195 
196 /*
197  * show --
198  *	Output a line depending on the flags and number of repetitions
199  *	of the line.
200  */
201 void
202 show(ofp, str)
203 	FILE *ofp;
204 	char *str;
205 {
206 
207 	if (cflag && *str)
208 		(void)fprintf(ofp, "%4d %s\n", repeats + 1, str);
209 	if ((dflag && repeats) || (uflag && !repeats))
210 		(void)fprintf(ofp, "%s\n", str);
211 }
212 
213 char *
214 skip(str)
215 	register char *str;
216 {
217 	register int nchars, nfields;
218 
219 	for (nfields = 0; *str != '\0' && nfields++ != numfields; ) {
220 		while (isblank((unsigned char)*str))
221 			str++;
222 		while (*str != '\0' && !isblank((unsigned char)*str))
223 			str++;
224 	}
225 	for (nchars = numchars; nchars-- && *str; ++str);
226 	return(str);
227 }
228 
229 FILE *
230 file(name, mode)
231 	const char *name, *mode;
232 {
233 	FILE *fp;
234 
235 	if ((fp = fopen(name, mode)) == NULL)
236 		err(1, "%s", name);
237 	return(fp);
238 }
239 
240 void
241 obsolete(argv)
242 	char *argv[];
243 {
244 	int len;
245 	char *ap, *p, *start;
246 
247 	while ((ap = *++argv)) {
248 		/* Return if "--" or not an option of any form. */
249 		if (ap[0] != '-') {
250 			if (ap[0] != '+')
251 				return;
252 		} else if (ap[1] == '-')
253 			return;
254 		if (!isdigit((unsigned char)ap[1]))
255 			continue;
256 		/*
257 		 * Digit signifies an old-style option.  Malloc space for dash,
258 		 * new option and argument.
259 		 */
260 		len = strlen(ap);
261 		if ((start = p = malloc(len + 3)) == NULL)
262 			err(1, "malloc");
263 		*p++ = '-';
264 		*p++ = ap[0] == '+' ? 's' : 'f';
265 		(void)strcpy(p, ap + 1);
266 		*argv = start;
267 	}
268 }
269 
270 static void
271 usage()
272 {
273 	(void)fprintf(stderr,
274 "usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
275 	exit(1);
276 }
277 
278 int
279 stricoll(s1, s2)
280 	char *s1, *s2;
281 {
282 	char *p, line1[MAXLINELEN], line2[MAXLINELEN];
283 
284 	for (p = line1; *s1; s1++)
285 		*p++ = tolower((unsigned char)*s1);
286 	*p = '\0';
287 	for (p = line2; *s2; s2++)
288 		*p++ = tolower((unsigned char)*s2);
289 	*p = '\0';
290 	return strcoll(line1, line2);
291 }
292