Re: fclose() again

From: Russell King (rmk@arm.linux.org.uk)
Date: Sat Jul 15 2000 - 04:31:09 EST


Khimenko Victor writes:
> In <20000714202754.28444.qmail@web1906.mail.yahoo.com> Evan Langlois (goth_74@yahoo.com) wrote:
> > Well, according to the man page, fclose() should return EBADF if the passed
> > argument is not an open stream. No where does it say that it segfaults, and
> > the I don't see when it returns EBADF instead of segfaulting.
>
> > I think the library might need to be patched, wherever it is segfaulting.
>
> No. Man page should be updated instead. If your program tried to call fclose
> for something other then open stream you program is seriosly ill. Better to
> fix it then try to band-aid it. And EBADF encourages such band-aids.

I believe the man page is refering the situation caused by:

        FILE *f;

        f = fopen("/tmp/foo", "w");
        close(fileno(f));
        /*
         * f is still a valid FILE * pointer, but the
         * file associated with it has been closed
         */
        errno = 0;
        fclose(f);
        /*
         * errno should now contain EBADF
         */
        perror("fclose");

Runing the above reveals:

fclose: Bad file descriptor

ie, the behaviour given in the man page. Therefore, the man page is correct.
I think it is a question of interpretation of the man page, which does say:

       EBADF The argument stream is not an open stream.
                                            ^^^^^^^^^^^
ie, "stream" is still a VALID stream (not a NULL pointer or some other weird
address), as demonstrated in the above program.
   _____
  |_____| ------------------------------------------------- ---+---+-
  | | Russell King rmk@arm.linux.org.uk --- ---
  | | | | http://www.arm.linux.org.uk/~rmk/aboutme.html / / |
  | +-+-+ --- -+-
  / | THE developer of ARM Linux |+| /|\
 / | | | --- |
    +-+-+ ------------------------------------------------- /\\\ |

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sat Jul 15 2000 - 21:00:21 EST