revert r62797, r62784. [Bug #13863]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@62801 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nagachika 2018-03-18 03:57:37 +00:00
parent 2171d75e9b
commit 3c5452dba1
10 changed files with 75 additions and 152 deletions

37
dir.c
View file

@ -74,7 +74,6 @@ char *strchr(char*,char);
#define rmdir(p) rb_w32_urmdir(p) #define rmdir(p) rb_w32_urmdir(p)
#undef opendir #undef opendir
#define opendir(p) rb_w32_uopendir(p) #define opendir(p) rb_w32_uopendir(p)
#define ruby_getcwd() rb_w32_ugetcwd(NULL, 0)
#define IS_WIN32 1 #define IS_WIN32 1
#else #else
#define IS_WIN32 0 #define IS_WIN32 0
@ -1049,52 +1048,26 @@ dir_s_chdir(int argc, VALUE *argv, VALUE obj)
} }
VALUE VALUE
rb_dir_getwd_ospath(void) rb_dir_getwd(void)
{ {
char *path; char *path;
VALUE cwd; VALUE cwd;
VALUE path_guard; int fsenc = rb_enc_to_index(rb_filesystem_encoding());
#undef RUBY_UNTYPED_DATA_WARNING if (fsenc == ENCINDEX_US_ASCII) fsenc = ENCINDEX_ASCII;
#define RUBY_UNTYPED_DATA_WARNING 0
path_guard = Data_Wrap_Struct((VALUE)0, NULL, RUBY_DEFAULT_FREE, NULL);
path = my_getcwd(); path = my_getcwd();
DATA_PTR(path_guard) = path; #ifdef __APPLE__
#ifdef _WIN32
cwd = rb_utf8_str_new_cstr(path);
OBJ_TAINT(cwd);
#elif defined __APPLE__
cwd = rb_str_normalize_ospath(path, strlen(path)); cwd = rb_str_normalize_ospath(path, strlen(path));
OBJ_TAINT(cwd); OBJ_TAINT(cwd);
#else #else
cwd = rb_tainted_str_new2(path); cwd = rb_tainted_str_new2(path);
#endif #endif
DATA_PTR(path_guard) = 0; rb_enc_associate_index(cwd, fsenc);
xfree(path); xfree(path);
return cwd; 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: * call-seq:
* Dir.getwd -> string * Dir.getwd -> string

32
file.c
View file

@ -126,14 +126,6 @@ int flock(int, int);
#define STAT(p, s) stat((p), (s)) #define STAT(p, s) stat((p), (s))
#endif #endif
#if defined _WIN32 || defined __APPLE__
# define USE_OSPATH 1
# define TO_OSPATH(str) rb_str_encode_ospath(str)
#else
# define USE_OSPATH 0
# define TO_OSPATH(str) (str)
#endif
VALUE rb_cFile; VALUE rb_cFile;
VALUE rb_mFileTest; VALUE rb_mFileTest;
VALUE rb_cStat; VALUE rb_cStat;
@ -230,7 +222,7 @@ rb_get_path(VALUE obj)
VALUE VALUE
rb_str_encode_ospath(VALUE path) rb_str_encode_ospath(VALUE path)
{ {
#if USE_OSPATH #if defined _WIN32 || defined __APPLE__
int encidx = ENCODING_GET(path); int encidx = ENCODING_GET(path);
#ifdef _WIN32 #ifdef _WIN32
if (encidx == ENCINDEX_ASCII) { if (encidx == ENCINDEX_ASCII) {
@ -3841,10 +3833,11 @@ realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved,
else { else {
struct stat sbuf; struct stat sbuf;
int ret; int ret;
VALUE testpath2 = rb_str_encode_ospath(testpath);
#ifdef __native_client__ #ifdef __native_client__
ret = stat(RSTRING_PTR(testpath), &sbuf); ret = stat(RSTRING_PTR(testpath2), &sbuf);
#else #else
ret = lstat(RSTRING_PTR(testpath), &sbuf); ret = lstat(RSTRING_PTR(testpath2), &sbuf);
#endif #endif
if (ret == -1) { if (ret == -1) {
int e = errno; int e = errno;
@ -3916,12 +3909,9 @@ rb_check_realpath_internal(VALUE basedir, VALUE path, enum rb_realpath_mode mode
if (!NIL_P(basedir)) { if (!NIL_P(basedir)) {
FilePathValue(basedir); FilePathValue(basedir);
basedir = TO_OSPATH(rb_str_dup_frozen(basedir)); basedir = rb_str_dup_frozen(basedir);
} }
enc = rb_enc_get(unresolved_path);
origenc = enc;
unresolved_path = TO_OSPATH(unresolved_path);
RSTRING_GETMEM(unresolved_path, ptr, len); RSTRING_GETMEM(unresolved_path, ptr, len);
path_names = skipprefixroot(ptr, ptr + len, rb_enc_get(unresolved_path)); path_names = skipprefixroot(ptr, ptr + len, rb_enc_get(unresolved_path));
if (ptr != path_names) { if (ptr != path_names) {
@ -3938,7 +3928,7 @@ rb_check_realpath_internal(VALUE basedir, VALUE path, enum rb_realpath_mode mode
} }
} }
curdir = rb_dir_getwd_ospath(); curdir = rb_dir_getwd();
RSTRING_GETMEM(curdir, ptr, len); RSTRING_GETMEM(curdir, ptr, len);
curdir_names = skipprefixroot(ptr, ptr + len, rb_enc_get(curdir)); curdir_names = skipprefixroot(ptr, ptr + len, rb_enc_get(curdir));
resolved = rb_str_subseq(curdir, 0, curdir_names - ptr); resolved = rb_str_subseq(curdir, 0, curdir_names - ptr);
@ -3946,6 +3936,7 @@ rb_check_realpath_internal(VALUE basedir, VALUE path, enum rb_realpath_mode mode
root_found: root_found:
RSTRING_GETMEM(resolved, prefixptr, prefixlen); RSTRING_GETMEM(resolved, prefixptr, prefixlen);
pend = prefixptr + prefixlen; pend = prefixptr + prefixlen;
enc = rb_enc_get(resolved);
ptr = chompdirsep(prefixptr, pend, enc); ptr = chompdirsep(prefixptr, pend, enc);
if (ptr < pend) { if (ptr < pend) {
prefixlen = ++ptr - prefixptr; prefixlen = ++ptr - prefixptr;
@ -3960,6 +3951,7 @@ rb_check_realpath_internal(VALUE basedir, VALUE path, enum rb_realpath_mode mode
} }
#endif #endif
origenc = enc;
switch (rb_enc_to_index(enc)) { switch (rb_enc_to_index(enc)) {
case ENCINDEX_ASCII: case ENCINDEX_ASCII:
case ENCINDEX_US_ASCII: case ENCINDEX_US_ASCII:
@ -3978,14 +3970,8 @@ rb_check_realpath_internal(VALUE basedir, VALUE path, enum rb_realpath_mode mode
if (realpath_rec(&prefixlen, &resolved, path_names, loopcheck, mode, 1)) if (realpath_rec(&prefixlen, &resolved, path_names, loopcheck, mode, 1))
return Qnil; return Qnil;
if (origenc != rb_enc_get(resolved)) { if (origenc != enc && rb_enc_str_asciionly_p(resolved))
if (rb_enc_str_asciionly_p(resolved)) {
rb_enc_associate(resolved, origenc); rb_enc_associate(resolved, origenc);
}
else {
resolved = rb_str_conv_enc(resolved, NULL, origenc);
}
}
OBJ_TAINT(resolved); OBJ_TAINT(resolved);
return resolved; return resolved;

View file

@ -982,9 +982,6 @@ void ruby_register_rollback_func_for_ensure(VALUE (*ensure_func)(ANYARGS), VALUE
/* debug.c */ /* debug.c */
PRINTF_ARGS(void ruby_debug_printf(const char*, ...), 1, 2); PRINTF_ARGS(void ruby_debug_printf(const char*, ...), 1, 2);
/* dir.c */
VALUE rb_dir_getwd_ospath(void);
/* dmyext.c */ /* dmyext.c */
void Init_enc(void); void Init_enc(void);
void Init_ext(void); void Init_ext(void);

13
load.c
View file

@ -95,6 +95,15 @@ rb_construct_expanded_load_path(enum expand_type type, int *has_relative, int *h
rb_ary_replace(vm->load_path_snapshot, vm->load_path); rb_ary_replace(vm->load_path_snapshot, vm->load_path);
} }
static VALUE
load_path_getcwd(void)
{
char *cwd = my_getcwd();
VALUE cwd_str = rb_filesystem_str_new_cstr(cwd);
xfree(cwd);
return cwd_str;
}
VALUE VALUE
rb_get_expanded_load_path(void) rb_get_expanded_load_path(void)
{ {
@ -106,7 +115,7 @@ rb_get_expanded_load_path(void)
int has_relative = 0, has_non_cache = 0; int has_relative = 0, has_non_cache = 0;
rb_construct_expanded_load_path(EXPAND_ALL, &has_relative, &has_non_cache); rb_construct_expanded_load_path(EXPAND_ALL, &has_relative, &has_non_cache);
if (has_relative) { if (has_relative) {
vm->load_path_check_cache = rb_dir_getwd_ospath(); vm->load_path_check_cache = load_path_getcwd();
} }
else if (has_non_cache) { else if (has_non_cache) {
/* Non string object. */ /* Non string object. */
@ -124,7 +133,7 @@ rb_get_expanded_load_path(void)
} }
else if (vm->load_path_check_cache) { else if (vm->load_path_check_cache) {
int has_relative = 1, has_non_cache = 1; int has_relative = 1, has_non_cache = 1;
VALUE cwd = rb_dir_getwd_ospath(); VALUE cwd = load_path_getcwd();
if (!rb_str_equal(vm->load_path_check_cache, cwd)) { if (!rb_str_equal(vm->load_path_check_cache, cwd)) {
/* Current working directory or filesystem encoding was changed. /* Current working directory or filesystem encoding was changed.
Expand relative load path and non-cacheable objects again. */ Expand relative load path and non-cacheable objects again. */

65
ruby.c
View file

@ -175,10 +175,8 @@ cmdline_options_init(ruby_cmdline_options_t *opt)
return opt; return opt;
} }
static NODE *load_file(VALUE parser, VALUE fname, VALUE f, int script, static NODE *load_file(VALUE, VALUE, int, ruby_cmdline_options_t *);
ruby_cmdline_options_t *opt); static void forbid_setid(const char *, ruby_cmdline_options_t *);
static VALUE open_load_file(VALUE fname_v, int *xflag);
static void forbid_setid(const char *, const ruby_cmdline_options_t *);
#define forbid_setid(s) forbid_setid((s), opt) #define forbid_setid(s) forbid_setid((s), opt)
static struct { static struct {
@ -425,8 +423,6 @@ str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE, ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE,
Qnil); Qnil);
} }
#else
# define str_conv_enc(str, from, to) (str)
#endif #endif
void ruby_init_loadpath_safe(int safe_level); void ruby_init_loadpath_safe(int safe_level);
@ -1054,7 +1050,6 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt)
case 'x': case 'x':
if (envopt) goto noenvopt; if (envopt) goto noenvopt;
forbid_setid("-x");
opt->xflag = TRUE; opt->xflag = TRUE;
s++; s++;
if (*s && chdir(s) < 0) { if (*s && chdir(s) < 0) {
@ -1444,7 +1439,6 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
{ {
NODE *tree = 0; NODE *tree = 0;
VALUE parser; VALUE parser;
VALUE script_name;
const rb_iseq_t *iseq; const rb_iseq_t *iseq;
rb_encoding *enc, *lenc; rb_encoding *enc, *lenc;
#if UTF8_PATH #if UTF8_PATH
@ -1520,9 +1514,6 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
argc--; argc--;
argv++; argv++;
} }
if (opt->script[0] == '-' && !opt->script[1]) {
forbid_setid("program input from stdin");
}
} }
opt->script_name = rb_str_new_cstr(opt->script); opt->script_name = rb_str_new_cstr(opt->script);
@ -1569,17 +1560,9 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
ienc = enc; ienc = enc;
#endif #endif
} }
script_name = opt->script_name; rb_enc_associate(opt->script_name, lenc);
rb_enc_associate(opt->script_name,
IF_UTF8_PATH(uenc = rb_utf8_encoding(), lenc));
#if UTF8_PATH
if (uenc != lenc) {
opt->script_name = str_conv_enc(opt->script_name, uenc, lenc);
opt->script = RSTRING_PTR(opt->script_name);
}
#endif
rb_obj_freeze(opt->script_name); rb_obj_freeze(opt->script_name);
if (IF_UTF8_PATH(uenc != lenc, 1)) { if (IF_UTF8_PATH((uenc = rb_utf8_encoding()) != lenc, 1)) {
long i; long i;
VALUE load_path = GET_VM()->load_path; VALUE load_path = GET_VM()->load_path;
const ID id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK; const ID id_initial_load_path_mark = INITIAL_LOAD_PATH_MARK;
@ -1615,6 +1598,12 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
rb_funcallv(rb_cISeq, rb_intern_const("compile_option="), 1, &option); rb_funcallv(rb_cISeq, rb_intern_const("compile_option="), 1, &option);
#undef SET_COMPILE_OPTION #undef SET_COMPILE_OPTION
} }
#if UTF8_PATH
if (uenc != lenc) {
opt->script_name = str_conv_enc(opt->script_name, uenc, lenc);
opt->script = RSTRING_PTR(opt->script_name);
}
#endif
ruby_set_argv(argc, argv); ruby_set_argv(argc, argv);
process_sflag(&opt->sflag); process_sflag(&opt->sflag);
@ -1652,11 +1641,13 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
tree = rb_parser_compile_string(parser, opt->script, opt->e_script, 1); tree = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
} }
else { else {
VALUE f; if (opt->script[0] == '-' && !opt->script[1]) {
forbid_setid("program input from stdin");
}
base_block = toplevel_context(toplevel_binding); base_block = toplevel_context(toplevel_binding);
rb_parser_set_context(parser, base_block, TRUE); rb_parser_set_context(parser, base_block, TRUE);
f = open_load_file(script_name, &opt->xflag); tree = load_file(parser, opt->script_name, 1, opt);
tree = load_file(parser, opt->script_name, f, 1, opt);
} }
ruby_set_script_name(opt->script_name); ruby_set_script_name(opt->script_name);
if (dump & DUMP_BIT(yydebug)) { if (dump & DUMP_BIT(yydebug)) {
@ -1713,7 +1704,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
{ {
VALUE path = Qnil; VALUE path = Qnil;
if (!opt->e_script && strcmp(opt->script, "-")) { if (!opt->e_script && strcmp(opt->script, "-")) {
path = rb_realpath_internal(Qnil, script_name, 1); path = rb_realpath_internal(Qnil, opt->script_name, 1);
} }
base_block = toplevel_context(toplevel_binding); base_block = toplevel_context(toplevel_binding);
iseq = rb_iseq_new_main(tree, opt->script_name, path, vm_block_iseq(base_block)); iseq = rb_iseq_new_main(tree, opt->script_name, path, vm_block_iseq(base_block));
@ -1752,6 +1743,7 @@ struct load_file_arg {
VALUE parser; VALUE parser;
VALUE fname; VALUE fname;
int script; int script;
int xflag;
ruby_cmdline_options_t *opt; ruby_cmdline_options_t *opt;
VALUE f; VALUE f;
}; };
@ -1769,6 +1761,7 @@ load_file_internal(VALUE argp_v)
NODE *tree = 0; NODE *tree = 0;
rb_encoding *enc; rb_encoding *enc;
ID set_encoding; ID set_encoding;
int xflag = argp->xflag;
argp->script = 0; argp->script = 0;
CONST_ID(set_encoding, "set_encoding"); CONST_ID(set_encoding, "set_encoding");
@ -1784,9 +1777,11 @@ load_file_internal(VALUE argp_v)
enc = rb_ascii8bit_encoding(); enc = rb_ascii8bit_encoding();
rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc)); rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc));
if (opt->xflag) { if (xflag || opt->xflag) {
line_start--; line_start--;
search_shebang: search_shebang:
forbid_setid("-x");
opt->xflag = FALSE;
while (!NIL_P(line = rb_io_gets(f))) { while (!NIL_P(line = rb_io_gets(f))) {
line_start++; line_start++;
RSTRING_GETMEM(line, str, len); RSTRING_GETMEM(line, str, len);
@ -1879,8 +1874,7 @@ load_file_internal(VALUE argp_v)
static VALUE static VALUE
open_load_file(VALUE fname_v, int *xflag) open_load_file(VALUE fname_v, int *xflag)
{ {
const char *fname = (fname_v = rb_str_encode_ospath(fname_v), const char *fname = StringValueCStr(fname_v);
StringValueCStr(fname_v));
long flen = RSTRING_LEN(fname_v); long flen = RSTRING_LEN(fname_v);
VALUE f; VALUE f;
int e; int e;
@ -1981,14 +1975,15 @@ restore_load_file(VALUE arg)
} }
static NODE * static NODE *
load_file(VALUE parser, VALUE fname, VALUE f, int script, ruby_cmdline_options_t *opt) load_file(VALUE parser, VALUE fname, int script, ruby_cmdline_options_t *opt)
{ {
struct load_file_arg arg; struct load_file_arg arg;
arg.parser = parser; arg.parser = parser;
arg.fname = fname; arg.fname = fname;
arg.script = script; arg.script = script;
arg.opt = opt; arg.opt = opt;
arg.f = f; arg.xflag = 0;
arg.f = open_load_file(rb_str_encode_ospath(fname), &arg.xflag);
return (NODE *)rb_ensure(load_file_internal, (VALUE)&arg, return (NODE *)rb_ensure(load_file_internal, (VALUE)&arg,
restore_load_file, (VALUE)&arg); restore_load_file, (VALUE)&arg);
} }
@ -2003,15 +1998,17 @@ rb_load_file(const char *fname)
void * void *
rb_load_file_str(VALUE fname_v) rb_load_file_str(VALUE fname_v)
{ {
return rb_parser_load_file(rb_parser_new(), fname_v); ruby_cmdline_options_t opt;
return load_file(rb_parser_new(), fname_v, 0, cmdline_options_init(&opt));
} }
void * void *
rb_parser_load_file(VALUE parser, VALUE fname_v) rb_parser_load_file(VALUE parser, VALUE fname_v)
{ {
ruby_cmdline_options_t opt; ruby_cmdline_options_t opt;
VALUE f = open_load_file(fname_v, &cmdline_options_init(&opt)->xflag);
return load_file(parser, fname_v, f, 0, &opt); return load_file(parser, fname_v, 0, cmdline_options_init(&opt));
} }
/* /*
@ -2120,7 +2117,7 @@ init_ids(ruby_cmdline_options_t *opt)
#undef forbid_setid #undef forbid_setid
static void static void
forbid_setid(const char *s, const ruby_cmdline_options_t *opt) forbid_setid(const char *s, ruby_cmdline_options_t *opt)
{ {
if (opt->setids & 1) if (opt->setids & 1)
rb_raise(rb_eSecurityError, "no %s allowed while running setuid", s); rb_raise(rb_eSecurityError, "no %s allowed while running setuid", s);

View file

@ -920,16 +920,4 @@ class TestRubyOptions < Test::Unit::TestCase
end end
end end
end end
def test_cwd_encoding
with_tmpchdir do
testdir = "\u30c6\u30b9\u30c8"
Dir.mkdir(testdir)
Dir.chdir(testdir) do
File.write("a.rb", "require './b'")
File.write("b.rb", "puts 'ok'")
assert_ruby_status([{"RUBYLIB"=>"."}, *%w[-E cp932:utf-8 a.rb]])
end
end
end
end end

8
util.c
View file

@ -511,10 +511,7 @@ ruby_getcwd(void)
char *buf = xmalloc(2); char *buf = xmalloc(2);
strcpy(buf, "."); strcpy(buf, ".");
#elif defined HAVE_GETCWD #elif defined HAVE_GETCWD
# undef RUBY_UNTYPED_DATA_WARNING
# define RUBY_UNTYPED_DATA_WARNING 0
# if defined NO_GETCWD_MALLOC # if defined NO_GETCWD_MALLOC
VALUE guard = Data_Wrap_Struct((VALUE)0, NULL, RUBY_DEFAULT_FREE, NULL);
int size = 200; int size = 200;
char *buf = xmalloc(size); char *buf = xmalloc(size);
@ -522,22 +519,17 @@ ruby_getcwd(void)
int e = errno; int e = errno;
if (e != ERANGE) { if (e != ERANGE) {
xfree(buf); xfree(buf);
DATA_PTR(guard) = NULL;
rb_syserr_fail(e, "getcwd"); rb_syserr_fail(e, "getcwd");
} }
size *= 2; size *= 2;
DATA_PTR(guard) = buf;
buf = xrealloc(buf, size); buf = xrealloc(buf, size);
} }
# else # else
VALUE guard = Data_Wrap_Struct((VALUE)0, NULL, free, NULL);
char *buf, *cwd = getcwd(NULL, 0); char *buf, *cwd = getcwd(NULL, 0);
DATA_PTR(guard) = cwd;
if (!cwd) rb_sys_fail("getcwd"); if (!cwd) rb_sys_fail("getcwd");
buf = ruby_strdup(cwd); /* allocate by xmalloc */ buf = ruby_strdup(cwd); /* allocate by xmalloc */
free(cwd); free(cwd);
# endif # endif
DATA_PTR(RB_GC_GUARD(guard)) = NULL;
#else #else
# ifndef PATH_MAX # ifndef PATH_MAX
# define PATH_MAX 8192 # define PATH_MAX 8192

View file

@ -1,6 +1,6 @@
#define RUBY_VERSION "2.4.4" #define RUBY_VERSION "2.4.4"
#define RUBY_RELEASE_DATE "2018-03-17" #define RUBY_RELEASE_DATE "2018-03-17"
#define RUBY_PATCHLEVEL 261 #define RUBY_PATCHLEVEL 259
#define RUBY_RELEASE_YEAR 2018 #define RUBY_RELEASE_YEAR 2018
#define RUBY_RELEASE_MONTH 3 #define RUBY_RELEASE_MONTH 3

View file

@ -33,7 +33,6 @@ long rb_w32_telldir(DIR *);
void rb_w32_seekdir(DIR *, long); void rb_w32_seekdir(DIR *, long);
void rb_w32_rewinddir(DIR *); void rb_w32_rewinddir(DIR *);
void rb_w32_closedir(DIR *); void rb_w32_closedir(DIR *);
char *rb_w32_ugetcwd(char *, int);
#define opendir(s) rb_w32_opendir((s)) #define opendir(s) rb_w32_opendir((s))
#define readdir(d) rb_w32_readdir((d), 0) #define readdir(d) rb_w32_readdir((d), 0)

View file

@ -4654,61 +4654,43 @@ clock_getres(clockid_t clock_id, struct timespec *sp)
} }
/* License: Ruby's */ /* License: Ruby's */
static char * char *
w32_getcwd(char *buffer, int size, UINT cp) rb_w32_getcwd(char *buffer, int size)
{ {
WCHAR *p; char *p = buffer;
int wlen, len; int len;
len = GetCurrentDirectoryW(0, NULL); len = GetCurrentDirectory(0, NULL);
if (!len) { if (!len) {
errno = map_errno(GetLastError()); errno = map_errno(GetLastError());
return NULL; return NULL;
} }
if (buffer && size < len) { if (p) {
errno = ERANGE;
return NULL;
}
p = ALLOCA_N(WCHAR, len);
if (!GetCurrentDirectoryW(len, p)) {
errno = map_errno(GetLastError());
return NULL;
}
wlen = translate_wchar(p, L'\\', L'/') - p + 1;
len = WideCharToMultiByte(cp, 0, p, wlen, NULL, 0, NULL, NULL);
if (buffer) {
if (size < len) { if (size < len) {
errno = ERANGE; errno = ERANGE;
return NULL; return NULL;
} }
} }
else { else {
buffer = malloc(len); p = malloc(len);
if (!buffer) { size = len;
if (!p) {
errno = ENOMEM; errno = ENOMEM;
return NULL; return NULL;
} }
} }
WideCharToMultiByte(cp, 0, p, wlen, buffer, len, NULL, NULL);
return buffer; if (!GetCurrentDirectory(size, p)) {
errno = map_errno(GetLastError());
if (!buffer)
free(p);
return NULL;
} }
/* License: Ruby's */ translate_char(p, '\\', '/', filecp());
char *
rb_w32_getcwd(char *buffer, int size)
{
return w32_getcwd(buffer, size, filecp());
}
/* License: Ruby's */ return p;
char *
rb_w32_ugetcwd(char *buffer, int size)
{
return w32_getcwd(buffer, size, CP_UTF8);
} }
/* License: Artistic or GPL */ /* License: Artistic or GPL */