From 93446fedd3ae3efcccdeede1a5af1ec7e8b6d1cd Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 30 Jun 2015 08:28:28 +0000 Subject: io.c: reopen OS encoding path * io.c (rb_io_reopen): freopen(3) with OS encoding path. [ruby-core:69780] [Bug #11320] * win32/file.c (rb_freopen): wrapper of wchar version freopen(3). use _wfreopen_s() if available. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51069 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- win32/file.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'win32') diff --git a/win32/file.c b/win32/file.c index 4a31fe37eb..799810a4ef 100644 --- a/win32/file.c +++ b/win32/file.c @@ -721,6 +721,28 @@ rb_file_load_ok(const char *path) return ret; } +int +rb_freopen(VALUE fname, const char *mode, FILE *file) +{ + WCHAR *wname, wmode[4]; + int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0); + if (n > numberof(wmode)) return EINVAL; + MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode)); + wname = rb_w32_mbstr_to_wstr(CP_UTF8, RSTRING_PTR(fname), + rb_long2int(RSTRING_LEN(fname)), NULL); + RB_GC_GUARD(fname); +#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S) + e = _wfreopen(wname, wmode, file) ? 0 : errno; +#else + { + FILE *newfp = 0; + e = _wfreopen_s(&newfp, wname, wmode, file); + } +#endif + xfree(wname); + return e; +} + void Init_w32_codepage(void) { -- cgit v1.2.3