[uClibc]fdopen() problem

Manuel Novoa III mnovoa3 at bellsouth.net
Tue Mar 13 16:05:49 UTC 2001


Michael,

On Tue, 13 Mar 2001, michaels at jungo.com wrote:
> While using netkit's ftp client compiled against uClibc, I get an error
> on fdopen.
> I tried to track down the problem and arrived to a conclusion that the
> follwoing code causes the return value of fdopen to be -1:
> ------------------------
> stdio.c at line 710
> 
> 	} else {   /* fdopen -- check mode is compatible. */
> 		cur_mode = fcntl(fd, F_GETFL);
> 		if ((cur_mode == -1) || ((cur_mode ^ open_mode) & O_ACCMODE)) {
> 			fd = -1;
> 		}
> ---------------------
> I don't understand the ((cur_mode ^ open_mode) & O_ACCMODE) importance
> to failure.

It is meant to check that the mode passed to fdopen is consistent with the
current mode of the file descriptor.  So, it fails if fcntl reports an error or
if the modes are inconsistent.  You don't want the FILE to think that the file
descriptor is open for reading or writing when it isn't.

What mode is being passed to fdopen?  How was the file descriptor opened?

> Can anyone claify this for me? 
> Anyway I think the condition must be inverse: !((cur_mode ^ open_mode) &
> O_ACCMODE)

(1 ^ 1) & 1 == 0      bits same -- ok
(0 ^ 0) & 1 == 0      
(1 ^ 0) & 1 == 1      bits different -- set fd = -1 to signal an error
(0 ^ 1) & 1 == 1
(x ^ x)  & 0 == 0      don't care about these

I don't know... looks good to me, but I've been up all night rewriting scanf.

Manuel





More information about the uClibc mailing list