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
This commit is contained in:
nobu 2014-04-16 08:46:29 +00:00
Родитель 9f4f9048a0
Коммит 17eb86eb12
1 изменённых файлов: 9 добавлений и 9 удалений

18
util.c
Просмотреть файл

@ -467,19 +467,14 @@ ruby_strdup(const char *str)
return tmp; return tmp;
} }
#ifdef __native_client__
char * char *
ruby_getcwd(void) ruby_getcwd(void)
{ {
#if defined __native_client__
char *buf = xmalloc(2); char *buf = xmalloc(2);
strcpy(buf, "."); strcpy(buf, ".");
return buf; #elif defined HAVE_GETCWD
} # if defined NO_GETCWD_MALLOC
#else
char *
ruby_getcwd(void)
{
#ifdef HAVE_GETCWD
int size = 200; int size = 200;
char *buf = xmalloc(size); char *buf = xmalloc(size);
@ -491,6 +486,12 @@ ruby_getcwd(void)
size *= 2; size *= 2;
buf = xrealloc(buf, size); 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 #else
# ifndef PATH_MAX # ifndef PATH_MAX
# define PATH_MAX 8192 # define PATH_MAX 8192
@ -504,7 +505,6 @@ ruby_getcwd(void)
#endif #endif
return buf; return buf;
} }
#endif
/**************************************************************** /****************************************************************
* *