Bug in isatty (all systems!)
Steven Bellovin
smb at ulysses.UUCP
Thu Jul 11 22:00:27 AEST 1985
> > The following short C program
> >
> > main(){
> > extern int errno;
> >
> > errno = 0;
> > putchar('\n');
> > if(errno > 0)
> > perror("perror");
> > }
> >
> > gives unexpected results.
> > The reason is that putchar calls putc calls _flsbuf calls isatty
> > and isatty contains the code
> > if(ioctl(f,TIOCGETP,&ttyb)<0)
> > return(0);
>
> (which sets errno=25: not a typewriter unless stdout is /dev/tty)
>
> I was going to post this one myself. I found it on BSD 4.2,
> System V/2.0 and UTX-32 (a Gould BSD + V port). It really
> screwed me up bad.
>
> The other 'gotcha' (same systems) is mktemp, which attempts
> to build a unique file name. errno is always 2 (file not
> found) after successfully calling mktemp on these 3 systems.
The manual warns you that errno is not cleared on successful calls, so you're
not allowed to check it unless you've gotten an error return -- say, a
-1 from a system call. With stdio, use ferror() to detect problems.
More information about the Comp.unix.wizards
mailing list