This is the mail archive of the libc-alpha@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: fdopen and errno


   From: Andreas Jaeger <aj@arthur.rhein-neckar.de>
   Date: 03 Jul 1999 15:35:10 +0200

   Ben mentioned a problem with fdopen on bug-glibc.  I'm redirecting
   this to libc-alpha and ask for your opinions.  Shouldn't fdopen set 
   errno if the mode to fdopen is incorrect?  I'm appending a simple
   program which open a file write only and the fdopen is called with
   read, write and append.  The behaviour is correct but shouldn't the
   call with read set errno?

First note that a portable program cannot expect fdopen to detect any
error condition.  The call

   fdopen (some_random_int, "r");

may very well return a valid (though probably unusable) stream.

That said, both Unix98 and the glibc manual say that the mode of the
stream must be allowed by the file access mode of the open file.  This
probably means that if this is not the case the result is unspecified.

However the glibc manual clearly says that this will be detected, and
a null pointer is returned.  The glibc manual does not mention any
errno values in the description of fdopen.  Which means that the
contents of errno are unspecified after a call to fdopen.  Unix98
however says that on error a null pointer is returned and errno is set
to indicate the error.  Thus for Unix98 compatibility we should
probably set errno.  It looks as if we already detect EBADF and ENOMEM (I
don't think we will ever encounter a condition that should generate
EMFILE in glibc), and EINVAL in the case of a totally bogus mode
specification.  We should probably set errno to EINVAL as well if the
stream mode doesn't match the access mode.

Mark


1999-07-04  Mark Kettenis  <kettenis@gnu.org>

	* libio/iofdopen.c (_IO_new_fdopen): Set EINVAL if MODE is not
	allowed by the file access mode of the open file.


--- /home/kettenis/CVS/libc/libio/iofdopen.c	Thu Jun 17 19:37:18 1999
+++ libc/libio/iofdopen.c	Sun Jul  4 14:59:22 1999
@@ -77,10 +77,15 @@
 #ifndef O_ACCMODE
 #define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
 #endif
-  if (fd_flags == -1
-      || ((fd_flags & O_ACCMODE) == O_RDONLY && !(read_write & _IO_NO_WRITES))
-      || ((fd_flags & O_ACCMODE) == O_WRONLY && !(read_write & _IO_NO_READS)))
+  if (fd_flags == -1)
     return NULL;
+  
+  if (((fd_flags & O_ACCMODE) == O_RDONLY && !(read_write & _IO_NO_WRITES))
+      || ((fd_flags & O_ACCMODE) == O_WRONLY && !(read_write & _IO_NO_READS)))
+    {
+      MAYBE_SET_EINVAL;
+      return NULL;
+    }
 
   /* The May 93 draft of P1003.4/D14.1 (redesignated as 1003.1b)
      [System Application Program Interface (API) Amendment 1:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]