summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2015-05-30 06:25:36 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2015-05-30 06:25:36 +0000
commit2ed1668f6afd3d9c7c035b0c66c35b1562eb3f4f (patch)
treecec9b586a5738fcfa2d2bd84786173ee0ab1e006 /usr.bin
parent6ef45dd02a6cc8e683256e6aa77a9fe011737f65 (diff)
Support - to read from stdin, from Sebastien Marie.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/file/file.111
-rw-r--r--usr.bin/file/file.c20
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 <nicm@openbsd.org>
@@ -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 <nicm@openbsd.org>
@@ -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);
}