Hi
We deliberately do not support FIFOs because they will block forever if
nothing is writing and using file(1) on FIFOs seems unnecessary, the old
file(1) had a hack to get around this with poll(2) but we're not going
to do that. So I think we should only allow FIFOs for stdin where people
presumably know what they're doing, not generally:
Index: file.1
===================================================================
RCS file: /cvs/src/usr.bin/file/file.1,v
retrieving revision 1.41
diff -u -p -r1.41 file.1
--- file.1 27 Apr 2015 11:12:49 -0000 1.41
+++ file.1 29 May 2015 22:29:59 -0000
@@ -74,6 +74,14 @@ 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
Index: file.c
===================================================================
RCS file: /cvs/src/usr.bin/file/file.c,v
retrieving revision 1.44
diff -u -p -r1.44 file.c
--- file.c 29 May 2015 15:58:34 -0000 1.44
+++ file.c 29 May 2015 22:29:59 -0000
@@ -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)
+ return (0);
case S_IFBLK:
case S_IFCHR:
case S_IFREG:
@@ -617,7 +626,10 @@ test_file(struct input_file *inf, size_t
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);
}
On Fri, May 29, 2015 at 09:34:32PM +0200, S??bastien Marie wrote:
> On Fri, May 29, 2015 at 12:27:16PM +0100, Nicholas Marriott wrote:
> > Here is a slightly tweaked version of your diff.
> >
>
> I am not completely ok (see below)
>
> > Index: file.c
> > ===================================================================
> > RCS file: /cvs/src/usr.bin/file/file.c,v
> > retrieving revision 1.41
> > diff -u -p -r1.41 file.c
> > --- file.c 29 May 2015 11:03:37 -0000 1.41
> > +++ file.c 29 May 2015 11:21:52 -0000
> > @@ -433,7 +439,7 @@ 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_IFBLK:
> > case S_IFCHR:
>
> If we collapse sflag with "-", a pipe will not be "expanded" by default.
>
> $ echo foobar | file -
> /dev/stdin: fifo (named pipe)
>
> I just also see a bogus behaviour compared to previous file(1) version:
>
> $ echo foobar | file -s -
> /dev/stdin: fifo (named pipe)
>
> I think fifo (S_IFIFO) should be attempted to be read too.
>
> 5.5$ mkfifo test
> 5.5$ (echo 123 > test) &
>
> [1] 18981
> 5.5$ file -s test
> test: ASCII text
>
> Next a slightly modified patch.
> --
> S??bastien Marie
>
>
> Index: file.1
> ===================================================================
> RCS file: /cvs/src/usr.bin/file/file.1,v
> retrieving revision 1.41
> diff -u -p -r1.41 file.1
> --- file.1 27 Apr 2015 11:12:49 -0000 1.41
> +++ file.1 29 May 2015 19:32:43 -0000
> @@ -74,6 +74,14 @@ 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
> @@ -92,7 +100,7 @@ rather than
> .It Fl L
> Causes symlinks to be followed.
> .It Fl s
> -Attempts to read block and character device files, not just regular files.
> +Attempts to read block, character device and fifo files, not just regular
> files.
> .It Fl W
> Displays warnings when parsing the magic file or applying its tests.
> Usually used for debugging.
> Index: file.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/file/file.c,v
> retrieving revision 1.44
> diff -u -p -r1.44 file.c
> --- file.c 29 May 2015 15:58:34 -0000 1.44
> +++ file.c 29 May 2015 19:32:43 -0000
> @@ -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,11 +447,12 @@ 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_IFBLK:
> case S_IFCHR:
> case S_IFREG:
> + case S_IFIFO:
> return (0);
> }
> }
> @@ -617,7 +624,10 @@ test_file(struct input_file *inf, size_t
> 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);
> }