Orphaned Response

aeb at mcvax.UUCP aeb at mcvax.UUCP
Wed Jul 10 13:56:00 AEST 1985


In article <112 at desint.UUCP> geoff at desint.UUCP (Geoff Kuenning) writes:
>Um, I think errno is specified to be valid only if putchar returns the
>constant EOF.  Thus, the code should be:
>
>	if (putchar ('\n') == EOF)
>		perror ("putchar");
>

Unfortunately, this is not true.
errno is set by system calls, but not by the stdio library routines.
Thus, when a stdio library routine fails it may be that errno
contains useful information (in case the failure was due to a
system call error return), but it may also be that errno contains
garbage (in case the library routine detected the error itself).
Thus, fopen can fail when all _iob entries are taken; putc can fail
when writing to a stream that has not been opened for writing, etc.

This means that you cannot reliably use the stdio routines when you want
to do error recovery.

Example:
#include <stdio.h>
main(){
	putchar('\n');			/* sets errno via isatty() */
	fclose(stdout);			/* make next putchar fail */
	if(putchar('\n') == EOF)
		perror("putchar");	/* produce garbage */
}
Again, the call  a.out > /dev/null  will produce  putchar: no such device,
and  a.out | ...  produces  putchar: operation not supported on socket.



More information about the Comp.unix.wizards mailing list