Skip to content

Commit

Permalink
os: add homedir()
Browse files Browse the repository at this point in the history
os.homedir() calls libuv's uv_os_homedir() to retrieve the current
user's home directory.
  • Loading branch information
cjihrig committed May 25, 2015
1 parent 6887116 commit e7e5992
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/api/os.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Use `require('os')` to access this module.

Returns the operating system's default directory for temporary files.

## os.homedir()

Returns the home directory of the current user.

## os.endianness()

Returns the endianness of the CPU. Possible values are `'BE'` for big endian
Expand Down
2 changes: 2 additions & 0 deletions lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ exports.cpus = binding.getCPUs;
exports.type = binding.getOSType;
exports.release = binding.getOSRelease;
exports.networkInterfaces = binding.getInterfaceAddresses;
exports.homedir = binding.getHomeDirectory;


exports.arch = function() {
return process.arch;
Expand Down
24 changes: 24 additions & 0 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,29 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
}


static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
#ifdef _WIN32
char buf[MAX_PATH];
#else
char buf[PATH_MAX];
#endif

size_t len = sizeof(buf);
int err = uv_os_homedir(buf, &len);

if (err) {
return env->ThrowUVException(err, "uv_os_homedir");
}

Local<String> home = String::NewFromUtf8(env->isolate(),
buf,
String::kNormalString,
len);
args.GetReturnValue().Set(home);
}


void Initialize(Handle<Object> target,
Handle<Value> unused,
Handle<Context> context) {
Expand All @@ -284,6 +307,7 @@ void Initialize(Handle<Object> target,
env->SetMethod(target, "getOSType", GetOSType);
env->SetMethod(target, "getOSRelease", GetOSRelease);
env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses);
env->SetMethod(target, "getHomeDirectory", GetHomeDirectory);
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "isBigEndian"),
Boolean::New(env->isolate(), IsBigEndian()));
}
Expand Down
15 changes: 14 additions & 1 deletion test/parallel/test-os.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
var common = require('../common');
var assert = require('assert');
var os = require('os');
var isWindows = process.platform === 'win32';


process.env.TMPDIR = '/tmpdir';
process.env.TMP = '/tmp';
process.env.TEMP = '/temp';
if (process.platform === 'win32') {
if (isWindows) {
assert.equal(os.tmpdir(), '/temp');
process.env.TEMP = '';
assert.equal(os.tmpdir(), '/tmp');
Expand Down Expand Up @@ -101,3 +102,15 @@ switch (platform) {

var EOL = os.EOL;
assert.ok(EOL.length > 0);


var home = os.homedir();

console.log('homedir = ' + home);
assert.ok(home.length > 0);

if (isWindows && process.env.USERPROFILE) {
assert.equal(home, process.env.USERPROFILE);
} else if (!isWindows && process.env.HOME) {
assert.equal(home, process.env.HOME);
}

0 comments on commit e7e5992

Please sign in to comment.