Imagine the following code:
1: FILE *s1, *s2;
2:
3: s1 = fopen ("foo", "rw"); /* s1 gets assigned */
4: fwrite ("Hello", 1, 5, s1);
5: fclose (s1); /* after this, the system is
6: free to do whatever it wants
7: with the memory pointed to
8: by s1 */
9: s2 = fopen ("bar", "rw"); /* actually, it gets assigned
10: to s2, so s1 and s2 now
11: refer to the same memory */
12: fwrite ("World", 1, 5, s1); /* Ooops, s1 now writes to
file bar!!!! */
This gets even more interesting in a threaded environment:
In this case, the fopen in line 9 might happen in another thread.
So there is no way to get a deterministic behaviour when
passing an fclosed stream to a function and glibc is free
to return whatever it wants (including success). A program
doing such things is buggy and cannot expect anything useful
from libc.
Regards
Joerg