summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-04-16 08:46:29 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-04-16 08:46:29 +0000
commit17eb86eb12ae6bdb299af12bee59502929dfa891 (patch)
treeb7323b3fb11f5190bbebf2295710f23fc81d94f5 /util.c
parent9f4f9048a0b095c340e7640a999f209ca02723ca (diff)
downloadruby-17eb86eb12ae6bdb299af12bee59502929dfa891.tar.gz
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
Diffstat (limited to 'util.c')
-rw-r--r--util.c18
1 files 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
/****************************************************************
*