Skip to content

Commit e84edd2

Browse files
piscisaureusBert Belder
authored and
Bert Belder
committed
Win: make process.cwd and chdir support non-ansi characters
Closes GH-2215
1 parent 823a443 commit e84edd2

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/node.cc

+15-10
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
#include <unistd.h> /* setuid, getuid */
4848
#else
4949
#include <direct.h>
50-
#define chdir _chdir
51-
#define getcwd _getcwd
5250
#include <process.h>
5351
#define getpid _getpid
5452
#include <io.h>
@@ -1231,10 +1229,10 @@ static Handle<Value> Chdir(const Arguments& args) {
12311229

12321230
String::Utf8Value path(args[0]->ToString());
12331231

1234-
int r = chdir(*path);
1232+
uv_err_t r = uv_chdir(*path);
12351233

1236-
if (r != 0) {
1237-
return ThrowException(Exception::Error(String::New(strerror(errno))));
1234+
if (r.code != UV_OK) {
1235+
return ThrowException(UVException(r.code, "uv_chdir"));
12381236
}
12391237

12401238
return Undefined();
@@ -1243,18 +1241,25 @@ static Handle<Value> Chdir(const Arguments& args) {
12431241

12441242
static Handle<Value> Cwd(const Arguments& args) {
12451243
HandleScope scope;
1244+
#ifdef _WIN32
1245+
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
1246+
char buf[MAX_PATH * 4 + 1];
1247+
#else
1248+
char buf[PATH_MAX + 1];
1249+
#endif
12461250

1247-
char *r = getcwd(getbuf, ARRAY_SIZE(getbuf) - 1);
1248-
if (r == NULL) {
1249-
return ThrowException(Exception::Error(String::New(strerror(errno))));
1251+
uv_err_t r = uv_cwd(buf, ARRAY_SIZE(buf) - 1);
1252+
if (r.code != UV_OK) {
1253+
return ThrowException(UVException(r.code, "uv_cwd"));
12501254
}
12511255

1252-
getbuf[ARRAY_SIZE(getbuf) - 1] = '\0';
1253-
Local<String> cwd = String::New(r);
1256+
buf[ARRAY_SIZE(buf) - 1] = '\0';
1257+
Local<String> cwd = String::New(buf);
12541258

12551259
return scope.Close(cwd);
12561260
}
12571261

1262+
12581263
#ifdef _WIN32
12591264
static Handle<Value> CwdForDrive(const Arguments& args) {
12601265
HandleScope scope;

test/simple/test-chdir.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,20 @@
2121

2222
var common = require('../common');
2323
var assert = require('assert');
24+
var fs = require('fs');
25+
var path = require('path');
2426

2527
assert.equal(true, process.cwd() !== __dirname);
2628

2729
process.chdir(__dirname);
28-
2930
assert.equal(true, process.cwd() === __dirname);
31+
32+
var dir = path.resolve(common.fixturesDir,
33+
'weird \uc3a4\uc3ab\uc3af characters \u00e1\u00e2\u00e3');
34+
fs.mkdirSync(dir);
35+
process.chdir(dir);
36+
assert(process.cwd() == dir);
37+
38+
process.chdir('..');
39+
assert(process.cwd() == path.resolve(common.fixturesDir));
40+
fs.rmdirSync(dir);

0 commit comments

Comments
 (0)