sbase/uniq.c

141 lines
2.5 KiB
C
Raw Normal View History

2012-05-21 23:24:28 +00:00
/* See LICENSE file for copyright and license details. */
2015-02-14 20:02:41 +00:00
#include <ctype.h>
2012-05-21 23:24:28 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2013-03-05 20:35:55 +00:00
2012-05-21 23:24:28 +00:00
#include "util.h"
static const char *countfmt = "";
static int dflag = 0;
static int uflag = 0;
static int fskip = 0;
static int sskip = 0;
2012-05-21 23:24:28 +00:00
static char *prevline = NULL;
static char *prevoffset = NULL;
2014-01-20 10:47:46 +00:00
static long prevlinecount = 0;
static size_t prevlinesiz = 0;
2012-05-21 23:24:28 +00:00
static const char *
uniqskip(const char *l)
{
const char *lo = l;
int f = fskip, s = sskip;
2015-02-11 12:02:33 +00:00
for (; f; --f) {
while (isblank(*lo))
lo++;
while (*lo && !isblank(*lo))
lo++;
}
for (; s && *lo && *lo != '\n'; --s, ++lo);
return lo;
}
static void
uniqline(FILE *ofp, const char *l, size_t len)
2012-05-21 23:24:28 +00:00
{
const char *loffset = l ? uniqskip(l) : l;
int linesequel = l && prevoffset &&
!strcmp(loffset, prevoffset);
2012-05-21 23:24:28 +00:00
if (linesequel) {
2014-01-20 10:47:46 +00:00
++prevlinecount;
2012-05-21 23:24:28 +00:00
return;
}
if (prevoffset) {
if ((prevlinecount == 1 && !dflag) ||
(prevlinecount != 1 && !uflag)) {
2015-03-07 13:04:04 +00:00
if (*countfmt)
fprintf(ofp, countfmt, prevlinecount);
fputs(prevline, ofp);
2012-05-21 23:24:28 +00:00
}
prevoffset = NULL;
2012-05-21 23:24:28 +00: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 10:47:46 +00:00
prevlinecount = 1;
2012-05-21 23:24:28 +00:00
}
static void
uniq(FILE *fp, FILE *ofp)
2012-05-21 23:24:28 +00:00
{
char *buf = NULL;
size_t size = 0;
ssize_t len;
2012-05-21 23:24:28 +00:00
while ((len = getline(&buf, &size, fp)) >= 0)
uniqline(ofp, buf, (size_t)len);
2012-05-21 23:24:28 +00:00
}
static void
uniqfinish(FILE *ofp)
2012-05-21 23:24:28 +00:00
{
uniqline(ofp, NULL, 0);
2012-05-21 23:24:28 +00:00
}
2015-03-07 13:02:04 +00: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[])
{
FILE *fp = stdin, *ofp = stdout;
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();
} ARGEND;
if (argc > 2)
usage();
if (!argc) {
2015-03-07 13:02:04 +00:00
uniq(stdin, stdout);
} else {
2015-03-07 13:02:04 +00:00
if (strcmp(argv[0], "-") && !(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[0]);
if (argc == 2) {
if (strcmp(argv[1], "-") && !(ofp = fopen(argv[1], "w")))
2015-03-07 13:02:04 +00:00
eprintf("fopen %s:", argv[1]);
}
uniq(fp, ofp);
fclose(fp);
}
2015-03-07 13:02:04 +00:00
uniqfinish(ofp);
fclose(ofp);
2015-03-07 13:02:04 +00:00
return 0;
}