From 143846ad369eba2324e13743ff03bcf50d56c942 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 21 Apr 2009 00:25:43 +0000 Subject: [PATCH] * dir.c (dir_path, dir_each, glob_helper): use readdir_r() if available. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23241 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ configure.in | 2 +- dir.c | 29 +++++++++++++++++++---------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6b5533e5aa..72b236fe6e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Apr 21 09:25:41 2009 Nobuyoshi Nakada + + * dir.c (dir_path, dir_each, glob_helper): use readdir_r() if + available. + Tue Apr 21 09:20:21 2009 Nobuyoshi Nakada * lib/mkmf.rb (init_mkmf): needs default library path even if diff --git a/configure.in b/configure.in index e5c58410d6..127901f80a 100644 --- a/configure.in +++ b/configure.in @@ -1068,7 +1068,7 @@ AC_REPLACE_FUNCS(dup2 memmove strerror\ strlcpy strlcat) AC_CHECK_FUNCS(fmod killpg wait4 waitpid fork spawnv syscall chroot fsync getcwd eaccess\ truncate ftruncate chsize times utimes utimensat fcntl lockf lstat\ - link symlink readlink\ + link symlink readlink readdir_r\ setitimer setruid seteuid setreuid setresuid setproctitle socketpair\ setrgid setegid setregid setresgid issetugid pause lchown lchmod\ getpgrp setpgrp getpgid setpgid initgroups getgroups setgroups\ diff --git a/dir.c b/dir.c index 0ecd09b262..0b293e3dd1 100644 --- a/dir.c +++ b/dir.c @@ -478,6 +478,19 @@ dir_path(VALUE dir) return rb_str_dup(dirp->path); } +#if defined HAVE_READDIR_R +# define READDIR(dir, enc, entry, dp) (readdir_r(dir, entry, &(dp)) == 0 && dp != 0) +#elif defined _WIN32 +# define READDIR(dir, enc, entry, dp) ((dp = rb_w32_readdir_with_enc(dir, enc)) != 0) +#else +# define READDIR(dir, enc, entry, dp) ((dp = readdir(dir)) != 0) +#endif +#if defined HAVE_READDIR_R +# define IF_HAVE_READDIR_R(something) something +#else +# define IF_HAVE_READDIR_R(something) /* nothing */ +#endif + /* * call-seq: * dir.read => string or nil @@ -493,19 +506,13 @@ dir_path(VALUE dir) static VALUE dir_read(VALUE dir) { -#ifdef _WIN32 -# define READDIR(dir, enc) rb_w32_readdir_with_enc(dir, enc) -#else -# define READDIR(dir, enc) readdir(dir) -#endif - struct dir_data *dirp; struct dirent *dp; + IF_HAVE_READDIR_R(struct dirent entry); GetDIR(dir, dirp); errno = 0; - dp = READDIR(dirp->dir, dirp->enc); - if (dp) { + if (READDIR(dirp->dir, dirp->enc, &entry, dp)) { return rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc); } else if (errno == 0) { /* end of stream */ @@ -539,11 +546,12 @@ dir_each(VALUE dir) { struct dir_data *dirp; struct dirent *dp; + IF_HAVE_READDIR_R(struct dirent entry); RETURN_ENUMERATOR(dir, 0, 0); GetDIR(dir, dirp); rewinddir(dirp->dir); - for (dp = READDIR(dirp->dir, dirp->enc); dp != NULL; dp = READDIR(dirp->dir, dirp->enc)) { + while (READDIR(dirp->dir, dirp->enc, &entry, dp)) { rb_yield(rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc)); if (dirp->dir == NULL) dir_closed(); } @@ -1259,10 +1267,11 @@ glob_helper( if (magical || recursive) { struct dirent *dp; + IF_HAVE_READDIR_R(struct dirent entry); DIR *dirp = do_opendir(*path ? path : ".", flags); if (dirp == NULL) return 0; - for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { + while (READDIR(dirp, enc, &entry, dp)) { char *buf = join_path(path, dirsep, dp->d_name); enum answer new_isdir = UNKNOWN;