Hi,

It was possible, using the previous file(1) version, to determine the
file type of stdin stream. The current version don't permit that (or I
don't found how to do that).

I would propose a patch in order to support '-' argument to be trait as
STDIN_FILENO descriptor.


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 -                                                          
                             
-: fifo (named pipe)

$ echo foobar | file -s -
-: ASCII text

$ file - < file.c
-: ASCII C program text

$ file ./-                              # process the file named '-'
./-: cannot stat './-' (No such file or directory)

$ echo foobar | file -s /dev/stdin      # behaviour not changed
/dev/stdin: data



About the patch:
  - my english wording is somehow hesitant: it isn't my native language,
    but you should already have noted that :). So corrections in manpage
    are expected.
  
  - I am not sure about mdoc tags that I used (.Em for "stdin" for
    example).

  - I am not sure if catching fcntl(3) or fstat(3) errors would make sens
    or not.
  
  - Same for configure STDIN_FILENO for O_NONBLOCK.


Remaining interrogations:
  - should '-' implies '-s' (as before) ?
  - should I rename "-" to "/dev/stdin" in order to have similar output
    than before ?
  - why "file -s /dev/stdin" returns "data" instead of proper
    determination ?
  - should I trait "/dev/stdin" as STDIN_FILENO too ?


Thanks.
-- 
Sébastien Marie

Index: file.1
===================================================================
RCS file: /cvs/src/usr.bin/file/file.1,v
retrieving revision 1.39
diff -u -p -r1.39 file.1
--- file.1      24 Apr 2015 20:57:51 -0000      1.39
+++ file.1      28 May 2015 18:07:15 -0000
@@ -74,6 +74,16 @@ or
 .Em data
 meaning anything else.
 .Pp
+If the argument is 
+.Dq -
+, it is traited as 
+.Em stdin
+stream. When
+.Em stdin
+is a pipe, consider use of
+.Fl s
+option in order to determine the type of the stream's content.
+.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.32
diff -u -p -r1.32 file.c
--- file.c      24 Apr 2015 17:34:57 -0000      1.32
+++ file.c      28 May 2015 18:07:15 -0000
@@ -197,6 +197,13 @@ static void
 open_file(struct input_file *inf)
 {
        int      retval;
+
+       if (strcmp(inf->path, "-") == 0) {
+               inf->fd = STDIN_FILENO;
+               fcntl(STDIN_FILENO, O_NONBLOCK);
+               fstat(STDIN_FILENO, &inf->sb);
+               return;
+       }
 
        retval = lstat(inf->path, &inf->sb);
        if (retval == -1) {

Reply via email to