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);
}