Skip to content
This repository
Browse code

Feature: add node.cwd() to access the current working directory.

  • Loading branch information...
commit 8ea6adcae63d7114e9765d471168fb19d2818863 1 parent fbf65b5
mcarter authored ry committed
18  src/node.cc
@@ -16,6 +16,8 @@
16 16
 #include <stdlib.h>
17 17
 #include <strings.h>
18 18
 #include <assert.h>
  19
+#include <unistd.h>
  20
+#include <errno.h>
19 21
 #include <dlfcn.h> /* dlopen(), dlsym() */
20 22
 
21 23
 #include <string>
@@ -102,6 +104,21 @@ ExecuteString(v8::Handle<v8::String> source,
102 104
   return scope.Close(result);
103 105
 }
104 106
 
  107
+static Handle<Value>
  108
+Cwd (const Arguments& args)
  109
+{
  110
+  HandleScope scope;
  111
+
  112
+  char output[PATH_MAX];
  113
+  char *r = getcwd(output, PATH_MAX);
  114
+  if (r == NULL) {
  115
+    return ThrowException(Exception::Error(String::New(strerror(errno))));
  116
+  }
  117
+  Local<String> cwd = String::New(output);
  118
+
  119
+  return scope.Close(cwd);
  120
+}
  121
+
105 122
 v8::Handle<v8::Value>
106 123
 node_exit (const v8::Arguments& args)
107 124
 {
@@ -249,6 +266,7 @@ Load (int argc, char *argv[])
249 266
 
250 267
   NODE_SET_METHOD(node_obj, "compile", compile);
251 268
   NODE_SET_METHOD(node_obj, "reallyExit", node_exit);
  269
+  NODE_SET_METHOD(node_obj, "cwd", Cwd);
252 270
   NODE_SET_METHOD(node_obj, "dlopen", node_dlopen);
253 271
 
254 272
   node_obj->Set(String::NewSymbol("EventEmitter"),
2  website/api.txt
@@ -77,6 +77,8 @@ Like +puts()+ but without the trailing new-line.
77 77
 +node.exit(code)+::
78 78
 Immediately ends the process with the specified code.
79 79
 
  80
++node.cwd()+::
  81
+Returns the current working directory of the process.
80 82
 
81 83
 
82 84
 === Global Variables

0 notes on commit 8ea6adc

Please sign in to comment.
Something went wrong with that request. Please try again.