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