From f31df7b5941dbdc6e6bb5151595ec5f3fe75b0b9 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Fri, 19 Jun 2020 13:17:29 +0800 Subject: [PATCH] head(1): Support '-q' and '-v' flags and improve man page * Support the '-q' flag to never print headers of each filename, and the '-v' flag to always print the headers, which are the GNU extensions to this tool. * Improve the man page to include the 'COMPATIBILITY' and 'STANDARDS' sections. Obtained-from: NetBSD --- usr.bin/head/head.1 | 26 +++++++++++++++++++++----- usr.bin/head/head.c | 17 ++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/usr.bin/head/head.1 b/usr.bin/head/head.1 index 5eca08cef3..b82205d4cc 100644 --- a/usr.bin/head/head.1 +++ b/usr.bin/head/head.1 @@ -27,16 +27,16 @@ .\" .\" @(#)head.1 8.1 (Berkeley) 6/6/93 .\" $FreeBSD: src/usr.bin/head/head.1,v 1.4.2.6 2002/07/15 06:33:12 keramida Exp $ -.\" $DragonFly: src/usr.bin/head/head.1,v 1.3 2007/07/30 22:11:33 swildner Exp $ .\" -.Dd June 6, 1993 +.Dd June 19, 2020 .Dt HEAD 1 .Os .Sh NAME .Nm head -.Nd display first lines of a file +.Nd display the first part of files .Sh SYNOPSIS .Nm +.Op Fl q | Fl v .Op Fl n Ar count | Fl c Ar bytes .Op Ar .Sh DESCRIPTION @@ -50,16 +50,32 @@ If .Ar count is omitted it defaults to 10. .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 +flag is used, each file is preceded by a header consisting of the string .Dq ==> XXX <== where .Dq XXX is the name of the file. +The +.Fl q +flag disables the printing of the header in both cases. .Sh EXIT STATUS .Ex -std +.Sh COMPATIBILITY +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 +The +.Nm +utility conforms to +.St -p1003.2-92 . .Sh HISTORY The .Nm diff --git a/usr.bin/head/head.c b/usr.bin/head/head.c index 9f9b3e0b11..1046448f7e 100644 --- a/usr.bin/head/head.c +++ b/usr.bin/head/head.c @@ -55,11 +55,12 @@ main(int argc, char **argv) int ch; FILE *fp; bool first; + bool qflag = false, vflag = false; int linecnt = -1, bytecnt = -1, eval = 0; char *ep; obsolete(argv); - while ((ch = getopt(argc, argv, "n:c:")) != -1) + while ((ch = getopt(argc, argv, "c:n:qv")) != -1) { switch(ch) { case 'c': bytecnt = strtol(optarg, &ep, 10); @@ -71,9 +72,18 @@ main(int argc, char **argv) if (*ep != 0 || linecnt <= 0) errx(1, "illegal line count -- %s", optarg); break; + case 'q': + qflag = true; + vflag = false; + break; + case 'v': + vflag = true; + qflag = false; + break; default: usage(); } + } argc -= optind; argv += optind; @@ -89,7 +99,7 @@ main(int argc, char **argv) eval = 1; continue; } - if (argc > 1) { + if (vflag || (!qflag && argc > 1)) { printf("%s==> %s <==\n", first ? "" : "\n", *argv); first = false; @@ -163,6 +173,7 @@ obsolete(char **argv) static void __dead2 usage(void) { - fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n"); + fprintf(stderr, + "usage: head [-q | -v] [-n lines | -c bytes] [file ...]\n"); exit(1); } -- 2.41.0