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[];
|
char *argv[];
|
||||||
{
|
{
|
||||||
FILE *f;
|
FILE *f;
|
||||||
int n, k, nfiles;
|
int legacy, n, k, nfiles;
|
||||||
char *ptr;
|
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;
|
k = 1;
|
||||||
ptr = argv[1];
|
|
||||||
n = DEFAULT;
|
n = DEFAULT;
|
||||||
if (argc > 1 && *ptr++ == '-') {
|
legacy = 0;
|
||||||
k++;
|
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);
|
n = atoi(ptr);
|
||||||
if (n <= 0) usage();
|
if (n <= 0) usage();
|
||||||
}
|
}
|
||||||
|
@ -73,6 +83,6 @@ FILE *f;
|
||||||
|
|
||||||
void usage()
|
void usage()
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Usage: head [-n] [file ...]\n");
|
fprintf(stderr, "Usage: head [-lines | -n lines] [file ...]\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
.SH NAME
|
.SH NAME
|
||||||
head \- print the first few lines of a file
|
head \- print the first few lines of a file
|
||||||
.SH SYNOPSIS
|
.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
|
.br
|
||||||
.de FL
|
.de FL
|
||||||
.TP
|
.TP
|
||||||
|
@ -15,9 +15,10 @@ head \- print the first few lines of a file
|
||||||
# \\$2
|
# \\$2
|
||||||
..
|
..
|
||||||
.SH OPTIONS
|
.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
|
.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"
|
.EX "head \-1 file1 file2" "Print first line of two files"
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
.PP
|
.PP
|
||||||
|
|
Loading…
Reference in a new issue