sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 16a0ee098c95c8b710fde00c5632cd82117992f1
parent 9f9ea07e6cf302dd20adf4d5c7ae817e3535283c
Author: Connor Lane Smith <cls@lubutu.com>
Date:   Thu,  2 Jun 2011 13:09:30 +0100

sort -u
Diffstat:
Msort.1 | 5++++-
Msort.c | 15++++++++-------
2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/sort.1 b/sort.1 @@ -3,7 +3,7 @@ sort \- sort lines .SH SYNOPSIS .B sort -.RB [ \-r ] +.RB [ \-ru ] .RI [ file ...] .SH DESCRIPTION .B sort @@ -13,3 +13,6 @@ given, sort reads from stdin. .TP .BI \-r reverses the sort. +.TP +.BI \-u +prints repeated lines only once. diff --git a/sort.c b/sort.c @@ -11,6 +11,7 @@ static int linecmp(const char **, const char **); static void getlines(FILE *, const char *); static bool rflag = false; +static bool uflag = false; static char **lines = NULL; static long nlines = 0; @@ -21,11 +22,14 @@ main(int argc, char *argv[]) long i; FILE *fp; - while((c = getopt(argc, argv, "r")) != -1) + while((c = getopt(argc, argv, "ru")) != -1) switch(c) { case 'r': rflag = true; break; + case 'u': + uflag = true; + break; default: exit(EXIT_FAILURE); } @@ -39,12 +43,9 @@ main(int argc, char *argv[]) } qsort(lines, nlines, sizeof *lines, (int (*)(const void *, const void *))linecmp); - for(i = 0; i < nlines; i++) { - fputs(lines[i], stdout); - free(lines[i]); - } - free(lines); - + for(i = 0; i < nlines; i++) + if(!uflag || i == 0 || strcmp(lines[i], lines[i-1]) != 0) + fputs(lines[i], stdout); return EXIT_SUCCESS; }