add support for:

-q quiet; never show filename headers
	-v verbose; always show headers
	-c <bytecount>; count <bytecount> bytes, not lines

as seen in GNU head(1).
This commit is contained in:
mrg 2004-05-04 10:57:42 +00:00
parent 35a3ccf1af
commit 53e1c6f576
2 changed files with 59 additions and 13 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: head.1,v 1.10 2003/08/07 11:14:02 agc Exp $
.\" $NetBSD: head.1,v 1.11 2004/05/04 10:57:42 mrg Exp $
.\"
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\"
.\" from: @(#)head.1 8.1 (Berkeley) 6/6/93
.\"
.Dd July 14, 1993
.Dd May 4, 2004
.Dt HEAD 1
.Os
.Sh NAME
@ -37,7 +37,9 @@
.Nd display first lines of a file
.Sh SYNOPSIS
.Nm
.Op Fl qv
.Op Fl n Ar count
.Op Fl c Ar byte_count
.Op Ar file ...
.Sh DESCRIPTION
This filter displays the first
@ -47,13 +49,20 @@ files are specified.
If
.Ar count
is omitted it defaults to 10.
If
.Ar byte_count
is specified, head counts bytes instead of lines.
.Pp
If more than a single file is specified, each file is preceded by a
header consisting of the string
If more than a single file is specified, or the
.Fl v
option is used, each file is preceded by a header consisting of the string
.Dq ==\*[Gt] XXX \*[Le]=
where
.Dq XXX
is the name of the file.
The
.Fl q
flag disables the printing of the header in all cases.
.Pp
The
.Nm
@ -62,6 +71,9 @@ utility exits 0 on success, and \*[Gt]0 if an error occurs.
The historic command line syntax of
.Nm
is supported by this implementation.
.Pp
This command is mostly compatible with GNU extensions to
.Nm .
.Sh SEE ALSO
.Xr tail 1
.Sh STANDARDS
@ -74,3 +86,10 @@ The
.Nm
utility appeared in
.Bx 3.0 .
It was enhanced to include the
.Fl q ,
.Fl v
and
.Fl c
options for
.Nx 2.1 .

View File

@ -1,4 +1,4 @@
/* $NetBSD: head.c,v 1.17 2004/01/05 23:23:34 jmmv Exp $ */
/* $NetBSD: head.c,v 1.18 2004/05/04 10:57:42 mrg Exp $ */
/*
* Copyright (c) 1980, 1987, 1992, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
#if 0
static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: head.c,v 1.17 2004/01/05 23:23:34 jmmv Exp $");
__RCSID("$NetBSD: head.c,v 1.18 2004/05/04 10:57:42 mrg Exp $");
#endif
#endif /* not lint */
@ -61,7 +61,7 @@ __RCSID("$NetBSD: head.c,v 1.17 2004/01/05 23:23:34 jmmv Exp $");
* Bill Joy UCB August 24, 1977
*/
void head __P((FILE *, long));
void head __P((FILE *, long, long));
void obsolete __P((char *[]));
void usage __P((void));
int main __P((int, char *[]));
@ -77,13 +77,27 @@ main(argc, argv)
FILE *fp;
int first;
long linecnt;
long bytecnt;
char *ep;
int qflag = 0;
int vflag = 0;
(void)setlocale(LC_ALL, "");
obsolete(argv);
linecnt = 10;
while ((ch = getopt(argc, argv, "n:")) != -1)
bytecnt = 0;
while ((ch = getopt(argc, argv, "c:n:qv")) != -1)
switch(ch) {
case 'c':
errno = 0;
bytecnt = strtol(optarg, &ep, 10);
if ((bytecnt == LONG_MIN || bytecnt == LONG_MAX) &&
errno == ERANGE)
err(1, "illegal byte count -- %s", optarg);
else if (*ep || bytecnt <= 0)
errx(1, "illegal byte count -- %s", optarg);
break;
case 'n':
errno = 0;
linecnt = strtol(optarg, &ep, 10);
@ -94,6 +108,16 @@ main(argc, argv)
errx(1, "illegal line count -- %s", optarg);
break;
case 'q':
qflag = 1;
vflag = 0;
break;
case 'v':
qflag = 0;
vflag = 1;
break;
case '?':
default:
usage();
@ -108,31 +132,34 @@ main(argc, argv)
eval = 1;
continue;
}
if (argc > 1) {
if (vflag || (qflag == 0 && argc > 1)) {
(void)printf("%s==> %s <==\n",
first ? "" : "\n", *argv);
first = 0;
}
head(fp, linecnt);
head(fp, linecnt, bytecnt);
(void)fclose(fp);
}
else
head(stdin, linecnt);
head(stdin, linecnt, bytecnt);
exit(eval);
}
void
head(fp, cnt)
head(fp, cnt, bytecnt)
FILE *fp;
long cnt;
long bytecnt;
{
int ch;
if (bytecnt)
cnt = bytecnt;
while (cnt--)
while ((ch = getc(fp)) != EOF) {
if (putchar(ch) == EOF)
err(1, "stdout");
if (ch == '\n')
if (ch == '\n' || bytecnt)
break;
}
}