Why does stat fail for some files?

Eric Blake ericblake@comcast.net
Thu Sep 15 22:34:00 GMT 2005


I'm glad to see you figured out your bug.  Now for another hint:
judicious use of chdir() can save you the effort of concatenating
the directory name with the entry name.

> >>    while((entry = readdir(directory)))
> >
> > You just ignored the possibility that readdir might set errno.
> >
> >>    {
> 
> Ok, I can set errno to 0 before I call stat(), but I was under the 
> impression that if stat() fails it will overwrite the current value of errno 
> with a new one indicating the error. And since I determine if an error 
> occured by checking the return value of stat() and *then* checking errno if 
> the return value was != 0 instead of looking at errno directly, isn't my way 
> safe even if errno was nonzero before the call?

No.  The point here is that ignoring the errno on ANY syscall is
bad practice, and a habit to be broken before it begins, even
though it may be unlikely that the call ever fails.  With
readdir(), the standard safe usage pattern is to assign errno to
0 just before readdir, then if readdir returns NULL, check if errno
is still 0 (you are done, succesfully) or not (readdir failed).

> >>    entry->d_name); }
> >>
> >>    return_value = closedir(directory);

It is right here, when your while loop ends, that you didn't
check to see if readdir failed, and therefore, you don't know
if you finished reading the directory before you tried closing it.

> 
> Thanks for the quick reply, Eric!

Also, questions like this should probably be directed to other
lists, since it was not cygwin specific.

--
Eric Blake



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list