POSIX's page on stderr, stdin, stdout - standard I/O streams says this:
The stderr stream is expected to be open for reading and writing.
How strong is "expected to be"? Is violating it Undefined Behavior? And whose responsibility is it, the system's or the application's?
Consider this program:
#include <stdio.h>
int main(void) {
printf("feof is %d and ferror is %d\n", feof(stderr), ferror(stderr));
printf("fgetc is %d\n", fgetc(stderr));
printf("feof is %d and ferror is %d\n", feof(stderr), ferror(stderr));
}
When I run that without redirecting stderr (so it's pointing to my terminal just like stdin is), it immediately outputs this without waiting for any input:
feof is 0 and ferror is 0
fgetc is -1
feof is 0 and ferror is 1
Does that mean my system isn't POSIX-compliant?
Also, if it's my responsibility, then suppose I have a file with permissions 620, and that I'm in the group but not the owner. Does this mean that someprogram 2>saidfile is Undefined Behavior, since you couldn't read from stderr no matter what in that case?