[Hackers] Re: any way to reset errno?

M. Warner Losh imp at bsdimp.com
Sun Feb 6 15:12:31 PST 2005


In message: <4206971F.1020005 at lava.net>
            Julian Cowley <julian at lava.net> writes:
: M. Warner Losh wrote:
: > In message: <20050206132124.GA746 at daemon.unete.cl>
: >             Daniel Molina Wegener <dmw at unete.cl> writes:
: > :    Any way to reset errno?
: > 
: > errno = 0;
: > 
: > Routines that return an error status in errno generally don't set it
: > to 0 to mean no error.
: 
: Which implies errno should never need to be set to zero since the 
: convention is to only look at errno if a system call fails.  The only 
: time errno needs to be explicitly set (to any value) is when preserving 
: the error value between system calls.  Such as:
: 
: if (write(fd, buf, len) < 0) {
: 	int saved_errno;
: 
: 	saved_errno = errno;
: 	close(fd); /* ignore any error */
: 	errno = saved_errno;
: 	return -1;
: }

Well, to portably[*] use some of the math(3) routines in libc, you
need to set it to 0 before calling them (strtol, et al come to mind).
Otherwise you are correct.  In fact, it is generally incorrect to
write code like:

      errno = 0;
      close(fd);
      if (errno != 0)

because errno isn't necessarily valid even if it has changed in that
case...

Warner


More information about the freebsd-hackers mailing list