From 8ea6adcae63d7114e9765d471168fb19d2818863 Mon Sep 17 00:00:00 2001 From: Michael Carter Date: Tue, 1 Sep 2009 11:39:30 +0200 Subject: [PATCH] Feature: add node.cwd() to access the current working directory. --- src/node.cc | 18 ++++++++++++++++++ website/api.txt | 2 ++ 2 files changed, 20 insertions(+) diff --git a/src/node.cc b/src/node.cc index f8d5b7269cb..c1385801457 100644 --- a/src/node.cc +++ b/src/node.cc @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include /* dlopen(), dlsym() */ #include @@ -102,6 +104,21 @@ ExecuteString(v8::Handle source, return scope.Close(result); } +static Handle +Cwd (const Arguments& args) +{ + HandleScope scope; + + char output[PATH_MAX]; + char *r = getcwd(output, PATH_MAX); + if (r == NULL) { + return ThrowException(Exception::Error(String::New(strerror(errno)))); + } + Local cwd = String::New(output); + + return scope.Close(cwd); +} + v8::Handle node_exit (const v8::Arguments& args) { @@ -249,6 +266,7 @@ Load (int argc, char *argv[]) NODE_SET_METHOD(node_obj, "compile", compile); NODE_SET_METHOD(node_obj, "reallyExit", node_exit); + NODE_SET_METHOD(node_obj, "cwd", Cwd); NODE_SET_METHOD(node_obj, "dlopen", node_dlopen); node_obj->Set(String::NewSymbol("EventEmitter"), diff --git a/website/api.txt b/website/api.txt index b86468c2ab7..b1a29fb2625 100644 --- a/website/api.txt +++ b/website/api.txt @@ -77,6 +77,8 @@ Like +puts()+ but without the trailing new-line. +node.exit(code)+:: Immediately ends the process with the specified code. ++node.cwd()+:: +Returns the current working directory of the process. === Global Variables