[hackers] [sbase] Implement nl -w width || sin

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

commit a92a34166a224c3d8ad64dba71623b5eb0f97787
Author: sin <sin_AT_2f30.org>
Date: Fri Feb 20 12:15:43 2015 +0000

    Implement nl -w width

diff --git a/README b/README
index 906991c..33d74dc 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, -w
+= nl no -d, -f, -h, -l, -n, -p
 =* nohup yes none
 #* paste yes none
 =* printenv non-posix none
diff --git a/nl.1 b/nl.1
index 8617c01..21f4ee9 100644
--- a/nl.1
+++ b/nl.1
_AT_@ -10,6 +10,7 @@
 .Op Fl i Ar incr
 .Op Fl s Ar sep
 .Op Fl v Ar startnum
+.Op Fl w Ar width
 .Op Ar file
 .Sh DESCRIPTION
 .Nm
_AT_@ -46,6 +47,11 @@ a tab.
 Start counting from
 .Ar startnum
 instead of the default 1.
+.It Fl w Ar width
+The number of characters to be occupied by the line number
+will be set to
+.Ar width .
+The default is 6.
 .El
 .Sh SEE ALSO
 .Xr pr 1
diff --git a/nl.c b/nl.c
index 530049d..434999a 100644
--- a/nl.c
+++ b/nl.c
_AT_@ -9,6 +9,7 @@
 
 static char mode = 't';
 static const char *sep = "\t";
+static int width = 6;
 static size_t startnum = 1;
 static size_t incr = 1;
 static regex_t preg;
_AT_@ -23,7 +24,7 @@ nl(const char *name, FILE *fp)
                 if ((mode == 'a')
                     || (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
                     || (mode == 't' && buf[0] != '\n')) {
- printf("%6ld%s%s", startnum, sep, buf);
+ printf("%*ld%s%s", width, startnum, sep, buf);
                         startnum += incr;
                 } else {
                         printf(" %s", buf);
_AT_@ -37,7 +38,7 @@ nl(const char *name, FILE *fp)
 static void
 usage(void)
 {
- eprintf("usage: %s [-b type] [-i incr] [-s sep] [-v startnum] [file]\n", argv0);
+ eprintf("usage: %s [-b type] [-i incr] [-s sep] [-v startnum] [-w width] [file]\n", argv0);
 }
 
 int
_AT_@ -64,6 +65,9 @@ main(int argc, char *argv[])
         case 'v':
                 startnum = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
                 break;
+ case 'w':
+ width = estrtonum(EARGF(usage()), 1, INT_MAX);
+ break;
         default:
                 usage();
         } ARGEND;
Received on Fri Feb 20 2015 - 13:16:37 CET

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