AT&T system V fdopen mode checking

Peter da Silva peter at ficc.uu.net
Sun Dec 3 07:18:31 AEST 1989


Doug Gwyn isn't often wrong, but...

In article <11723 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
> In article <713 at mbf.UUCP> hae at mbf.UUCP (Hae Hirdler) writes:
> -The man page on the fdopen states that "the type of the stream must 
> -agree with the mode of the open file."

> I believe by "mode" they mean "file access permissions".

No, they mean the second argument to open(). But I don't believe it's
possible to determine what the mode of a file descriptor is by examining
it, so:

> -For example, fd= open(filename, O_RDONLY); fptr= fdopen(filename, "w")
> -will return a NON_NULL fptr value instead of NULL.  

Yes, but writes to the file may fail. There's no safe way of checking this
from fdopen. I suppose a write(fd, "", 0) will work as a check. I don't
know what this might do on some files (tape drives, for example).

I said may, not will, because if the file descriptor refers to a terminal
you can write to it even if it's opened O_RDONLY.

> O_RDONLY has nothing to do with the file.  It is just a parameter to open().

Yes, a parameter that determines the mode of the file descriptor. When you
try to write to a file opened O_RDONLY you will get an error EBADF.

The manual is correct, you should make the modes agree. The code doesn't
check, so you won't get an error, but don't do it anyway.
-- 
`-_-' Peter da Silva. +1 713 274 5180. <peter at ficc.uu.net>.
 'U`  Also <peter at ficc.lonestar.org> or <peter at sugar.lonestar.org>.
"The basic notion underlying USENET is the flame."
	-- Chuq Von Rospach, chuq at Apple.COM 



More information about the Comp.bugs.sys5 mailing list