From 17eb86eb12ae6bdb299af12bee59502929dfa891 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 16 Apr 2014 08:46:29 +0000 Subject: [PATCH] util.c: let getcwd allocate buffer * util.c (ruby_getcwd): POSIX.1-2001 extends getcwd(3) as it allocates the buffer if the argument is NULL. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45598 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- util.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/util.c b/util.c index b999aff05c..a9e5590cde 100644 --- a/util.c +++ b/util.c @@ -467,19 +467,14 @@ ruby_strdup(const char *str) return tmp; } -#ifdef __native_client__ char * ruby_getcwd(void) { +#if defined __native_client__ char *buf = xmalloc(2); strcpy(buf, "."); - return buf; -} -#else -char * -ruby_getcwd(void) -{ -#ifdef HAVE_GETCWD +#elif defined HAVE_GETCWD +# if defined NO_GETCWD_MALLOC int size = 200; char *buf = xmalloc(size); @@ -491,6 +486,12 @@ ruby_getcwd(void) size *= 2; buf = xrealloc(buf, size); } +# else + char *buf, *cwd = getcwd(NULL, 0); + if (!cwd) rb_sys_fail("getcwd"); + buf = ruby_strdup(cwd); /* allocate by xmalloc */ + free(cwd); +# endif #else # ifndef PATH_MAX # define PATH_MAX 8192 @@ -504,7 +505,6 @@ ruby_getcwd(void) #endif return buf; } -#endif /**************************************************************** *