From acc3b7f02a1e7877276a3d5786ccd58e2211ae2b Mon Sep 17 00:00:00 2001 From: nicm Date: Sat, 30 May 2015 06:25:35 +0000 Subject: [PATCH] Support - to read from stdin, from Sebastien Marie. --- usr.bin/file/file.1 | 11 +++++++++-- usr.bin/file/file.c | 20 ++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/usr.bin/file/file.1 b/usr.bin/file/file.1 index 24eaa056e67..3f2644e1553 100644 --- a/usr.bin/file/file.1 +++ b/usr.bin/file/file.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: file.1,v 1.41 2015/04/27 11:12:49 jmc Exp $ +.\" $OpenBSD: file.1,v 1.42 2015/05/30 06:25:35 nicm Exp $ .\" $FreeBSD: src/usr.bin/file/file.1,v 1.16 2000/03/01 12:19:39 sheldonh Exp $ .\" .\" Copyright (c) 2015 Nicholas Marriott @@ -28,7 +28,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd $Mdocdate: April 27 2015 $ +.Dd $Mdocdate: May 30 2015 $ .Dt FILE 1 .Os .Sh NAME @@ -74,6 +74,13 @@ or .Em data meaning anything else. .Pp +If +.Ar file +is a single dash +.Pq Sq - , +.Nm +reads from the standard input. +.Pp The options are as follows: .Bl -tag -width indent .It Fl b diff --git a/usr.bin/file/file.c b/usr.bin/file/file.c index 81294d8b791..ba0c3df0903 100644 --- a/usr.bin/file/file.c +++ b/usr.bin/file/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.44 2015/05/29 15:58:34 nicm Exp $ */ +/* $OpenBSD: file.c,v 1.45 2015/05/30 06:25:35 nicm Exp $ */ /* * Copyright (c) 2015 Nicholas Marriott @@ -209,7 +209,13 @@ main(int argc, char **argv) memset(&msg, 0, sizeof msg); msg.idx = idx; - if (lstat(argv[idx], &msg.sb) == -1) { + if (strcmp(argv[idx], "-") == 0) { + if (fstat(STDIN_FILENO, &msg.sb) == -1) { + fd = -1; + msg.error = errno; + } else + fd = STDIN_FILENO; + } else if (lstat(argv[idx], &msg.sb) == -1) { fd = -1; msg.error = errno; } else { @@ -441,8 +447,11 @@ try_stat(struct input_file *inf) strerror(inf->msg->error)); return (1); } - if (sflag) { + if (sflag || strcmp(inf->path, "-") == 0) { switch (inf->msg->sb.st_mode & S_IFMT) { + case S_IFIFO: + if (strcmp(inf->path, "-") != 0) + break; case S_IFBLK: case S_IFCHR: case S_IFREG: @@ -617,7 +626,10 @@ test_file(struct input_file *inf, size_t width) if (bflag) printf("%s\n", inf->result); else { - xasprintf(&label, "%s:", inf->path); + if (strcmp(inf->path, "-") == 0) + xasprintf(&label, "/dev/stdin:"); + else + xasprintf(&label, "%s:", inf->path); printf("%-*s %s\n", (int)width, label, inf->result); free(label); } -- 2.20.1