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