From 0143cb2335bb7f7def6c95eee3ce29372ca30388 Mon Sep 17 00:00:00 2001 From: David van Moolenbroek Date: Sat, 10 Oct 2009 22:36:46 +0000 Subject: [PATCH] support for head -n (suggested by xorquewasp) --- commands/simple/head.c | 22 ++++++++++++++++------ man/man1/head.1 | 7 ++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/commands/simple/head.c b/commands/simple/head.c index 3eb73776a..e29f24ae1 100755 --- a/commands/simple/head.c +++ b/commands/simple/head.c @@ -16,15 +16,25 @@ int argc; char *argv[]; { FILE *f; - int n, k, nfiles; + int legacy, n, k, nfiles; char *ptr; - /* Check for flag. Only flag is -n, to say how many lines to print. */ + /* Check for flags. One can only specify how many lines to print. */ k = 1; - ptr = argv[1]; n = DEFAULT; - if (argc > 1 && *ptr++ == '-') { - k++; + legacy = 0; + for (k = 1; k < argc && argv[k][0] == '-'; k++) { + ptr = &argv[k][1]; + if (ptr[0] == 'n' && ptr[1] == 0) { + k++; + if (k >= argc) usage(); + ptr = argv[k]; + } + else if (ptr[0] == '-' && ptr[1] == 0) { + k++; + break; + } + else if (++legacy > 1) usage(); n = atoi(ptr); if (n <= 0) usage(); } @@ -73,6 +83,6 @@ FILE *f; void usage() { - fprintf(stderr, "Usage: head [-n] [file ...]\n"); + fprintf(stderr, "Usage: head [-lines | -n lines] [file ...]\n"); exit(1); } diff --git a/man/man1/head.1 b/man/man1/head.1 index a4acc0542..6e7907bd2 100644 --- a/man/man1/head.1 +++ b/man/man1/head.1 @@ -2,7 +2,7 @@ .SH NAME head \- print the first few lines of a file .SH SYNOPSIS -\fBhead\fR [\fB\-\fIn\fR]\fR [\fIfile\fR] ...\fR +\fBhead\fR [\fB\-\fIn\fR | \fB\-n\fR \fIn\fR]\fR [\fIfile\fR] ...\fR .br .de FL .TP @@ -15,9 +15,10 @@ head \- print the first few lines of a file # \\$2 .. .SH OPTIONS -.FL "\-\fIn\fR" "How many lines to print" +.FL "\-n \fIn\fR" "How many lines to print" +.FL "\-\fIn\fR" "How many lines to print (backwards-compatible)" .SH EXAMPLES -.EX "head \-6" "Print first 6 lines of \fIstdin\fR" +.EX "head \-n 6" "Print first 6 lines of \fIstdin\fR" .EX "head \-1 file1 file2" "Print first line of two files" .SH DESCRIPTION .PP -- 2.44.0