[hackers] [sbase] Implement nl -v startnum || sin

From: <git_AT_suckless.org>
Date: Fri, 20 Feb 2015 13:06:52 +0100 (CET)

commit 1051783f22b10951c76ea93ee64fad20690e5f62
Author: sin <sin_AT_2f30.org>
Date: Fri Feb 20 12:05:54 2015 +0000

    Implement nl -v startnum

diff --git a/README b/README
index ef24d9e..906991c 100644
--- a/README
+++ b/README
_AT_@ -48,7 +48,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
 =* mktemp non-posix none
 =* mv yes none (-i)
 =* nice yes none
-= nl no -d, -f, -h, -l, -n, -p, -v, -w
+= nl no -d, -f, -h, -l, -n, -p, -w
 =* nohup yes none
 #* paste yes none
 =* printenv non-posix none
diff --git a/nl.1 b/nl.1
index 06c5aad..8617c01 100644
--- a/nl.1
+++ b/nl.1
_AT_@ -1,4 +1,4 @@
-.Dd December 4, 2014
+.Dd February 20, 2015
 .Dt NL 1
 .Os sbase
 .Sh NAME
_AT_@ -9,6 +9,7 @@
 .Op Fl b Ar type
 .Op Fl i Ar incr
 .Op Fl s Ar sep
+.Op Fl v Ar startnum
 .Op Ar file
 .Sh DESCRIPTION
 .Nm
_AT_@ -41,6 +42,10 @@ Defines the increment between numbered lines.
 .It Fl s Ar sep
 Defines the string used to separate line numbers and lines. By default this is
 a tab.
+.It Fl v Ar startnum
+Start counting from
+.Ar startnum
+instead of the default 1.
 .El
 .Sh SEE ALSO
 .Xr pr 1
diff --git a/nl.c b/nl.c
index e24050d..530049d 100644
--- a/nl.c
+++ b/nl.c
_AT_@ -9,6 +9,7 @@
 
 static char mode = 't';
 static const char *sep = "\t";
+static size_t startnum = 1;
 static size_t incr = 1;
 static regex_t preg;
 
_AT_@ -16,15 +17,17 @@ void
 nl(const char *name, FILE *fp)
 {
         char *buf = NULL;
- size_t n = 0, size = 0;
+ size_t size = 0;
 
         while (getline(&buf, &size, fp) != -1) {
                 if ((mode == 'a')
                     || (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
- || (mode == 't' && buf[0] != '\n'))
- printf("%6ld%s%s", n += incr, sep, buf);
- else
+ || (mode == 't' && buf[0] != '\n')) {
+ printf("%6ld%s%s", startnum, sep, buf);
+ startnum += incr;
+ } else {
                         printf(" %s", buf);
+ }
         }
         free(buf);
         if (ferror(fp))
_AT_@ -34,7 +37,7 @@ nl(const char *name, FILE *fp)
 static void
 usage(void)
 {
- eprintf("usage: %s [-b type] [-i incr] [-s sep] [file]\n", argv0);
+ eprintf("usage: %s [-b type] [-i incr] [-s sep] [-v startnum] [file]\n", argv0);
 }
 
 int
_AT_@ -58,6 +61,9 @@ main(int argc, char *argv[])
         case 's':
                 sep = EARGF(usage());
                 break;
+ case 'v':
+ startnum = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
+ break;
         default:
                 usage();
         } ARGEND;
Received on Fri Feb 20 2015 - 13:06:52 CET

This archive was generated by hypermail 2.3.0 : Fri Feb 20 2015 - 13:12:08 CET