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