[patch] Use thread safe readdir_r instead of readdir

Havoc Pennington hp at redhat.com
Fri Sep 8 18:31:16 PDT 2006


I went googling to see if readdir_r is portable and found this:
http://lists.grok.org.uk/pipermail/full-disclosure/2005-November/038295.html

We should probably use the buffer size function from there, appended.

I'm guessing there's some portability wonkiness that will come up too, 
but we can just wait and see what BSD and OS X users report.

Havoc

    /* Calculate the required buffer size (in bytes) for directory
     * entries read from the given directory handle.  Return -1 if this
     * this cannot be done. 

     * If you use autoconf, include fpathconf and dirfd in your
     * AC_CHECK_FUNCS list.  Otherwise use some other method to detect
     * and use them where available.
     */

     size_t dirent_buf_size(DIR * dirp)
     {
         long name_max;
     #   if defined(HAVE_FPATHCONF) && defined(HAVE_DIRFD) \
            && defined(_PC_NAME_MAX)
             name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX);
             if (name_max == -1)
     #           if defined(NAME_MAX)
                     name_max = NAME_MAX;
     #           else
                     return (size_t)(-1);
     #           endif
     #   else
     #       if defined(NAME_MAX)
                 name_max = NAME_MAX;
     #       else
     #           error "buffer size for readdir_r cannot be determined"
     #       endif
     #   endif
         return (size_t)offsetof(struct dirent, d_name) + name_max + 1;
     }



More information about the dbus mailing list