guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

readdir_r


From: Kevin Ryde
Subject: readdir_r
Date: Fri, 26 Mar 2004 08:29:53 +1000
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3 (gnu/linux)

        * filesys.c (scm_readdir): Use readdir_r when available, for thread
        safety.

--- filesys.c.~1.120.~  2004-03-25 14:16:28.000000000 +1000
+++ filesys.c   2004-03-26 08:26:38.000000000 +1000
@@ -803,6 +803,11 @@
 #undef FUNC_NAME
 
 
+/* FIXME: The glibc manual has a portability note that readdir_r may not
+   null-terminate its return string.  The circumstances outlined for this
+   are not clear, nor is it clear what should be done about it.  Lets worry
+   about this if/when someone can figure it out.  */
+
 SCM_DEFINE (scm_readdir, "readdir", 1, 0, 0, 
             (SCM port),
            "Return (as a string) the next directory entry from the directory 
stream\n"
@@ -810,6 +815,16 @@
            "end of file object is returned.")
 #define FUNC_NAME s_scm_readdir
 {
+  /* On Solaris 2.7, struct dirent only contains "char d_name[1]" and one is
+     expected to provide a buffer of "sizeof(struct dirent) + NAME_MAX"
+     bytes.  The glibc 2.3.2 manual notes this sort of thing too, and
+     advises "offsetof(struct dirent,d_name) + NAME_MAX + 1".  Either should
+     suffice, we give both to be certain.  */
+  union {
+    struct dirent ent;
+    char pad1 [sizeof(struct dirent) + NAME_MAX];
+    char pad2 [offsetof (struct dirent, d_name) + NAME_MAX + 1];
+  } u;
   struct dirent *rdent;
 
   SCM_VALIDATE_DIR (1, port);
@@ -817,7 +832,12 @@
     SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port));
 
   errno = 0;
+#if HAVE_READDIR_R
+  SCM_SYSCALL (readdir_r ((DIR *) SCM_CELL_WORD_1 (port), &u.ent, &rdent));
+#else
+  rdent = &u.ent;  /* suppress warning about u unused */
   SCM_SYSCALL (rdent = readdir ((DIR *) SCM_CELL_WORD_1 (port)));
+#endif
   if (errno != 0)
     SCM_SYSERROR;
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]