The diff below makes head(1) recognize `-'
as a name for the standard input,
as many other utilities do.

        Jan

Index: head.1
===================================================================
RCS file: /cvs/src/usr.bin/head/head.1,v
retrieving revision 1.23
diff -u -p -r1.23 head.1
--- head.1      25 Oct 2015 21:50:32 -0000      1.23
+++ head.1      11 Oct 2016 13:33:23 -0000
@@ -47,6 +47,9 @@ utility copies the first
 lines of each specified
 .Ar file
 to the standard output.
+A name of
+.Sq -
+is recognized as standard input.
 If no files are named,
 .Nm
 copies lines from the standard input.
@@ -77,6 +80,16 @@ To display the first 500 lines of the fi
 .Ar foo :
 .Pp
 .Dl $ head -n 500 foo
+.Pp
+To display the first line of
+.Ar foo ,
+the standard input,
+.Ar bar
+and
+.Ar foo
+again:
+.Pp
+.Dl $ head -n 1 foo - bar foo
 .Pp
 .Nm
 can be used in conjunction with
Index: head.c
===================================================================
RCS file: /cvs/src/usr.bin/head/head.c,v
retrieving revision 1.21
diff -u -p -r1.21 head.c
--- head.c      20 Mar 2016 17:14:51 -0000      1.21
+++ head.c      11 Oct 2016 13:33:23 -0000
@@ -93,7 +93,9 @@ main(int argc, char *argv[])
                        if (pledge("stdio", NULL) == -1)
                                err(1, "pledge");
                } else {
-                       if ((fp = fopen(*argv, "r")) == NULL) {
+                       fp = (argv[0][0] == '-' && argv[0][1] == 0)
+                               ? stdin : fopen(*argv, "r");
+                       if (fp == NULL) {
                                warn("%s", *argv++);
                                status = 1;
                                continue;
@@ -101,7 +103,9 @@ main(int argc, char *argv[])
                        if (argc > 1) {
                                if (!firsttime)
                                        putchar('\n');
-                               printf("==> %s <==\n", *argv);
+                               printf("==> %s <==\n",
+                                       (argv[0][0] == '-' && argv[0][1] == 0)
+                                       ? "(stdin)" : *argv);
                        }
                        ++argv;
                }

Reply via email to