Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Win: make process.cwd and chdir support non-ansi characters
Browse files Browse the repository at this point in the history
Closes GH-2215
  • Loading branch information
piscisaureus committed Dec 1, 2011
1 parent cf6cd5e commit 7a9b4c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
25 changes: 15 additions & 10 deletions src/node.cc
Expand Up @@ -47,8 +47,6 @@
#include <unistd.h> /* setuid, getuid */
#else
#include <direct.h>
#define chdir _chdir
#define getcwd _getcwd
#include <process.h>
#define getpid _getpid
#include <io.h>
Expand Down Expand Up @@ -1231,10 +1229,10 @@ static Handle<Value> Chdir(const Arguments& args) {

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

int r = chdir(*path);
uv_err_t r = uv_chdir(*path);

if (r != 0) {
return ThrowException(Exception::Error(String::New(strerror(errno))));
if (r.code != UV_OK) {
return ThrowException(UVException(r.code, "uv_chdir"));
}

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

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

char *r = getcwd(getbuf, ARRAY_SIZE(getbuf) - 1);
if (r == NULL) {
return ThrowException(Exception::Error(String::New(strerror(errno))));
uv_err_t r = uv_cwd(buf, ARRAY_SIZE(buf) - 1);
if (r.code != UV_OK) {
return ThrowException(UVException(r.code, "uv_cwd"));
}

getbuf[ARRAY_SIZE(getbuf) - 1] = '\0';
Local<String> cwd = String::New(r);
buf[ARRAY_SIZE(buf) - 1] = '\0';
Local<String> cwd = String::New(buf);

return scope.Close(cwd);
}


#ifdef _WIN32
static Handle<Value> CwdForDrive(const Arguments& args) {
HandleScope scope;
Expand Down
13 changes: 12 additions & 1 deletion test/simple/test-chdir.js
Expand Up @@ -21,9 +21,20 @@

var common = require('../common');
var assert = require('assert');
var fs = require('fs');
var path = require('path');

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

process.chdir(__dirname);

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

var dir = path.resolve(common.fixturesDir,
'weird \uc3a4\uc3ab\uc3af characters \u00e1\u00e2\u00e3');
fs.mkdirSync(dir);
process.chdir(dir);
assert(process.cwd() == dir);

process.chdir('..');
assert(process.cwd() == path.resolve(common.fixturesDir));
fs.rmdirSync(dir);

0 comments on commit 7a9b4c9

Please sign in to comment.