S_ISCHR, S_ISFIFO, O_NONBLOCK, O_NOCTTY

Melissa O'Neill oneill@cs.sfu.ca
Thu Feb 4 12:49:00 GMT 1999


I wrote:
>>> cppfiles.c now tests file types using S_ISFIFO and S_ISCHR.  Some
>>> Unix systems don't have FIFOs, and non-POSIX systems do not have
>>> these macros.

... and Zack Weinberg <zack@rabi.columbia.edu> replied:
> On systems without FIFOs, what does fstat() return for stdin when
> stdin is an anonymous pipe?  The check is applied to the main input,
> so it's relevant.  We don't want to disallow piping to cpp by accident.

On many older BSD systems, pipes are implemented using socketpair, so
in theory, S_IFSOCK should be set. However, on my own system (NEXTSTEP 3.3)
the manual page for fstat states:

    BUGS
	  Applying fstat to a socket (and thus to a pipe) returns a
	  zeroed buffer, except for the blocksize field, and a unique
	  device and inode number.

So, in my own code, for some `struct stat buf', I tend to use:

    if (buf.st_mode == 0 && buf.st_size == 0 && buf.st_nlink == 0)
    	we_have_a_socket();

as my test which is probably overkill, but certainly works for me on
NEXTSTEP 3.3.

    Melissa.

P.S. Yesterday, I discovered that if you fstat an open AF_INET socket
on Solaris 2.5, it tells you that it's a character special device
(S_IFCHR) rather than telling you it is a socket (S_IFSOCK) as you might
have expected  (Solaris 2.6 gets it right).  I had been trying to use
fstat to determine whether a file descriptor was a socket and conditionally
call shutdown(), but I just gave up.  With socket fstat bugs in NEXTSTEP
and Solaris it was easier just to call shutdown and ignore any ENOTSOCK
errors.


More information about the Gcc-patches mailing list