1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Case Larsen.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-08-21 06:51:10 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1989, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-08-21 06:51:10 +00:00
|
|
|
#if 0
|
1997-03-11 13:08:12 +00:00
|
|
|
static char sccsid[] = "@(#)uniq.c 8.3 (Berkeley) 5/4/95";
|
1997-08-21 06:51:10 +00:00
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:08:13 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <ctype.h>
|
1997-08-21 06:51:10 +00:00
|
|
|
#include <err.h>
|
1999-10-24 04:08:15 +00:00
|
|
|
#include <limits.h>
|
1998-03-08 20:56:43 +00:00
|
|
|
#include <locale.h>
|
1997-08-21 06:51:10 +00:00
|
|
|
#include <stdio.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-03-11 13:08:12 +00:00
|
|
|
#include <unistd.h>
|
2004-07-02 23:43:05 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1999-10-24 04:08:15 +00:00
|
|
|
#define MAXLINELEN (LINE_MAX + 1)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int cflag, dflag, uflag;
|
|
|
|
int numchars, numfields, repeats;
|
|
|
|
|
2002-03-22 01:42:45 +00:00
|
|
|
FILE *file(const char *, const char *);
|
2008-02-08 23:04:13 +00:00
|
|
|
wchar_t *getline(wchar_t *, size_t *, FILE *);
|
2004-07-02 23:43:05 +00:00
|
|
|
void show(FILE *, wchar_t *);
|
|
|
|
wchar_t *skip(wchar_t *);
|
2002-03-22 01:42:45 +00:00
|
|
|
void obsolete(char *[]);
|
|
|
|
static void usage(void);
|
2004-07-02 23:43:05 +00:00
|
|
|
int wcsicoll(wchar_t *, wchar_t *);
|
1999-10-24 04:08:15 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
int
|
2002-09-04 23:29:10 +00:00
|
|
|
main (int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-07-02 23:43:05 +00:00
|
|
|
wchar_t *t1, *t2;
|
1994-05-27 12:33:43 +00:00
|
|
|
FILE *ifp, *ofp;
|
2008-02-08 23:04:13 +00:00
|
|
|
int ch, b1;
|
|
|
|
size_t prevbuflen, thisbuflen;
|
2004-07-02 23:43:05 +00:00
|
|
|
wchar_t *prevline, *thisline;
|
|
|
|
char *p;
|
|
|
|
const char *ifn;
|
1997-09-07 15:09:22 +00:00
|
|
|
int iflag = 0, comp;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-04-19 08:16:01 +00:00
|
|
|
(void) setlocale(LC_ALL, "");
|
1998-03-08 20:56:43 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
obsolete(argv);
|
2002-05-29 23:52:55 +00:00
|
|
|
while ((ch = getopt(argc, argv, "cdif:s:u")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
1997-09-07 15:09:22 +00:00
|
|
|
case 'i':
|
|
|
|
iflag = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'f':
|
|
|
|
numfields = strtol(optarg, &p, 10);
|
|
|
|
if (numfields < 0 || *p)
|
1997-08-21 06:51:10 +00:00
|
|
|
errx(1, "illegal field skip value: %s", optarg);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
numchars = strtol(optarg, &p, 10);
|
|
|
|
if (numchars < 0 || *p)
|
1997-08-21 06:51:10 +00:00
|
|
|
errx(1, "illegal character skip value: %s", optarg);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
2007-05-17 00:11:58 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-05-29 23:52:55 +00:00
|
|
|
argc -= optind;
|
2007-05-17 00:18:01 +00:00
|
|
|
argv += optind;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/* If no flags are set, default is -d -u. */
|
|
|
|
if (cflag) {
|
|
|
|
if (dflag || uflag)
|
|
|
|
usage();
|
|
|
|
} else if (!dflag && !uflag)
|
|
|
|
dflag = uflag = 1;
|
|
|
|
|
2002-05-30 00:07:14 +00:00
|
|
|
if (argc > 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
ifp = stdin;
|
2004-07-02 23:43:05 +00:00
|
|
|
ifn = "stdin";
|
2002-05-30 00:07:14 +00:00
|
|
|
ofp = stdout;
|
|
|
|
if (argc > 0 && strcmp(argv[0], "-") != 0)
|
2004-07-02 23:43:05 +00:00
|
|
|
ifp = file(ifn = argv[0], "r");
|
2002-05-30 00:07:14 +00:00
|
|
|
if (argc > 1)
|
1994-05-27 12:33:43 +00:00
|
|
|
ofp = file(argv[1], "w");
|
|
|
|
|
2008-02-08 23:04:13 +00:00
|
|
|
prevbuflen = MAXLINELEN;
|
|
|
|
thisbuflen = MAXLINELEN;
|
|
|
|
prevline = malloc(prevbuflen * sizeof(*prevline));
|
|
|
|
thisline = malloc(thisbuflen * sizeof(*thisline));
|
1994-05-27 12:33:43 +00:00
|
|
|
if (prevline == NULL || thisline == NULL)
|
2002-07-05 09:28:13 +00:00
|
|
|
err(1, "malloc");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2008-02-08 23:04:13 +00:00
|
|
|
if ((prevline = getline(prevline, &prevbuflen, ifp)) == NULL) {
|
2007-05-17 00:19:56 +00:00
|
|
|
if (ferror(ifp))
|
2007-05-17 00:18:01 +00:00
|
|
|
err(1, "%s", ifn);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
2004-07-02 23:43:05 +00:00
|
|
|
}
|
2002-07-05 09:28:13 +00:00
|
|
|
if (!cflag && uflag && dflag)
|
|
|
|
show(ofp, prevline);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2008-02-08 23:04:13 +00:00
|
|
|
while ((thisline = getline(thisline, &thisbuflen, ifp)) != NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
/* If requested get the chosen fields + character offsets. */
|
|
|
|
if (numfields || numchars) {
|
|
|
|
t1 = skip(thisline);
|
|
|
|
t2 = skip(prevline);
|
|
|
|
} else {
|
|
|
|
t1 = thisline;
|
|
|
|
t2 = prevline;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If different, print; set previous to new value. */
|
1997-09-07 15:09:22 +00:00
|
|
|
if (iflag)
|
2004-07-02 23:43:05 +00:00
|
|
|
comp = wcsicoll(t1, t2);
|
1997-09-07 15:09:22 +00:00
|
|
|
else
|
2004-07-02 23:43:05 +00:00
|
|
|
comp = wcscoll(t1, t2);
|
1997-09-07 15:09:22 +00:00
|
|
|
|
|
|
|
if (comp) {
|
2002-07-05 09:28:13 +00:00
|
|
|
if (cflag || !dflag || !uflag)
|
|
|
|
show(ofp, prevline);
|
1994-05-27 12:33:43 +00:00
|
|
|
t1 = prevline;
|
2008-02-08 23:04:13 +00:00
|
|
|
b1 = prevbuflen;
|
1994-05-27 12:33:43 +00:00
|
|
|
prevline = thisline;
|
2008-02-08 23:04:13 +00:00
|
|
|
prevbuflen = thisbuflen;
|
2002-07-05 09:28:13 +00:00
|
|
|
if (!cflag && uflag && dflag)
|
|
|
|
show(ofp, prevline);
|
1994-05-27 12:33:43 +00:00
|
|
|
thisline = t1;
|
2008-02-08 23:04:13 +00:00
|
|
|
thisbuflen = b1;
|
1994-05-27 12:33:43 +00:00
|
|
|
repeats = 0;
|
|
|
|
} else
|
|
|
|
++repeats;
|
|
|
|
}
|
2004-07-02 23:43:05 +00:00
|
|
|
if (ferror(ifp))
|
2007-05-17 00:18:01 +00:00
|
|
|
err(1, "%s", ifn);
|
2002-07-05 09:28:13 +00:00
|
|
|
if (cflag || !dflag || !uflag)
|
|
|
|
show(ofp, prevline);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2004-07-02 23:43:05 +00:00
|
|
|
wchar_t *
|
2008-02-08 23:04:13 +00:00
|
|
|
getline(wchar_t *buf, size_t *buflen, FILE *fp)
|
2002-06-21 07:08:34 +00:00
|
|
|
{
|
|
|
|
size_t bufpos;
|
2004-07-02 23:43:05 +00:00
|
|
|
wint_t ch;
|
2002-06-21 07:08:34 +00:00
|
|
|
|
|
|
|
bufpos = 0;
|
2008-02-08 23:04:13 +00:00
|
|
|
while ((ch = getwc(fp)) != WEOF && ch != '\n') {
|
|
|
|
if (bufpos + 2 >= *buflen) {
|
|
|
|
*buflen = *buflen * 2;
|
|
|
|
buf = reallocf(buf, *buflen * sizeof(*buf));
|
|
|
|
if (buf == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
2002-06-21 07:08:34 +00:00
|
|
|
buf[bufpos++] = ch;
|
2008-02-08 23:04:13 +00:00
|
|
|
}
|
|
|
|
if (bufpos + 1 != *buflen)
|
2002-06-21 07:08:34 +00:00
|
|
|
buf[bufpos] = '\0';
|
|
|
|
|
|
|
|
return (bufpos != 0 || ch == '\n' ? buf : NULL);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* show --
|
|
|
|
* Output a line depending on the flags and number of repetitions
|
|
|
|
* of the line.
|
|
|
|
*/
|
|
|
|
void
|
2004-07-02 23:43:05 +00:00
|
|
|
show(FILE *ofp, wchar_t *str)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
1997-03-11 13:08:12 +00:00
|
|
|
|
2004-09-14 12:01:18 +00:00
|
|
|
if (cflag)
|
2004-07-02 23:43:05 +00:00
|
|
|
(void)fprintf(ofp, "%4d %ls\n", repeats + 1, str);
|
1997-08-21 06:51:10 +00:00
|
|
|
if ((dflag && repeats) || (uflag && !repeats))
|
2004-07-02 23:43:05 +00:00
|
|
|
(void)fprintf(ofp, "%ls\n", str);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2004-07-02 23:43:05 +00:00
|
|
|
wchar_t *
|
|
|
|
skip(wchar_t *str)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-09-04 23:29:10 +00:00
|
|
|
int nchars, nfields;
|
2002-06-21 07:59:31 +00:00
|
|
|
|
|
|
|
for (nfields = 0; *str != '\0' && nfields++ != numfields; ) {
|
2004-07-02 23:43:05 +00:00
|
|
|
while (iswblank(*str))
|
2002-06-21 07:59:31 +00:00
|
|
|
str++;
|
2004-07-02 23:43:05 +00:00
|
|
|
while (*str != '\0' && !iswblank(*str))
|
2002-06-21 07:59:31 +00:00
|
|
|
str++;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
for (nchars = numchars; nchars-- && *str; ++str);
|
|
|
|
return(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
2002-09-04 23:29:10 +00:00
|
|
|
file(const char *name, const char *mode)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if ((fp = fopen(name, mode)) == NULL)
|
1997-08-21 06:51:10 +00:00
|
|
|
err(1, "%s", name);
|
1994-05-27 12:33:43 +00:00
|
|
|
return(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-09-04 23:29:10 +00:00
|
|
|
obsolete(char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char *ap, *p, *start;
|
|
|
|
|
1997-08-21 06:51:10 +00:00
|
|
|
while ((ap = *++argv)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
/* Return if "--" or not an option of any form. */
|
|
|
|
if (ap[0] != '-') {
|
|
|
|
if (ap[0] != '+')
|
|
|
|
return;
|
|
|
|
} else if (ap[1] == '-')
|
|
|
|
return;
|
1998-03-08 20:56:43 +00:00
|
|
|
if (!isdigit((unsigned char)ap[1]))
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* Digit signifies an old-style option. Malloc space for dash,
|
|
|
|
* new option and argument.
|
|
|
|
*/
|
|
|
|
len = strlen(ap);
|
|
|
|
if ((start = p = malloc(len + 3)) == NULL)
|
2002-07-05 09:28:13 +00:00
|
|
|
err(1, "malloc");
|
1994-05-27 12:33:43 +00:00
|
|
|
*p++ = '-';
|
|
|
|
*p++ = ap[0] == '+' ? 's' : 'f';
|
|
|
|
(void)strcpy(p, ap + 1);
|
|
|
|
*argv = start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-08-21 06:51:10 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
2002-06-06 03:05:30 +00:00
|
|
|
"usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2002-06-06 13:44:14 +00:00
|
|
|
|
2008-02-08 23:04:13 +00:00
|
|
|
static size_t wcsicoll_l1_buflen = 0, wcsicoll_l2_buflen = 0;
|
|
|
|
static wchar_t *wcsicoll_l1_buf = NULL, *wcsicoll_l2_buf = NULL;
|
|
|
|
|
2002-06-06 13:44:14 +00:00
|
|
|
int
|
2004-07-02 23:43:05 +00:00
|
|
|
wcsicoll(wchar_t *s1, wchar_t *s2)
|
2002-06-06 13:44:14 +00:00
|
|
|
{
|
2008-02-08 23:04:13 +00:00
|
|
|
wchar_t *p;
|
|
|
|
size_t l1, l2;
|
|
|
|
size_t new_l1_buflen, new_l2_buflen;
|
|
|
|
|
|
|
|
l1 = wcslen(s1) + 1;
|
|
|
|
l2 = wcslen(s2) + 1;
|
|
|
|
new_l1_buflen = wcsicoll_l1_buflen;
|
|
|
|
new_l2_buflen = wcsicoll_l2_buflen;
|
|
|
|
while (new_l1_buflen < l1) {
|
|
|
|
if (new_l1_buflen == 0)
|
|
|
|
new_l1_buflen = MAXLINELEN;
|
|
|
|
else
|
|
|
|
new_l1_buflen *= 2;
|
|
|
|
}
|
|
|
|
while (new_l2_buflen < l2) {
|
|
|
|
if (new_l2_buflen == 0)
|
|
|
|
new_l2_buflen = MAXLINELEN;
|
|
|
|
else
|
|
|
|
new_l2_buflen *= 2;
|
|
|
|
}
|
|
|
|
if (new_l1_buflen > wcsicoll_l1_buflen) {
|
|
|
|
wcsicoll_l1_buf = reallocf(wcsicoll_l1_buf, new_l1_buflen * sizeof(*wcsicoll_l1_buf));
|
|
|
|
if (wcsicoll_l1_buf == NULL)
|
|
|
|
err(1, "reallocf");
|
|
|
|
wcsicoll_l1_buflen = new_l1_buflen;
|
|
|
|
}
|
|
|
|
if (new_l2_buflen > wcsicoll_l2_buflen) {
|
|
|
|
wcsicoll_l2_buf = reallocf(wcsicoll_l2_buf, new_l2_buflen * sizeof(*wcsicoll_l2_buf));
|
|
|
|
if (wcsicoll_l2_buf == NULL)
|
|
|
|
err(1, "reallocf");
|
|
|
|
wcsicoll_l2_buflen = new_l2_buflen;
|
|
|
|
}
|
2002-06-06 13:44:14 +00:00
|
|
|
|
2008-02-08 23:04:13 +00:00
|
|
|
for (p = wcsicoll_l1_buf; *s1; s1++)
|
2004-07-02 23:43:05 +00:00
|
|
|
*p++ = towlower(*s1);
|
2002-06-06 13:44:14 +00:00
|
|
|
*p = '\0';
|
2008-02-08 23:04:13 +00:00
|
|
|
for (p = wcsicoll_l2_buf; *s2; s2++)
|
2004-07-02 23:43:05 +00:00
|
|
|
*p++ = towlower(*s2);
|
2002-06-06 13:44:14 +00:00
|
|
|
*p = '\0';
|
2008-02-08 23:04:13 +00:00
|
|
|
|
|
|
|
return (wcscoll(wcsicoll_l1_buf, wcsicoll_l2_buf));
|
2002-06-06 13:44:14 +00:00
|
|
|
}
|