Feature: add node.cwd() to access the current working directory.
authorMichael Carter <cartermichael@gmail.com>
Tue, 1 Sep 2009 09:39:30 +0000 (11:39 +0200)
committerRyan <ry@tinyclouds.org>
Tue, 1 Sep 2009 09:39:30 +0000 (11:39 +0200)
src/node.cc
website/api.txt

index f8d5b72..c138580 100644 (file)
@@ -16,6 +16,8 @@
 #include <stdlib.h>
 #include <strings.h>
 #include <assert.h>
+#include <unistd.h>
+#include <errno.h>
 #include <dlfcn.h> /* dlopen(), dlsym() */
 
 #include <string>
@@ -102,6 +104,21 @@ ExecuteString(v8::Handle<v8::String> source,
   return scope.Close(result);
 }
 
+static Handle<Value>
+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<String> cwd = String::New(output);
+
+  return scope.Close(cwd);
+}
+
 v8::Handle<v8::Value>
 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"),
index b86468c..b1a29fb 100644 (file)
@@ -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