delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/03/02/06:37:06

Sender: rich AT phekda DOT freeserve DOT co DOT uk
Message-ID: <3E61ECD9.232BAE0@phekda.freeserve.co.uk>
Date: Sun, 02 Mar 2003 11:36:57 +0000
From: Richard Dawe <rich AT phekda DOT freeserve DOT co DOT uk>
X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.23 i586)
X-Accept-Language: de,fr
MIME-Version: 1.0
To: djgpp-workers AT delorie DOT com
Subject: Re: Some tidy-ups after the changes to isatty [PATCH]
References: <10303020302 DOT AA17358 AT clio DOT rice DOT edu>
Reply-To: djgpp-workers AT delorie DOT com

Hello.

Charles Sandmann wrote:
> 
> > * Document the new behaviour on the isatty info page.
> >
> > * Some code assumed isatty(fd) != 0 indicated that fd was a terminal.
> >   Fix these cases.
> 
> Does POSIX really say that a bad file handle should return -1?  If so,
> this is inconsistent with every implementation I've ever seen (Turbo C,
> old DJGPP, AIX, Solaris, HP/UX).

Add Linux to this list.

---Start t-isatty---
#include <stdio.h>
#include <unistd.h>

int
main (void)
{
  int foo = isatty(99);

  if (foo < 0)
    perror("wibble");
  else if (foo == 0)
    puts("Not a TTY");
  else
    puts("A TTY");

  return(0);
}
---End t-isatty.c---

iolanthe:~/tmp =] !gcc
gcc -g -Wall -o t-isatty t-isatty.c 
iolanthe:~/tmp =] ./t-isatty 
Not a TTY

> Even our example is wrong (which isn't fixed with this patch).  All of
> the other implementations say it will return 0 and set errno() if
> something was wrong.  Scary.  Just look at all the places in the libc
> which need changing.  Admittedly a bad handle should be rare, but...
[snip]

Re-reading SUSv2 and the POSIX draft, it turns out that it doesn't return -1.
I don't know where I got that from. I guess I saw "sets errno" and added "and
returns -1" mentally. <sigh>

But the previous behaviour was slightly incorrect: it did not set errno.

Thanks for catching this. Patch to follow.

Bye, Rich =]

-- 
Richard Dawe [ http://www.phekda.freeserve.co.uk/richdawe/ ]

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019