2011-06-02 08:03:34 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int linecmp(const char **, const char **);
|
|
|
|
|
|
|
|
static bool rflag = false;
|
2011-06-02 08:09:30 -04:00
|
|
|
static bool uflag = false;
|
2012-05-21 16:09:44 -04:00
|
|
|
|
|
|
|
static struct linebuf linebuf = EMPTY_LINEBUF;
|
|
|
|
|
2011-06-02 08:03:34 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
long i;
|
|
|
|
FILE *fp;
|
|
|
|
|
2011-06-02 08:09:30 -04:00
|
|
|
while((c = getopt(argc, argv, "ru")) != -1)
|
2011-06-02 08:03:34 -04:00
|
|
|
switch(c) {
|
|
|
|
case 'r':
|
|
|
|
rflag = true;
|
|
|
|
break;
|
2011-06-02 08:09:30 -04:00
|
|
|
case 'u':
|
|
|
|
uflag = true;
|
|
|
|
break;
|
2011-06-02 08:03:34 -04:00
|
|
|
default:
|
2012-05-20 10:38:52 -04:00
|
|
|
exit(2);
|
2011-06-02 08:03:34 -04:00
|
|
|
}
|
|
|
|
if(optind == argc)
|
2012-05-21 16:09:44 -04:00
|
|
|
getlines(stdin, &linebuf);
|
2011-06-02 08:03:34 -04:00
|
|
|
else for(; optind < argc; optind++) {
|
|
|
|
if(!(fp = fopen(argv[optind], "r")))
|
|
|
|
eprintf("fopen %s:", argv[optind]);
|
2012-05-21 16:09:44 -04:00
|
|
|
getlines(fp, &linebuf);
|
2011-06-02 08:03:34 -04:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2012-05-21 16:09:44 -04:00
|
|
|
qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines, (int (*)(const void *, const void *))linecmp);
|
2011-06-02 08:03:34 -04:00
|
|
|
|
2012-05-21 16:09:44 -04:00
|
|
|
for(i = 0; i < linebuf.nlines; i++)
|
|
|
|
if(!uflag || i == 0 || strcmp(linebuf.lines[i], linebuf.lines[i-1]) != 0)
|
|
|
|
fputs(linebuf.lines[i], stdout);
|
2011-06-02 08:03:34 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linecmp(const char **a, const char **b)
|
|
|
|
{
|
|
|
|
return strcmp(*a, *b) * (rflag ? -1 : +1);
|
|
|
|
}
|