xref: /dragonfly/usr.bin/uniq/uniq.c (revision f2a91d318c23c9c19e4dc272cd0207fc9af00577)
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
33  * @(#)uniq.c       8.3 (Berkeley) 5/4/95
34  * $FreeBSD: head/usr.bin/uniq/uniq.c 303526 2016-07-30 01:07:47Z bapt $
35  */
36 
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <limits.h>
41 #include <locale.h>
42 #include <nl_types.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <termios.h>
48 #include <unistd.h>
49 #include <wchar.h>
50 #include <wctype.h>
51 
52 static int cflag, dflag, uflag, iflag;
53 static int numchars, numfields, repeats;
54 
55 static FILE         *file(const char *, const char *);
56 static wchar_t      *convert(const char *);
57 static int           inlcmp(const char *, const char *);
58 static void          show(FILE *, const char *);
59 static wchar_t      *skip(wchar_t *);
60 static void          obsolete(char *[]);
61 static void          usage(void);
62 
63 int
main(int argc,char * argv[])64 main(int argc, char *argv[])
65 {
66           wchar_t *tprev, *tthis;
67           FILE *ifp, *ofp;
68           int ch, comp;
69           size_t prevbuflen, thisbuflen, b1;
70           char *prevline, *thisline, *p;
71           const char *ifn;
72 
73           (void) setlocale(LC_ALL, "");
74 
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();
103                     }
104 
105           argc -= optind;
106           argv += optind;
107 
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;
114 
115           if (argc > 2)
116                     usage();
117 
118           ifp = stdin;
119           ifn = "stdin";
120           ofp = stdout;
121           if (argc > 0 && strcmp(argv[0], "-") != 0)
122                     ifp = file(ifn = argv[0], "r");
123           if (argc > 1)
124                     ofp = file(argv[1], "w");
125 
126           prevbuflen = thisbuflen = 0;
127           prevline = thisline = NULL;
128 
129           if (getline(&prevline, &prevbuflen, ifp) < 0) {
130                     if (ferror(ifp))
131                               err(1, "%s", ifn);
132                     exit(0);
133           }
134           tprev = convert(prevline);
135 
136           if (!cflag && uflag && dflag)
137                     show(ofp, prevline);
138 
139           tthis = NULL;
140           while (getline(&thisline, &thisbuflen, ifp) >= 0) {
141                     if (tthis != NULL)
142                               free(tthis);
143                     tthis = convert(thisline);
144 
145                     if (tthis == NULL && tprev == NULL)
146                               comp = inlcmp(thisline, prevline);
147                     else if (tthis == NULL || tprev == NULL)
148                               comp = 1;
149                     else
150                               comp = wcscoll(tthis, tprev);
151 
152                     if (comp) {
153                               /* If different, print; set previous to new value. */
154                               if (cflag || !dflag || !uflag)
155                                         show(ofp, prevline);
156                               p = prevline;
157                               b1 = prevbuflen;
158                               prevline = thisline;
159                               prevbuflen = thisbuflen;
160                               if (tprev != NULL)
161                                         free(tprev);
162                               tprev = tthis;
163                               if (!cflag && uflag && dflag)
164                                         show(ofp, prevline);
165                               thisline = p;
166                               thisbuflen = b1;
167                               tthis = NULL;
168                               repeats = 0;
169                     } else
170                               ++repeats;
171           }
172           if (ferror(ifp))
173                     err(1, "%s", ifn);
174           if (cflag || !dflag || !uflag)
175                     show(ofp, prevline);
176           exit(0);
177 }
178 
179 static wchar_t *
convert(const char * str)180 convert(const char *str)
181 {
182           size_t n;
183           wchar_t *buf, *ret, *p;
184 
185           if ((n = mbstowcs(NULL, str, 0)) == (size_t)-1)
186                     return (NULL);
187           if (SIZE_MAX / sizeof(*buf) < n + 1)
188                     errx(1, "conversion buffer length overflow");
189           if ((buf = malloc((n + 1) * sizeof(*buf))) == NULL)
190                     err(1, "malloc");
191           if (mbstowcs(buf, str, n + 1) != n)
192                     errx(1, "internal mbstowcs() error");
193           /* The last line may not end with \n. */
194           if (n > 0 && buf[n - 1] == L'\n')
195                     buf[n - 1] = L'\0';
196 
197           /* If requested get the chosen fields + character offsets. */
198           if (numfields || numchars) {
199                     if ((ret = wcsdup(skip(buf))) == NULL)
200                               err(1, "wcsdup");
201                     free(buf);
202           } else
203                     ret = buf;
204 
205           if (iflag) {
206                     for (p = ret; *p != L'\0'; p++)
207                               *p = towlower(*p);
208           }
209 
210           return (ret);
211 }
212 
213 static int
inlcmp(const char * s1,const char * s2)214 inlcmp(const char *s1, const char *s2)
215 {
216           int c1, c2;
217 
218           while (*s1 == *s2++)
219                     if (*s1++ == '\0')
220                               return (0);
221           c1 = (unsigned char)*s1;
222           c2 = (unsigned char)*(s2 - 1);
223           /* The last line may not end with \n. */
224           if (c1 == '\n')
225                     c1 = '\0';
226           if (c2 == '\n')
227                     c2 = '\0';
228           return (c1 - c2);
229 }
230 
231 /*
232  * show --
233  *        Output a line depending on the flags and number of repetitions
234  *        of the line.
235  */
236 static void
show(FILE * ofp,const char * str)237 show(FILE *ofp, const char *str)
238 {
239 
240           if (cflag)
241                     (void)fprintf(ofp, "%4d %s", repeats + 1, str);
242           if ((dflag && repeats) || (uflag && !repeats))
243                     (void)fprintf(ofp, "%s", str);
244 }
245 
246 static wchar_t *
skip(wchar_t * str)247 skip(wchar_t *str)
248 {
249           int nchars, nfields;
250 
251           for (nfields = 0; *str != L'\0' && nfields++ != numfields; ) {
252                     while (iswblank(*str))
253                               str++;
254                     while (*str != L'\0' && !iswblank(*str))
255                               str++;
256           }
257           for (nchars = numchars; nchars-- && *str != L'\0'; ++str)
258                     ;
259           return(str);
260 }
261 
262 static FILE *
file(const char * name,const char * mode)263 file(const char *name, const char *mode)
264 {
265           FILE *fp;
266 
267           if ((fp = fopen(name, mode)) == NULL)
268                     err(1, "%s", name);
269           return(fp);
270 }
271 
272 static void
obsolete(char * argv[])273 obsolete(char *argv[])
274 {
275           int len;
276           char *ap, *p, *start;
277 
278           while ((ap = *++argv)) {
279                     /* Return if "--" or not an option of any form. */
280                     if (ap[0] != '-') {
281                               if (ap[0] != '+')
282                                         return;
283                     } else if (ap[1] == '-')
284                               return;
285                     if (!isdigit((unsigned char)ap[1]))
286                               continue;
287                     /*
288                      * Digit signifies an old-style option.  Malloc space for dash,
289                      * new option and argument.
290                      */
291                     len = strlen(ap);
292                     if ((start = p = malloc(len + 3)) == NULL)
293                               err(1, "malloc");
294                     *p++ = '-';
295                     *p++ = ap[0] == '+' ? 's' : 'f';
296                     (void)strcpy(p, ap + 1);
297                     *argv = start;
298           }
299 }
300 
301 static void
usage(void)302 usage(void)
303 {
304           (void)fprintf(stderr,
305 "usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
306           exit(1);
307 }
308