Can I use readdir_r like this? I don’t find anything about it in the spec, but maybe I’m just blind…
readdir_r(dir, entry, &entry);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s readdir_r, and the second argument is a pointer to a struct dirent, not a struct dirent itself, and the third argument is a pointer to pointer to a struct dirent, which receives the address of the struct dirent or NULL for end-of-directory. The usage is something like
As Hristo points out above, the arguments are passed by value so you could pass the address of the second arg (pentry) as the third arg (i.e., &pentry) — it doesn’t affect readir_r, which has no way to tell. But that will store NULL in pentry when you reach the end of the directory, but you need the value of pentry in order to free the malloced buffer it points to. So forget about whether using the address of the second argument is allowed … doing so is pointless, misleading, and results in a memory leak.
For the spec of readdir_r, see
http://www.kernel.org/doc/man-pages/online/pages/man3/readdir.3.html
or
http://pubs.opengroup.org/onlinepubs/009695399/functions/readdir.html