Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/18/84; site ulysses.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!smb
From: smb@ulysses.UUCP (Steven Bellovin)
Newsgroups: net.bugs.4bsd,net.unix-wizards,net.bugs.usg
Subject: Re: Bug in isatty (all systems!)
Message-ID: <1005@ulysses.UUCP>
Date: Thu, 11-Jul-85 08:00:27 EDT
Article-I.D.: ulysses.1005
Posted: Thu Jul 11 08:00:27 1985
Date-Received: Sat, 13-Jul-85 07:32:56 EDT
References: <726@mcvax.UUCP> <965@sdcsvax.UUCP>
Organization: AT&T Bell Laboratories, Murray Hill
Lines: 30
Xref: watmath net.bugs.4bsd:1600 net.unix-wizards:13785 net.bugs.usg:248

> > 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.