Implement nl -w width

This commit is contained in:
sin 2015-02-20 12:15:43 +00:00
parent 13e4231f4c
commit 9de3546082
3 changed files with 13 additions and 3 deletions

2
README
View File

@ -48,7 +48,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=* mktemp non-posix none =* mktemp non-posix none
=* mv yes none (-i) =* mv yes none (-i)
=* nice yes none =* nice yes none
= nl no -d, -f, -h, -l, -n, -p, -w = nl no -d, -f, -h, -l, -n, -p
=* nohup yes none =* nohup yes none
#* paste yes none #* paste yes none
=* printenv non-posix none =* printenv non-posix none

6
nl.1
View File

@ -10,6 +10,7 @@
.Op Fl i Ar incr .Op Fl i Ar incr
.Op Fl s Ar sep .Op Fl s Ar sep
.Op Fl v Ar startnum .Op Fl v Ar startnum
.Op Fl w Ar width
.Op Ar file .Op Ar file
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
@ -46,6 +47,11 @@ a tab.
Start counting from Start counting from
.Ar startnum .Ar startnum
instead of the default 1. 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 .El
.Sh SEE ALSO .Sh SEE ALSO
.Xr pr 1 .Xr pr 1

8
nl.c
View File

@ -9,6 +9,7 @@
static char mode = 't'; static char mode = 't';
static const char *sep = "\t"; static const char *sep = "\t";
static int width = 6;
static size_t startnum = 1; static size_t startnum = 1;
static size_t incr = 1; static size_t incr = 1;
static regex_t preg; static regex_t preg;
@ -23,7 +24,7 @@ nl(const char *name, FILE *fp)
if ((mode == 'a') if ((mode == 'a')
|| (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0)) || (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0))
|| (mode == 't' && buf[0] != '\n')) { || (mode == 't' && buf[0] != '\n')) {
printf("%6ld%s%s", startnum, sep, buf); printf("%*ld%s%s", width, startnum, sep, buf);
startnum += incr; startnum += incr;
} else { } else {
printf(" %s", buf); printf(" %s", buf);
@ -37,7 +38,7 @@ nl(const char *name, FILE *fp)
static void static void
usage(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 int
@ -64,6 +65,9 @@ main(int argc, char *argv[])
case 'v': case 'v':
startnum = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX)); startnum = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
break; break;
case 'w':
width = estrtonum(EARGF(usage()), 1, INT_MAX);
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;