[hackers] [sbase] Add -n support to sort(1) || sin

From: <git_AT_suckless.org>
Date: Thu, 12 Dec 2013 14:11:15 +0100

commit 544857623ba8692cd8f7ea25cc8f15b906a14951
Author: sin <sin_AT_2f30.org>
Date: Thu Dec 12 13:08:49 2013 +0000

    Add -n support to sort(1)

diff --git a/sort.1 b/sort.1
index 6cbe030..7913357 100644
--- a/sort.1
+++ b/sort.1
_AT_@ -3,7 +3,7 @@
 sort \- sort lines
 .SH SYNOPSIS
 .B sort
-.RB [ \-ru ]
+.RB [ \-nru ]
 .RI [ file ...]
 .SH DESCRIPTION
 .B sort
_AT_@ -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
_AT_@ -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...]
", argv0);
+ eprintf("usage: %s [-nru] [file...]
", argv0);
 }
 
 int
_AT_@ -27,6 +28,9 @@ main(int argc, char *argv[])
         FILE *fp;
 
         ARGBEGIN {
+ case 'n':
+ nflag = true;
+ break;
         case 'r':
                 rflag = true;
                 break;
_AT_@ -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);
 }
 
Received on Thu Dec 12 2013 - 14:11:15 CET

This archive was generated by hypermail 2.3.0 : Thu Dec 12 2013 - 14:12:11 CET