Bug in isatty (BSD4.2)

M.J.Shannon mjs at eagle.UUCP
Thu Jul 11 02:32:24 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.

How about using the routine which is part of stdio to determine the presence
of errors?  I beileve it's called ferror(iop).  The only problem is that the
programmer must determine what the error was by context.....
-- 
	Marty Shannon
UUCP:	ihnp4!eagle!mjs
Phone:	+1 201 522 6063



More information about the Comp.unix.wizards mailing list