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"
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-03-17 18:59:09 -04:00
|
|
|
static char *prevline = NULL;
|
|
|
|
static char *prevoffset = NULL;
|
2014-01-20 05:47:46 -05:00
|
|
|
static long prevlinecount = 0;
|
2015-03-08 09:41:05 -04:00
|
|
|
static size_t prevlinesiz = 0;
|
2012-05-21 19:24:28 -04:00
|
|
|
|
2015-03-08 09:41:05 -04:00
|
|
|
static const char *
|
|
|
|
uniqskip(const char *l)
|
2015-02-11 01:02:54 -05:00
|
|
|
{
|
2015-03-08 09:41:05 -04:00
|
|
|
const char *lo = l;
|
2015-02-11 01:02:54 -05:00
|
|
|
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);
|
2015-03-17 18:59:09 -04:00
|
|
|
|
2015-02-11 01:02:54 -05:00
|
|
|
return lo;
|
|
|
|
}
|
|
|
|
|
2014-06-01 08:59:47 -04:00
|
|
|
static void
|
2015-03-08 09:41:05 -04:00
|
|
|
uniqline(FILE *ofp, const char *l, size_t len)
|
2012-05-21 19:24:28 -04:00
|
|
|
{
|
2015-03-08 09:41:05 -04:00
|
|
|
const char *loffset = l ? uniqskip(l) : l;
|
2015-02-11 01:02:54 -05:00
|
|
|
|
2015-03-08 09:41:05 -04:00
|
|
|
int linesequel = l && prevoffset &&
|
|
|
|
!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;
|
|
|
|
}
|
|
|
|
|
2015-03-08 09:41:05 -04:00
|
|
|
if (prevoffset) {
|
2014-11-13 12:29:30 -05:00
|
|
|
if ((prevlinecount == 1 && !dflag) ||
|
|
|
|
(prevlinecount != 1 && !uflag)) {
|
2015-03-07 08:04:04 -05:00
|
|
|
if (*countfmt)
|
|
|
|
fprintf(ofp, countfmt, prevlinecount);
|
2015-02-11 09:56:16 -05:00
|
|
|
fputs(prevline, ofp);
|
2012-05-21 19:24:28 -04:00
|
|
|
}
|
2015-03-08 09:41:05 -04:00
|
|
|
prevoffset = NULL;
|
2012-05-21 19:24:28 -04:00
|
|
|
}
|
|
|
|
|
2015-03-27 17:47:15 -04:00
|
|
|
if (l) {
|
|
|
|
if (!prevline || len >= prevlinesiz) {
|
|
|
|
prevlinesiz = len + 1;
|
|
|
|
prevline = erealloc(prevline, prevlinesiz);
|
|
|
|
}
|
|
|
|
memcpy(prevline, l, len);
|
|
|
|
prevline[len] = '\0';
|
|
|
|
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;
|
2015-03-08 09:41:05 -04:00
|
|
|
ssize_t len;
|
2012-05-21 19:24:28 -04:00
|
|
|
|
2015-03-27 09:49:48 -04:00
|
|
|
while ((len = getline(&buf, &size, fp)) > 0)
|
2015-03-08 09:41:05 -04:00
|
|
|
uniqline(ofp, buf, (size_t)len);
|
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-03-08 09:41:05 -04:00
|
|
|
uniqline(ofp, NULL, 0);
|
2012-05-21 19:24:28 -04:00
|
|
|
}
|
2015-03-07 08:02:04 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-c] [-d | -u] [-f fields] [-s chars]"
|
|
|
|
" [input [output]]\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-05-19 09:30:09 -04:00
|
|
|
FILE *fp[2] = { stdin, stdout };
|
2015-05-24 19:33:19 -04:00
|
|
|
int ret = 0, i;
|
2015-05-19 09:30:09 -04:00
|
|
|
char *fname[2] = { "<stdin>", "<stdout>" };
|
2015-03-07 08:02:04 -05:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'c':
|
|
|
|
countfmt = "%7ld ";
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fskip = estrtonum(EARGF(usage()), 0, INT_MAX);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sskip = estrtonum(EARGF(usage()), 0, INT_MAX);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 05:16:49 -05:00
|
|
|
} ARGEND
|
2015-03-07 08:02:04 -05:00
|
|
|
|
|
|
|
if (argc > 2)
|
|
|
|
usage();
|
|
|
|
|
2015-05-19 09:30:09 -04:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "-")) {
|
|
|
|
fname[i] = argv[i];
|
|
|
|
if (!(fp[i] = fopen(argv[i], (i == 0) ? "r" : "w")))
|
|
|
|
eprintf("fopen %s:", argv[i]);
|
2015-05-15 07:28:39 -04:00
|
|
|
}
|
2015-03-17 18:59:09 -04:00
|
|
|
}
|
2015-03-07 08:02:04 -05:00
|
|
|
|
2015-05-19 09:30:09 -04:00
|
|
|
uniq(fp[0], fp[1]);
|
|
|
|
uniqfinish(fp[1]);
|
|
|
|
|
2015-05-24 19:33:19 -04:00
|
|
|
ret |= fshut(fp[0], fname[0]) | fshut(fp[1], fname[1]);
|
|
|
|
|
|
|
|
return ret;
|
2015-03-07 08:02:04 -05:00
|
|
|
}
|