Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 2 Mar 2004 22:56:57 +0100
From:      Erik Trulsson <ertr1013@student.uu.se>
To:        "Jordan K. Hubbard" <jkh@queasyweasel.com>
Cc:        freebsd-arch@freebsd.org
Subject:   Re: Another conformance question...  This time fputs().
Message-ID:  <20040302215657.GA88174@falcon.midgard.homeip.net>
In-Reply-To: <E7470FE8-6C82-11D8-9000-000393BB9222@queasyweasel.com>
References:  <1060DC2A-6C31-11D8-9000-000393BB9222@queasyweasel.com> <20040302213412.X1223@gamplex.bde.org> <E7470FE8-6C82-11D8-9000-000393BB9222@queasyweasel.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Mar 02, 2004 at 11:50:48AM -0800, Jordan K. Hubbard wrote:
> Thanks to everyone (not just bde) for reviewing the fix.  Linux was 
> right, I was wrong, EBADF it is!
> 
> On Mar 2, 2004, at 4:03 AM, Bruce Evans wrote:
> 
> >Should be EBADF, as in Linux.
> >
> >>                 return (EOF);
> >>+       }
> >
> >Otherwise, I agree with this change except for its style bugs 
> >(unsorting
> >of the #includes and tab lossage).
> 
> Change committed with #includes sorted and proper use of tabs (change 
> is consistent with the rest of the file's style).
> 
> It's also not clear to me why ENODEV is being returned, though I'm not 
> as inclined to blame isatty() since it appears to do the right thing:
> 
> #include <stdio.h>
> #include <fcntl.h>
> #include <errno.h>
> 
> main() {
>         int fd, ret;
> 
>         fd = open("/dev/null", O_RDONLY);
>         ret = isatty(fd);
>         printf("ret = %d, errno = %d\n", ret, errno);
>         close(fd);
> }
> 
> Prints:	ret = 0, errno = 25
> 
> All isatty() does is call tcgetattr(), it doesn't do an 
> ioctl(...TIOCGETA...).

But tcgetattr does call ioctl(..TIOCGETA...).

In particular isatty is implemented as
[...]
  retval = (tcgetattr(fd, &t) != -1);
[...]
  return retval;
[...]
so if isatty returns 0, then tcgetattr returned -1.
If tcgetattr returns -1 it means some error occured and errno is set to
indicate the type of error.
errno==25 is ENOTTY which is exactly what tcgetattr(3) is documented to
return if the file descriptor does not refer to a terminal (which it
does not do in this example.)
In short: The code above seems to behave absolutely correctly and as
one would expect.


Unless a function actually returns an error, (and the documentation
says it sets errno) errno should be assumed to contain garbage.
>From the intro(2) manpage:

   When a system call detects an error, it returns an integer value
   indicating failure (usually -1) and sets the variable errno
   accordingly. <This allows interpretation of the failure on receiving
   a -1 and to take action accordingly.> Successful calls never set
   errno; once set, it remains until another error occurs.  It should
   only be examined after an error.  Note that a number of system calls
   overload the meanings of these error numbers, and that the meanings
   must be interpreted according to the type and circumstances of the
   call.


-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013@student.uu.se



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040302215657.GA88174>