From owner-freebsd-hackers Sun Jun 25 12:52:11 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from mail.rpi.edu (mail.rpi.edu [128.113.100.7]) by hub.freebsd.org (Postfix) with ESMTP id 4631E37B843 for ; Sun, 25 Jun 2000 12:52:06 -0700 (PDT) (envelope-from drosih@rpi.edu) Received: from [128.113.24.47] (gilead.acs.rpi.edu [128.113.24.47]) by mail.rpi.edu (8.9.3/8.9.3) with ESMTP id PAA17586; Sun, 25 Jun 2000 15:51:30 -0400 Mime-Version: 1.0 X-Sender: drosih@mail.rpi.edu Message-Id: In-Reply-To: References: Date: Sun, 25 Jun 2000 15:52:03 -0400 To: Chuck Robey , From: Garance A Drosihn Subject: RE: libc_r/_read(), should the errno be reset to 0? Cc: , , hackers@FreeBSD.ORG Content-Type: text/plain; charset="us-ascii" ; format="flowed" Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG At 12:28 AM -0400 6/25/00, Chuck Robey wrote: >On Fri, 23 Jun 2000, Bohne, Peter wrote: > > > True enough. That's the best advice. However, your original > > post indicated an expectation on your part that errno would > > be somehow automatically reset to 0 before a system call, which > > is definitely *not* the case. You have to clear it - the kernel > > does not do it for you. > >Why would you bother to clear it? You don't check errno to determine >fail/success, you check the function return. If it returns fail, then >errno is going to be correct whether or not you bothered to reset it or >not. Setting it to zero might give you a warm fuzzy feeling, but if >you're correctly detected errors, it's not going to change anything. There are times when clearing errno is significant. What you said is correct for the routine which actually calls the system routine (is it some *_read() routine here?), but some library routines will call several system routines, and only return an error to caller (of that library routine) in some cases. For instance, it might stat() some file, which sets errno if that file does not exist. To the library routine, it isn't really an error if that file does not exist, it just creates the file or perhaps skips over some optional processing. Later on, it may return an error due to some OTHER condition, a condition which did not set errno. Someone checking errno will think that "file does not exist" is the problem, when the problem is elsewhere. As usual with this topic, I can never remember a good example of this, except that I know I've run into it on some occasions... For those occasions, it IS important to zero out errno "at the right place" (wherever that might be...). One of these days I'll have the presence of mind to save a good example of this... Please note that I'm not saying the system routine should necessarily set errno to zero, I'm just saying that there ARE occasions where it IS important for the programmer to reset it to zero before calling some other routines. In those situations, it does much more than just giving someone a "warm and fuzzy" feeling. --- Garance Alistair Drosehn = gad@eclipse.acs.rpi.edu Senior Systems Programmer or drosih@rpi.edu Rensselaer Polytechnic Institute To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message