support for head -n (suggested by xorquewasp)
This commit is contained in:
parent
fc2634d1ed
commit
0143cb2335
2 changed files with 20 additions and 9 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue