This commit is contained in:
Connor Lane Smith 2011-06-02 13:09:30 +01:00
parent 9f9ea07e6c
commit 16a0ee098c
2 changed files with 12 additions and 8 deletions

5
sort.1
View File

@ -3,7 +3,7 @@
sort \- sort lines sort \- sort lines
.SH SYNOPSIS .SH SYNOPSIS
.B sort .B sort
.RB [ \-r ] .RB [ \-ru ]
.RI [ file ...] .RI [ file ...]
.SH DESCRIPTION .SH DESCRIPTION
.B sort .B sort
@ -13,3 +13,6 @@ given, sort reads from stdin.
.TP .TP
.BI \-r .BI \-r
reverses the sort. reverses the sort.
.TP
.BI \-u
prints repeated lines only once.

15
sort.c
View File

@ -11,6 +11,7 @@ static int linecmp(const char **, const char **);
static void getlines(FILE *, const char *); static void getlines(FILE *, const char *);
static bool rflag = false; static bool rflag = false;
static bool uflag = false;
static char **lines = NULL; static char **lines = NULL;
static long nlines = 0; static long nlines = 0;
@ -21,11 +22,14 @@ main(int argc, char *argv[])
long i; long i;
FILE *fp; FILE *fp;
while((c = getopt(argc, argv, "r")) != -1) while((c = getopt(argc, argv, "ru")) != -1)
switch(c) { switch(c) {
case 'r': case 'r':
rflag = true; rflag = true;
break; break;
case 'u':
uflag = true;
break;
default: default:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -39,12 +43,9 @@ main(int argc, char *argv[])
} }
qsort(lines, nlines, sizeof *lines, (int (*)(const void *, const void *))linecmp); qsort(lines, nlines, sizeof *lines, (int (*)(const void *, const void *))linecmp);
for(i = 0; i < nlines; i++) { for(i = 0; i < nlines; i++)
fputs(lines[i], stdout); if(!uflag || i == 0 || strcmp(lines[i], lines[i-1]) != 0)
free(lines[i]); fputs(lines[i], stdout);
}
free(lines);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }