On Thu, May 28, 2015 at 11:52:22PM +0100, Nicholas Marriott wrote:
> Hi
>
> Can you make a patch against -current? Your source tree seems pretty out
> of date.
arg, my bads.
> Yes please check the return values from fcntl and fstat.
I added check for fstat, but not for fcntl.
The problem with fcntl is when '-' is "/dev/zero" (for example), fcntl
return error "Operation not supported by device".
$ file - < /dev/zero
-: cannot stat '-' (Operation not supported by device)
There are two problems:
- inconsistency with "file /dev/zero" as it works well
- invalid error message (cannot stat), whereas it is "cannot fcntl"
What do you think ? Should I trait fcntl(O_NONBLOCK) as error ?
I have also integrated the comment from sthen@ in order to have file
working like before:
- mention "/dev/stdin" instead of "-" in report
- don't need to pass -s when "-" is used
I have changed the file.1 patch too.
> > file(1) in 5.5:
> > ---------------
> >
> > $ echo foobar | file -
> > /dev/stdin: ASCII text
> >
> > $ echo foobar | file -s /dev/stdin
> > /dev/stdin: ASCII text
> >
> >
> >
> > file(1) in -current:
> > --------------------
> >
> > $ echo foobar | file -
> > -: cannot stat '-' (No such file or directory)
> >
> > $ echo foobar | file -s /dev/stdin
> > /dev/stdin: data
> >
> >
> >
> > after patching:
> > ---------------
> >
$ echo foobar | file -
/dev/stdin: ASCII text
$ file - < file.c
/dev/stdin: ASCII C program text
$ file ./- # process the file named '-'
./-: cannot stat './-' (No such file or directory)
Thanks.
--
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 10:30:53 -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.39
diff -u -p -r1.39 file.c
--- file.c 28 May 2015 19:26:37 -0000 1.39
+++ file.c 29 May 2015 10:30:53 -0000
@@ -208,9 +208,20 @@ 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;
+ (void)fcntl(fd, O_NONBLOCK);
+ }
+
+ } else if (lstat(argv[idx], &msg.sb) == -1) {
fd = -1;
msg.error = errno;
+
} else {
fd = open(argv[idx], O_RDONLY|O_NONBLOCK);
if (fd == -1 && (errno == ENFILE || errno == EMFILE))
@@ -435,6 +447,10 @@ try_stat(struct input_file *inf)
strerror(inf->msg->error));
return (1);
}
+
+ if (strcmp(inf->path, "-") == 0)
+ return (0);
+
if (sflag) {
switch (inf->msg->sb.st_mode & S_IFMT) {
case S_IFBLK:
@@ -611,7 +627,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);
}