Annotation of src/usr.bin/uniq/uniq.c, Revision 1.15
1.15 ! ray 1: /* $OpenBSD: uniq.c,v 1.14 2003/06/03 02:56:21 millert Exp $ */
1.1 deraadt 2: /* $NetBSD: uniq.c,v 1.7 1995/08/31 22:03:48 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1989, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Case Larsen.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.14 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: static char copyright[] =
38: "@(#) Copyright (c) 1989, 1993\n\
39: The Regents of the University of California. All rights reserved.\n";
40: #endif /* not lint */
41:
42: #ifndef lint
43: #if 0
44: static char sccsid[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95";
45: #endif
1.15 ! ray 46: static char rcsid[] = "$OpenBSD: uniq.c,v 1.14 2003/06/03 02:56:21 millert Exp $";
1.1 deraadt 47: #endif /* not lint */
48:
1.15 ! ray 49: #include <ctype.h>
! 50: #include <err.h>
1.1 deraadt 51: #include <errno.h>
1.15 ! ray 52: #include <limits.h>
1.1 deraadt 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:
1.9 millert 63: FILE *file(char *, char *);
64: void show(FILE *, char *);
65: char *skip(char *);
66: void obsolete(char *[]);
1.15 ! ray 67: __dead void usage(void);
1.1 deraadt 68:
69: int
1.10 deraadt 70: main(int argc, char *argv[])
1.1 deraadt 71: {
1.8 mpech 72: char *t1, *t2;
1.10 deraadt 73: FILE *ifp = NULL, *ofp = NULL;
1.1 deraadt 74: int ch;
1.15 ! ray 75: char *prevline, *thisline;
1.1 deraadt 76:
77: obsolete(argv);
1.15 ! ray 78: while ((ch = getopt(argc, argv, "cdf:s:u")) != -1) {
! 79: const char *errstr;
! 80:
1.1 deraadt 81: switch (ch) {
82: case 'c':
83: cflag = 1;
84: break;
85: case 'd':
86: dflag = 1;
87: break;
88: case 'f':
1.15 ! ray 89: numfields = (int)strtonum(optarg, 0, INT_MAX,
! 90: &errstr);
! 91: if (errstr)
! 92: errx(1, "field skip value is %s: %s",
! 93: errstr, optarg);
1.1 deraadt 94: break;
95: case 's':
1.15 ! ray 96: numchars = (int)strtonum(optarg, 0, INT_MAX,
! 97: &errstr);
! 98: if (errstr)
! 99: errx(1,
! 100: "character skip value is %s: %s",
! 101: errstr, optarg);
1.1 deraadt 102: break;
103: case 'u':
104: uflag = 1;
105: break;
106: default:
107: usage();
1.15 ! ray 108: }
1.1 deraadt 109: }
110:
1.11 millert 111: argc -= optind;
1.15 ! ray 112: argv += optind;
1.1 deraadt 113:
114: /* If no flags are set, default is -d -u. */
115: if (cflag) {
116: if (dflag || uflag)
117: usage();
118: } else if (!dflag && !uflag)
119: dflag = uflag = 1;
120:
121: switch(argc) {
122: case 0:
123: ifp = stdin;
124: ofp = stdout;
125: break;
126: case 1:
127: ifp = file(argv[0], "r");
128: ofp = stdout;
129: break;
130: case 2:
131: ifp = file(argv[0], "r");
132: ofp = file(argv[1], "w");
133: break;
134: default:
135: usage();
136: }
137:
138: prevline = malloc(MAXLINELEN);
139: thisline = malloc(MAXLINELEN);
140: if (prevline == NULL || thisline == NULL)
1.5 mickey 141: err(1, "malloc");
1.1 deraadt 142:
143: if (fgets(prevline, MAXLINELEN, ifp) == NULL)
144: exit(0);
145:
146: while (fgets(thisline, MAXLINELEN, ifp)) {
147: /* If requested get the chosen fields + character offsets. */
148: if (numfields || numchars) {
149: t1 = skip(thisline);
150: t2 = skip(prevline);
151: } else {
152: t1 = thisline;
153: t2 = prevline;
154: }
155:
156: /* If different, print; set previous to new value. */
157: if (strcmp(t1, t2)) {
158: show(ofp, prevline);
159: t1 = prevline;
160: prevline = thisline;
161: thisline = t1;
162: repeats = 0;
163: } else
164: ++repeats;
165: }
166: show(ofp, prevline);
167: exit(0);
168: }
169:
170: /*
171: * show --
172: * Output a line depending on the flags and number of repetitions
173: * of the line.
174: */
175: void
1.10 deraadt 176: show(FILE *ofp, char *str)
1.1 deraadt 177: {
178:
179: if (cflag && *str)
180: (void)fprintf(ofp, "%4d %s", repeats + 1, str);
1.15 ! ray 181: if ((dflag && repeats) || (uflag && !repeats))
1.1 deraadt 182: (void)fprintf(ofp, "%s", str);
183: }
184:
185: char *
1.10 deraadt 186: skip(char *str)
1.1 deraadt 187: {
1.8 mpech 188: int infield, nchars, nfields;
1.1 deraadt 189:
190: for (nfields = numfields, infield = 0; nfields && *str; ++str)
191: if (isspace(*str)) {
192: if (infield) {
193: infield = 0;
194: --nfields;
195: }
196: } else if (!infield)
197: infield = 1;
1.15 ! ray 198: for (nchars = numchars; nchars-- && *str; ++str)
! 199: ;
! 200: return (str);
1.1 deraadt 201: }
202:
203: FILE *
1.10 deraadt 204: file(char *name, char *mode)
1.1 deraadt 205: {
206: FILE *fp;
207:
1.12 millert 208: if (strcmp(name, "-") == 0)
209: return(*mode == 'r' ? stdin : stdout);
1.1 deraadt 210: if ((fp = fopen(name, mode)) == NULL)
1.6 millert 211: err(1, "%s", name);
1.15 ! ray 212: return (fp);
1.1 deraadt 213: }
214:
215: void
1.10 deraadt 216: obsolete(char *argv[])
1.1 deraadt 217: {
1.15 ! ray 218: size_t len;
1.1 deraadt 219: char *ap, *p, *start;
220:
1.7 deraadt 221: while ((ap = *++argv)) {
1.1 deraadt 222: /* Return if "--" or not an option of any form. */
223: if (ap[0] != '-') {
224: if (ap[0] != '+')
225: return;
226: } else if (ap[1] == '-')
227: return;
228: if (!isdigit(ap[1]))
229: continue;
230: /*
231: * Digit signifies an old-style option. Malloc space for dash,
232: * new option and argument.
233: */
1.13 deraadt 234: len = strlen(ap) + 3;
235: if ((start = p = malloc(len)) == NULL)
1.5 mickey 236: err(1, "malloc");
1.1 deraadt 237: *p++ = '-';
238: *p++ = ap[0] == '+' ? 's' : 'f';
1.13 deraadt 239: (void)strlcpy(p, ap + 1, len - 2);
1.1 deraadt 240: *argv = start;
241: }
242: }
243:
1.15 ! ray 244: __dead void
1.10 deraadt 245: usage(void)
1.1 deraadt 246: {
1.15 ! ray 247: extern char *__progname;
! 248:
1.1 deraadt 249: (void)fprintf(stderr,
1.15 ! ray 250: "usage: %s [-c | -d | -u] [-f fields] [-s chars] [input_file [output_file]]\n",
! 251: __progname);
1.1 deraadt 252: exit(1);
253: }