rpc.statd(8): Fix 2 cases where syslog() was passed too many args.
[dragonfly.git] / usr.bin / uniq / uniq.c
blobf506c9c4f562f4f613a916062755fea896b8df3c
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Case Larsen.
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.
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.
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.5 2008/10/16 01:52:34 swildner Exp $
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>
51 #define MAXLINELEN (LINE_MAX + 1)
53 int cflag, dflag, uflag;
54 int numchars, numfields, repeats;
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*);
64 int
65 main(int argc, char **argv)
67 char *t1, *t2;
68 FILE *ifp, *ofp;
69 int ch;
70 char *prevline, *thisline, *p;
71 int iflag = 0, comp;
73 (void) setlocale(LC_ALL, "");
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();
105 argc -= optind;
106 argv +=optind;
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;
115 if (argc > 2)
116 usage();
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");
125 prevline = malloc(MAXLINELEN);
126 thisline = malloc(MAXLINELEN);
127 if (prevline == NULL || thisline == NULL)
128 errx(1, "malloc");
130 if (getline(prevline, MAXLINELEN, ifp) == NULL)
131 exit(0);
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;
143 /* If different, print; set previous to new value. */
144 if (iflag)
145 comp = stricoll(t1, t2);
146 else
147 comp = strcoll(t1, t2);
149 if (comp) {
150 show(ofp, prevline);
151 t1 = prevline;
152 prevline = thisline;
153 thisline = t1;
154 repeats = 0;
155 } else
156 ++repeats;
158 show(ofp, prevline);
159 exit(0);
162 char *
163 getline(char *buf, size_t buflen, FILE *fp)
165 size_t bufpos;
166 int ch = EOF;
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);
176 return (bufpos != 0 || ch == '\n' ? buf : NULL);
180 * show --
181 * Output a line depending on the flags and number of repetitions
182 * of the line.
184 void
185 show(FILE *ofp, char *str)
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);
194 char *
195 skip(char *str)
197 int nchars, nfields;
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++;
205 for (nchars = numchars; nchars-- && *str; ++str);
206 return(str);
209 FILE *
210 file(const char *name, const char *mode)
212 FILE *fp;
214 if ((fp = fopen(name, mode)) == NULL)
215 err(1, "%s", name);
216 return(fp);
219 void
220 obsolete(char **argv)
222 int len;
223 char *ap, *p, *start;
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;
235 * Digit signifies an old-style option. Malloc space for dash,
236 * new option and argument.
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;
248 static void
249 usage(void)
251 (void)fprintf(stderr,
252 "usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
253 exit(1);
257 stricoll(char *s1, char *s2)
259 char *p, line1[MAXLINELEN], line2[MAXLINELEN];
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);