2012-05-21 19:24:28 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 15:02:41 -05:00
|
|
|
#include <ctype.h>
|
2012-05-21 19:24:28 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-03-05 15:35:55 -05:00
|
|
|
|
2012-05-21 19:24:28 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-11 09:56:16 -05:00
|
|
|
static void uniqline(FILE *, char *);
|
|
|
|
static void uniq(FILE *, FILE *);
|
|
|
|
static void uniqfinish(FILE *);
|
2012-05-21 19:24:28 -04:00
|
|
|
|
|
|
|
static const char *countfmt = "";
|
2014-11-13 15:24:47 -05:00
|
|
|
static int dflag = 0;
|
|
|
|
static int uflag = 0;
|
2015-02-11 01:02:54 -05:00
|
|
|
static int fskip = 0;
|
|
|
|
static int sskip = 0;
|
2012-05-21 19:24:28 -04:00
|
|
|
|
2014-01-20 05:47:46 -05:00
|
|
|
static char *prevline = NULL;
|
2015-02-11 01:02:54 -05:00
|
|
|
static char *prevoffset = NULL;
|
2014-01-20 05:47:46 -05:00
|
|
|
static long prevlinecount = 0;
|
2012-05-21 19:24:28 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-11 09:56:16 -05:00
|
|
|
eprintf("usage: %s [-c] [-d | -u] [-f fields] [-s chars]"
|
2015-02-19 09:54:09 -05:00
|
|
|
" [input [output]]\n", argv0);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2012-05-21 19:24:28 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-02-11 09:56:16 -05:00
|
|
|
FILE *fp = stdin, *ofp = stdout;
|
2012-05-21 19:24:28 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'c':
|
|
|
|
countfmt = "%7ld ";
|
|
|
|
break;
|
|
|
|
case 'd':
|
2014-11-13 15:24:47 -05:00
|
|
|
dflag = 1;
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
|
|
|
case 'u':
|
2014-11-13 15:24:47 -05:00
|
|
|
uflag = 1;
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
2015-02-11 01:02:54 -05:00
|
|
|
case 'f':
|
|
|
|
fskip = estrtonum(EARGF(usage()), 0, INT_MAX);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sskip = estrtonum(EARGF(usage()), 0, INT_MAX);
|
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2012-05-21 19:24:28 -04:00
|
|
|
|
2015-03-07 07:29:12 -05:00
|
|
|
if (argc > 2)
|
|
|
|
usage();
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (argc == 0) {
|
2015-02-11 09:56:16 -05:00
|
|
|
uniq(stdin, stdout);
|
|
|
|
} else if (argc >= 1) {
|
|
|
|
if (strcmp(argv[0], "-") && !(fp = fopen(argv[0], "r")))
|
2013-06-14 14:20:47 -04:00
|
|
|
eprintf("fopen %s:", argv[0]);
|
2015-02-11 09:56:16 -05:00
|
|
|
if (argc == 2) {
|
|
|
|
if (strcmp(argv[1], "-") &&
|
|
|
|
!(ofp = fopen(argv[1], "w")))
|
|
|
|
eprintf("fopen %s:", argv[1]);
|
2015-03-07 07:29:12 -05:00
|
|
|
}
|
2015-02-11 09:56:16 -05:00
|
|
|
uniq(fp, ofp);
|
|
|
|
if (fp != stdin)
|
|
|
|
fclose(fp);
|
2012-05-21 19:33:36 -04:00
|
|
|
} else
|
2013-06-14 14:20:47 -04:00
|
|
|
usage();
|
2015-02-11 09:56:16 -05:00
|
|
|
uniqfinish(ofp);
|
|
|
|
if (ofp != stdout)
|
|
|
|
fclose(ofp);
|
2012-05-21 19:24:28 -04:00
|
|
|
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2012-05-21 19:24:28 -04:00
|
|
|
}
|
|
|
|
|
2015-02-11 01:02:54 -05:00
|
|
|
static char *
|
|
|
|
uniqskip(char *l)
|
|
|
|
{
|
|
|
|
char *lo = l;
|
|
|
|
int f = fskip, s = sskip;
|
2015-02-11 07:02:33 -05:00
|
|
|
|
2015-02-11 01:02:54 -05:00
|
|
|
for (; f; --f) {
|
|
|
|
while (isblank(*lo))
|
|
|
|
lo++;
|
|
|
|
while (*lo && !isblank(*lo))
|
|
|
|
lo++;
|
|
|
|
}
|
|
|
|
for (; s && *lo && *lo != '\n'; --s, ++lo);
|
|
|
|
return lo;
|
|
|
|
}
|
|
|
|
|
2014-06-01 08:59:47 -04:00
|
|
|
static void
|
2015-02-11 09:56:16 -05:00
|
|
|
uniqline(FILE *ofp, char *l)
|
2012-05-21 19:24:28 -04:00
|
|
|
{
|
2015-02-11 01:02:54 -05:00
|
|
|
char *loffset = l ? uniqskip(l) : l;
|
|
|
|
|
2014-11-13 16:16:29 -05:00
|
|
|
int linesequel = (!l || !prevline)
|
2014-01-20 05:47:46 -05:00
|
|
|
? l == prevline
|
2015-02-11 01:02:54 -05:00
|
|
|
: !strcmp(loffset, prevoffset);
|
2012-05-21 19:24:28 -04:00
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (linesequel) {
|
2014-01-20 05:47:46 -05:00
|
|
|
++prevlinecount;
|
2012-05-21 19:24:28 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-13 16:16:29 -05:00
|
|
|
if (prevline) {
|
2014-11-13 12:29:30 -05:00
|
|
|
if ((prevlinecount == 1 && !dflag) ||
|
|
|
|
(prevlinecount != 1 && !uflag)) {
|
2015-02-11 09:56:16 -05:00
|
|
|
fprintf(ofp, countfmt, prevlinecount);
|
|
|
|
fputs(prevline, ofp);
|
2012-05-21 19:24:28 -04:00
|
|
|
}
|
2014-01-20 05:47:46 -05:00
|
|
|
free(prevline);
|
2015-02-11 01:02:54 -05:00
|
|
|
prevline = prevoffset = NULL;
|
2012-05-21 19:24:28 -04:00
|
|
|
}
|
|
|
|
|
2015-02-11 01:02:54 -05:00
|
|
|
if (l) {
|
2014-11-16 05:07:26 -05:00
|
|
|
prevline = estrdup(l);
|
2015-02-11 01:02:54 -05:00
|
|
|
prevoffset = prevline + (loffset - l);
|
|
|
|
}
|
2014-01-20 05:47:46 -05:00
|
|
|
prevlinecount = 1;
|
2012-05-21 19:24:28 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 08:59:47 -04:00
|
|
|
static void
|
2015-02-11 09:56:16 -05:00
|
|
|
uniq(FILE *fp, FILE *ofp)
|
2012-05-21 19:24:28 -04:00
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
|
2014-11-18 15:49:30 -05:00
|
|
|
while (getline(&buf, &size, fp) != -1)
|
2015-02-11 09:56:16 -05:00
|
|
|
uniqline(ofp, buf);
|
2012-05-21 19:24:28 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 08:59:47 -04:00
|
|
|
static void
|
2015-02-11 09:56:16 -05:00
|
|
|
uniqfinish(FILE *ofp)
|
2012-05-21 19:24:28 -04:00
|
|
|
{
|
2015-02-11 09:56:16 -05:00
|
|
|
uniqline(ofp, NULL);
|
2012-05-21 19:24:28 -04:00
|
|
|
}
|