Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com From: ericblake AT comcast DOT net (Eric Blake) To: Mikael , cygwin AT cygwin DOT com Subject: Re: Why does stat fail for some files? Date: Thu, 15 Sep 2005 20:38:35 +0000 Message-Id: <091520052038.1926.4329DBCB000279E40000078622007614380A050E040D0C079D0A@comcast.net> 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/