2012-05-21 23:24:28 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2013-03-05 20:35:55 +00:00
|
|
|
|
2012-05-21 23:24:28 +00:00
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-01-20 10:47:46 +00:00
|
|
|
static void uniqline(char *);
|
2012-05-21 23:24:28 +00:00
|
|
|
static void uniq(FILE *, const char *);
|
2014-01-20 10:47:46 +00:00
|
|
|
static void uniqfinish(void);
|
2012-05-21 23:24:28 +00:00
|
|
|
|
|
|
|
static const char *countfmt = "";
|
2014-11-13 20:24:47 +00:00
|
|
|
static int dflag = 0;
|
|
|
|
static int uflag = 0;
|
2012-05-21 23:24:28 +00:00
|
|
|
|
2014-01-20 10:47:46 +00:00
|
|
|
static char *prevline = NULL;
|
|
|
|
static long prevlinecount = 0;
|
2012-05-21 23:24:28 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-08-15 09:55:21 +00:00
|
|
|
eprintf("usage: %s [-cdiu] [input]]\n", argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2012-05-21 23:24:28 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
2013-08-15 09:55:21 +00:00
|
|
|
case 'i':
|
|
|
|
eprintf("not implemented\n");
|
2013-06-14 18:20:47 +00:00
|
|
|
case 'c':
|
|
|
|
countfmt = "%7ld ";
|
|
|
|
break;
|
|
|
|
case 'd':
|
2014-11-13 20:24:47 +00:00
|
|
|
dflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
2014-11-13 20:24:47 +00:00
|
|
|
uflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2012-05-21 23:24:28 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc == 0) {
|
2012-05-21 23:24:28 +00:00
|
|
|
uniq(stdin, "<stdin>");
|
2014-11-13 17:29:30 +00:00
|
|
|
} else if (argc == 1) {
|
|
|
|
if (!(fp = fopen(argv[0], "r")))
|
2013-06-14 18:20:47 +00:00
|
|
|
eprintf("fopen %s:", argv[0]);
|
|
|
|
uniq(fp, argv[0]);
|
2012-05-21 23:24:28 +00:00
|
|
|
fclose(fp);
|
2012-05-21 23:33:36 +00:00
|
|
|
} else
|
2013-06-14 18:20:47 +00:00
|
|
|
usage();
|
2014-01-20 10:47:46 +00:00
|
|
|
uniqfinish();
|
2012-05-21 23:24:28 +00:00
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2012-05-21 23:24:28 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2014-01-20 10:47:46 +00:00
|
|
|
uniqline(char *l)
|
2012-05-21 23:24:28 +00:00
|
|
|
{
|
2014-11-13 21:16:29 +00:00
|
|
|
int linesequel = (!l || !prevline)
|
2014-01-20 10:47:46 +00:00
|
|
|
? l == prevline
|
|
|
|
: !strcmp(l, prevline);
|
2012-05-21 23:24:28 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (linesequel) {
|
2014-01-20 10:47:46 +00:00
|
|
|
++prevlinecount;
|
2012-05-21 23:24:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-13 21:16:29 +00:00
|
|
|
if (prevline) {
|
2014-11-13 17:29:30 +00:00
|
|
|
if ((prevlinecount == 1 && !dflag) ||
|
|
|
|
(prevlinecount != 1 && !uflag)) {
|
2014-01-20 10:47:46 +00:00
|
|
|
printf(countfmt, prevlinecount);
|
|
|
|
fputs(prevline, stdout);
|
2012-05-21 23:24:28 +00:00
|
|
|
}
|
2014-01-20 10:47:46 +00:00
|
|
|
free(prevline);
|
|
|
|
prevline = NULL;
|
2012-05-21 23:24:28 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 10:07:26 +00:00
|
|
|
if (l)
|
|
|
|
prevline = estrdup(l);
|
2014-01-20 10:47:46 +00:00
|
|
|
prevlinecount = 1;
|
2012-05-21 23:24:28 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2012-05-21 23:24:28 +00:00
|
|
|
uniq(FILE *fp, const char *str)
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
while (agetline(&buf, &size, fp) != -1)
|
2014-01-20 10:47:46 +00:00
|
|
|
uniqline(buf);
|
2012-05-21 23:24:28 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
|
|
|
uniqfinish(void)
|
2012-05-21 23:24:28 +00:00
|
|
|
{
|
2014-01-20 10:47:46 +00:00
|
|
|
uniqline(NULL);
|
2012-05-21 23:24:28 +00:00
|
|
|
}
|