From: kagel AT quasar DOT bloomberg DOT com Date: Mon, 21 Aug 1995 10:31:17 -0400 To: nbn.nbn.com!sapien!elf Cc: nbn.nbn.com!sapien!sun.soe.clarkson.edu!djgpp Subject: Re: mkdir and errno in V2 Reply-To: kagel AT ts1 DOT bloomberg DOT com From: Marc Singer Date: Thu, 17 Aug 1995 18:02:11 -0700 (PDT) It appears that the mkdir call does not clear errno on success. My code is creating a new path by starting at the full length trying to create the directory. On failure, it removes the last path element and tries again until it succeeds. As soon as it does so, it replaces the path elements one at a time and creates the path one directory at a time. Since it fails once, errno is ENOTDIR. The successful mkdir calls do not clear this value so errno on exit from my procedure is still ENOTDIR. I patch my procedure by clearing errno before each call to mkdir. I'm porting this code fragment as part of CVS. I notice that the procedure assumes that mkdir returns ENOENT when the directory cannot be created. Is the ENOTDIR an incorrect return value? ENOENT is what the Linux libc returns. Most library functions only set errno and do not clear it. It is not safe, in general, to test errno after a function return without first checking the function's other error indication (like returning -1 or NULL) or without having cleared errno yourself before calling the library function. Even if you have recently checked errno and found it clear an intervening function, even one of your own, may have set it or called another function which set it. I always test the functions error indication to discover an error and reserve the use of errno to determine the nature of the error. I have known others, however, whose convention was to clear errno before any library call and then rely on its value upon function return; and they even keep their jobs! Whichever. -- Art S. Kagel, kagel AT ts1 DOT bloomberg DOT com Variety is the soul of pleasure. -- Aphra Behn