diff options
author | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-03-16 17:34:44 +0000 |
---|---|---|
committer | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-03-16 17:34:44 +0000 |
commit | 2368dbdfa3c438b614a4727bde7cacac2d0a87a9 (patch) | |
tree | a3580abe631e47b8fc161eb5085587023cf19bfa /dir.c | |
parent | 3ca42c0435512e44f4662152afa62100be968c8f (diff) |
merge revision(s) 58745,58780,59040,60743: [Backport #13863]
rb_w32_ugetcwd: UTF-8 version getcwd
* dir.c (rb_dir_getwd): convert from UTF-8.
* win32/win32.c (w32_getcwd): codepage aware getcwd using
GetCurrentDirectoryW.
potential memory leak
* dir.c (rb_dir_getwd): get rid of potential memory leak.
* util.c (ruby_getcwd): ditto.
file.c: realpath in OS path encoding
* dir.c (rb_dir_getwd_ospath): return cwd path in the OS path
encoding.
* file.c (rb_realpath_internal): work in the OS path encoding
load.c: cwd encoding
* load.c (rb_get_expanded_load_path): save cwd cache in OS path
encoding, to get rid of unnecessary conversion and infinite
loading when it needs encoding conversion.
[ruby-dev:50221] [Bug #13863]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@62784 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'dir.c')
-rw-r--r-- | dir.c | 37 |
1 files changed, 32 insertions, 5 deletions
@@ -74,6 +74,7 @@ char *strchr(char*,char); #define rmdir(p) rb_w32_urmdir(p) #undef opendir #define opendir(p) rb_w32_uopendir(p) +#define ruby_getcwd() rb_w32_ugetcwd(NULL, 0) #define IS_WIN32 1 #else #define IS_WIN32 0 @@ -1048,26 +1049,52 @@ dir_s_chdir(int argc, VALUE *argv, VALUE obj) } VALUE -rb_dir_getwd(void) +rb_dir_getwd_ospath(void) { char *path; VALUE cwd; - int fsenc = rb_enc_to_index(rb_filesystem_encoding()); + VALUE path_guard; - if (fsenc == ENCINDEX_US_ASCII) fsenc = ENCINDEX_ASCII; +#undef RUBY_UNTYPED_DATA_WARNING +#define RUBY_UNTYPED_DATA_WARNING 0 + path_guard = Data_Wrap_Struct((VALUE)0, NULL, RUBY_DEFAULT_FREE, NULL); path = my_getcwd(); -#ifdef __APPLE__ + DATA_PTR(path_guard) = path; +#ifdef _WIN32 + cwd = rb_utf8_str_new_cstr(path); + OBJ_TAINT(cwd); +#elif defined __APPLE__ cwd = rb_str_normalize_ospath(path, strlen(path)); OBJ_TAINT(cwd); #else cwd = rb_tainted_str_new2(path); #endif - rb_enc_associate_index(cwd, fsenc); + DATA_PTR(path_guard) = 0; xfree(path); return cwd; } +VALUE +rb_dir_getwd(void) +{ + rb_encoding *fs = rb_filesystem_encoding(); + int fsenc = rb_enc_to_index(fs); + VALUE cwd = rb_dir_getwd_ospath(); + + switch (fsenc) { + case ENCINDEX_US_ASCII: + fsenc = ENCINDEX_ASCII; + case ENCINDEX_ASCII: + break; +#if defined _WIN32 || defined __APPLE__ + default: + return rb_str_conv_enc(cwd, NULL, fs); +#endif + } + return rb_enc_associate_index(cwd, fsenc); +} + /* * call-seq: * Dir.getwd -> string |