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