[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
fflush, EBADF, POSIX
From: |
Bruno Haible |
Subject: |
fflush, EBADF, POSIX |
Date: |
Sun, 18 Sep 2011 17:29:14 +0200 |
User-agent: |
KMail/1.13.6 (Linux/2.6.37.6-0.5-desktop; KDE/4.6.0; x86_64; ; ) |
Hi Eric,
According to POSIX:2008
<http://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html>,
fflush() must fail with EBADF if the file descriptor is invalid.
This test program
==================================== foo.c ====================================
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
int
main ()
{
FILE *f;
f = fopen ("test-fflush.txt", "w");
if (f == NULL)
return 1;
#if 0 /* fflush should also fail without this statement. */
fputc ('x', f);
#endif
if (close (fileno (f)) < 0)
return 2;
errno = 0;
if (!(fflush (f) == EOF && errno == EBADF))
{
fprintf (stderr, "fflush did not fail, as expected by POSIX\n");
return 3;
}
fclose (f);
return 0;
}
===============================================================================
shows that this requirement is not fulfilled, not even on glibc platforms:
$ gcc -Wall foo.c
$ ./a.out
fflush did not fail, as expected by POSIX
But it is actually stupid to require that even if the buffer is empty, that is,
even if fflush(f) is normally a no-op, the implementation should make system
calls to test whether the file descriptor is valid or not.
Can you bring this before the Austin group, please?
I would say, a good fix would be to change the "shall fail" requirement for
EBADF
to a "may fail" requirement.
Bruno
--
In memoriam Bernhard Bästlein <http://en.wikipedia.org/wiki/Bernhard_Bästlein>
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- fflush, EBADF, POSIX,
Bruno Haible <=