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