Annotation of src/usr.bin/uniq/uniq.c, Revision 1.28
1.28 ! cheloha 1: /* $OpenBSD: uniq.c,v 1.27 2018/07/31 02:55:57 deraadt 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:
1.15 ray 36: #include <ctype.h>
37: #include <err.h>
1.1 deraadt 38: #include <errno.h>
1.15 ray 39: #include <limits.h>
1.24 schwarze 40: #include <locale.h>
1.1 deraadt 41: #include <stdio.h>
42: #include <stdlib.h>
43: #include <string.h>
44: #include <unistd.h>
1.24 schwarze 45: #include <wchar.h>
46: #include <wctype.h>
1.1 deraadt 47:
1.25 tb 48: int cflag, dflag, iflag, uflag;
1.1 deraadt 49: int numchars, numfields, repeats;
50:
1.9 millert 51: FILE *file(char *, char *);
52: void show(FILE *, char *);
53: char *skip(char *);
54: void obsolete(char *[]);
1.15 ray 55: __dead void usage(void);
1.1 deraadt 56:
57: int
1.10 deraadt 58: main(int argc, char *argv[])
1.1 deraadt 59: {
1.28 ! cheloha 60: char *prevline, *t1, *t2, *thisline;
1.10 deraadt 61: FILE *ifp = NULL, *ofp = NULL;
1.28 ! cheloha 62: size_t prevsize, thissize, tmpsize;
1.1 deraadt 63: int ch;
64:
1.24 schwarze 65: setlocale(LC_CTYPE, "");
66:
1.22 deraadt 67: if (pledge("stdio rpath wpath cpath", NULL) == -1)
68: err(1, "pledge");
1.20 deraadt 69:
1.1 deraadt 70: obsolete(argv);
1.25 tb 71: while ((ch = getopt(argc, argv, "cdf:is:u")) != -1) {
1.15 ray 72: const char *errstr;
73:
1.1 deraadt 74: switch (ch) {
75: case 'c':
76: cflag = 1;
77: break;
78: case 'd':
79: dflag = 1;
80: break;
81: case 'f':
1.15 ray 82: numfields = (int)strtonum(optarg, 0, INT_MAX,
83: &errstr);
84: if (errstr)
85: errx(1, "field skip value is %s: %s",
86: errstr, optarg);
1.1 deraadt 87: break;
1.25 tb 88: case 'i':
89: iflag = 1;
90: break;
1.1 deraadt 91: case 's':
1.15 ray 92: numchars = (int)strtonum(optarg, 0, INT_MAX,
93: &errstr);
94: if (errstr)
95: errx(1,
96: "character skip value is %s: %s",
97: errstr, optarg);
1.1 deraadt 98: break;
99: case 'u':
100: uflag = 1;
101: break;
102: default:
103: usage();
1.15 ray 104: }
1.1 deraadt 105: }
106:
1.11 millert 107: argc -= optind;
1.15 ray 108: argv += optind;
1.1 deraadt 109:
1.16 kili 110: /* If neither -d nor -u are set, default is -d -u. */
111: if (!dflag && !uflag)
1.1 deraadt 112: dflag = uflag = 1;
113:
1.27 deraadt 114: switch (argc) {
1.1 deraadt 115: case 0:
116: ifp = stdin;
117: ofp = stdout;
118: break;
119: case 1:
120: ifp = file(argv[0], "r");
121: ofp = stdout;
122: break;
123: case 2:
124: ifp = file(argv[0], "r");
125: ofp = file(argv[1], "w");
126: break;
127: default:
128: usage();
129: }
1.20 deraadt 130:
1.22 deraadt 131: if (pledge("stdio", NULL) == -1)
132: err(1, "pledge");
1.1 deraadt 133:
1.28 ! cheloha 134: prevsize = 0;
! 135: prevline = NULL;
! 136: if (getline(&prevline, &prevsize, ifp) == -1) {
! 137: free(prevline);
! 138: if (ferror(ifp))
! 139: err(1, "getline");
1.1 deraadt 140: exit(0);
1.28 ! cheloha 141: }
! 142:
! 143: thissize = 0;
! 144: thisline = NULL;
! 145: while (getline(&thisline, &thissize, ifp) != -1) {
1.1 deraadt 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. */
1.26 tb 156: if ((iflag ? strcasecmp : strcmp)(t1, t2)) {
1.1 deraadt 157: show(ofp, prevline);
158: t1 = prevline;
159: prevline = thisline;
160: thisline = t1;
1.28 ! cheloha 161: tmpsize = prevsize;
! 162: prevsize = thissize;
! 163: thissize = tmpsize;
1.1 deraadt 164: repeats = 0;
165: } else
166: ++repeats;
167: }
1.28 ! cheloha 168: free(thisline);
! 169: if (ferror(ifp))
! 170: err(1, "getline");
! 171:
1.1 deraadt 172: show(ofp, prevline);
1.28 ! cheloha 173: free(prevline);
! 174:
1.1 deraadt 175: exit(0);
176: }
177:
178: /*
179: * show --
180: * Output a line depending on the flags and number of repetitions
181: * of the line.
182: */
183: void
1.10 deraadt 184: show(FILE *ofp, char *str)
1.1 deraadt 185: {
1.16 kili 186: if ((dflag && repeats) || (uflag && !repeats)) {
187: if (cflag)
188: (void)fprintf(ofp, "%4d %s", repeats + 1, str);
189: else
190: (void)fprintf(ofp, "%s", str);
191: }
1.1 deraadt 192: }
193:
194: char *
1.10 deraadt 195: skip(char *str)
1.1 deraadt 196: {
1.24 schwarze 197: wchar_t wc;
1.16 kili 198: int nchars, nfields;
1.24 schwarze 199: int len;
200: int field_started;
1.1 deraadt 201:
1.16 kili 202: for (nfields = numfields; nfields && *str; nfields--) {
1.24 schwarze 203: /* Skip one field, including preceding blanks. */
204: for (field_started = 0; *str != '\0'; str += len) {
205: if ((len = mbtowc(&wc, str, MB_CUR_MAX)) == -1) {
206: (void)mbtowc(NULL, NULL, MB_CUR_MAX);
207: wc = L'?';
208: len = 1;
209: }
210: if (iswblank(wc)) {
211: if (field_started)
212: break;
213: } else
214: field_started = 1;
215: }
1.16 kili 216: }
1.24 schwarze 217:
218: /* Skip some additional characters. */
219: for (nchars = numchars; nchars-- && *str != '\0'; str += len)
220: if ((len = mblen(str, MB_CUR_MAX)) == -1)
221: len = 1;
222:
1.15 ray 223: return (str);
1.1 deraadt 224: }
225:
226: FILE *
1.10 deraadt 227: file(char *name, char *mode)
1.1 deraadt 228: {
229: FILE *fp;
230:
1.12 millert 231: if (strcmp(name, "-") == 0)
232: return(*mode == 'r' ? stdin : stdout);
1.1 deraadt 233: if ((fp = fopen(name, mode)) == NULL)
1.6 millert 234: err(1, "%s", name);
1.15 ray 235: return (fp);
1.1 deraadt 236: }
237:
238: void
1.10 deraadt 239: obsolete(char *argv[])
1.1 deraadt 240: {
1.15 ray 241: size_t len;
1.1 deraadt 242: char *ap, *p, *start;
243:
1.7 deraadt 244: while ((ap = *++argv)) {
1.1 deraadt 245: /* Return if "--" or not an option of any form. */
246: if (ap[0] != '-') {
247: if (ap[0] != '+')
248: return;
249: } else if (ap[1] == '-')
250: return;
1.23 mmcc 251: if (!isdigit((unsigned char)ap[1]))
1.1 deraadt 252: continue;
253: /*
254: * Digit signifies an old-style option. Malloc space for dash,
255: * new option and argument.
256: */
1.13 deraadt 257: len = strlen(ap) + 3;
258: if ((start = p = malloc(len)) == NULL)
1.5 mickey 259: err(1, "malloc");
1.1 deraadt 260: *p++ = '-';
261: *p++ = ap[0] == '+' ? 's' : 'f';
1.13 deraadt 262: (void)strlcpy(p, ap + 1, len - 2);
1.1 deraadt 263: *argv = start;
264: }
265: }
266:
1.15 ray 267: __dead void
1.10 deraadt 268: usage(void)
1.1 deraadt 269: {
1.15 ray 270: extern char *__progname;
1.16 kili 271:
1.1 deraadt 272: (void)fprintf(stderr,
1.25 tb 273: "usage: %s [-ci] [-d | -u] [-f fields] [-s chars] [input_file [output_file]]\n",
1.15 ray 274: __progname);
1.1 deraadt 275: exit(1);
276: }