sbase

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

commit 544857623ba8692cd8f7ea25cc8f15b906a14951
parent 0b6b84886cbf1dc3f26da8c8935da35553b62aed
Author: sin <sin@2f30.org>
Date:   Thu, 12 Dec 2013 13:08:49 +0000

Add -n support to sort(1)

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

diff --git 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 @@ -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); }