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);
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);
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
struct dirent* pentry = malloc(offsetof(struct dirent, d_name) +
pathconf(dirpath, _PC_NAME_MAX) + 1);
if (!pentry)
out_of_memory();
for (;;){
struct dirent* result;
readdir_r(dirp, pentry, &result); // you can check the return code, but it only fails if dirp is invalid
if( !result )
break;
// process result
}
free(pentry);
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