Bug in isatty (BSD4.2)

Geoff Kuenning geoff at desint.UUCP
Sat Jul 6 21:22:30 AEST 1985


In article <726 at mcvax.UUCP> aeb at mcvax.UUCP (Andries Brouwer) writes:
>The following short C program
>
>main(){
>	extern int errno;
>
>	errno = 0;
>	putchar('\n');
>	if(errno > 0)
>		perror("perror");
>}
>
>gives unexpected results.
>E.g.
>	a.out | ...
>gives the error message "Operation not supported on socket".
>The reason is...[that putchar can leave errno nonzero even in nonerror cases]

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");

Incidentally, this brings up a bug in putc/putchar.  On both BSD and VMS,
the following code will print a message, even though the putchar succeeds:

	if (putchar ('\377') == EOF)
		fprintf (stderr, "you have the bug!\n");

The problem is that, in <stdio.h>, the putc macro neglects to cast its
argument to an *unsigned* character, so it gets sign-extended on return
to -1.  The fix is easy;  I won't post it because (a) I don't have BSD
at my fingertips at the moment and (b) it's probably a licensing violation
anyway.
-- 

	Geoff Kuenning
	...!ihnp4!trwrb!desint!geoff



More information about the Comp.unix.wizards mailing list