From 544857623ba8692cd8f7ea25cc8f15b906a14951 Mon Sep 17 00:00:00 2001 From: sin Date: Thu, 12 Dec 2013 13:08:49 +0000 Subject: [PATCH] Add -n support to sort(1) --- sort.1 | 5 ++++- sort.c | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/sort.1 b/sort.1 index 6cbe030..7913357 100644 --- a/sort.1 +++ b/sort.1 @@ -3,7 +3,7 @@ sort \- sort lines .SH SYNOPSIS .B sort -.RB [ \-ru ] +.RB [ \-nru ] .RI [ file ...] .SH DESCRIPTION .B sort @@ -11,6 +11,9 @@ writes the sorted concatenation of the given files to stdout. If no file is given, sort reads from stdin. .SH OPTIONS .TP +.B \-n +perform a numeric sort. +.TP .B \-r reverses the sort. .TP diff --git a/sort.c b/sort.c index f992423..348e16b 100644 --- a/sort.c +++ b/sort.c @@ -11,13 +11,14 @@ static int linecmp(const char **, const char **); static bool rflag = false; static bool uflag = false; +static bool nflag = false; static struct linebuf linebuf = EMPTY_LINEBUF; static void usage(void) { - eprintf("usage: %s [-ru] [file...]\n", argv0); + eprintf("usage: %s [-nru] [file...]\n", argv0); } int @@ -27,6 +28,9 @@ main(int argc, char *argv[]) FILE *fp; ARGBEGIN { + case 'n': + nflag = true; + break; case 'r': rflag = true; break; @@ -63,6 +67,12 @@ main(int argc, char *argv[]) int linecmp(const char **a, const char **b) { + if (nflag) { + if (rflag) + return strtoul(*b, 0, 10) - strtoul(*a, 0, 10); + else + return strtoul(*a, 0, 10) - strtoul(*b, 0, 10); + } return strcmp(*a, *b) * (rflag ? -1 : +1); }