From owner-freebsd-arch@FreeBSD.ORG Tue Mar 2 13:57:01 2004 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4D6A916A4CE for ; Tue, 2 Mar 2004 13:57:01 -0800 (PST) Received: from falcon.midgard.homeip.net (h201n1fls24o1048.bredband.comhem.se [212.181.162.201]) by mx1.FreeBSD.org (Postfix) with SMTP id 9534543D45 for ; Tue, 2 Mar 2004 13:56:59 -0800 (PST) (envelope-from ertr1013@student.uu.se) Received: (qmail 88482 invoked by uid 1001); 2 Mar 2004 21:56:57 -0000 Date: Tue, 2 Mar 2004 22:56:57 +0100 From: Erik Trulsson To: "Jordan K. Hubbard" Message-ID: <20040302215657.GA88174@falcon.midgard.homeip.net> Mail-Followup-To: "Jordan K. Hubbard" , Bruce Evans , freebsd-arch@freebsd.org References: <1060DC2A-6C31-11D8-9000-000393BB9222@queasyweasel.com> <20040302213412.X1223@gamplex.bde.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.6i cc: freebsd-arch@freebsd.org Subject: Re: Another conformance question... This time fputs(). X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 02 Mar 2004 21:57:01 -0000 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 > #include > #include > > 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. 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. -- Erik Trulsson ertr1013@student.uu.se