diff --git a/cmake/node_build.cmake b/cmake/node_build.cmake index 72e218e0d0..ec9010dd3f 100644 --- a/cmake/node_build.cmake +++ b/cmake/node_build.cmake @@ -32,6 +32,7 @@ set(node_sources src/node_stdio.cc src/node_timer.cc src/node_script.cc + src/node_os.cc src/node_natives.h ${node_extra_src}) diff --git a/doc/api/_toc.markdown b/doc/api/_toc.markdown index 3546a19e4b..d2c150a6f9 100644 --- a/doc/api/_toc.markdown +++ b/doc/api/_toc.markdown @@ -28,6 +28,7 @@ * [Child Processes](child_processes.html) * [Assertion Testing](assert.html) * [TTY](tty.html) +* [OS](os.html) * Appendixes * [Appendix 1: Recommended Third-party Modules](appendix_1.html) * [Appendix 2: Deprecated API's](appendix_2.html) diff --git a/doc/api/os.markdown b/doc/api/os.markdown new file mode 100644 index 0000000000..06df21bec7 --- /dev/null +++ b/doc/api/os.markdown @@ -0,0 +1,7 @@ +## os Module + +Use `require('os')` to access this module. + +### tls.getHostname + +Returns the hostname of the operating system. diff --git a/lib/os.js b/lib/os.js new file mode 100644 index 0000000000..14a582c2e2 --- /dev/null +++ b/lib/os.js @@ -0,0 +1,3 @@ +var binding = process.binding('os'); + +exports.getHostname = binding.getHostname; diff --git a/src/node_extensions.h b/src/node_extensions.h index 5a6bc8654e..131d34e6f1 100644 --- a/src/node_extensions.h +++ b/src/node_extensions.h @@ -12,5 +12,6 @@ NODE_EXT_LIST_ITEM(node_net) NODE_EXT_LIST_ITEM(node_http_parser) NODE_EXT_LIST_ITEM(node_signal_watcher) NODE_EXT_LIST_ITEM(node_stdio) +NODE_EXT_LIST_ITEM(node_os) NODE_EXT_LIST_END diff --git a/src/node_os.cc b/src/node_os.cc new file mode 100644 index 0000000000..68b08959ac --- /dev/null +++ b/src/node_os.cc @@ -0,0 +1,34 @@ +#include + +#include +#include + +#include +#include // gethostname + +namespace node { + +using namespace v8; + +static Handle GetHostname(const Arguments& args) { + HandleScope scope; + char s[255]; + int r = gethostname(s, 255); + + if (r < 0) { + return ThrowException(ErrnoException(errno, "gethostname")); + } + + return scope.Close(String::New(s)); +} + +void OS::Initialize(v8::Handle target) { + HandleScope scope; + + NODE_SET_METHOD(target, "getHostname", GetHostname); +} + + +} // namespace node + +NODE_MODULE(node_os, node::OS::Initialize); diff --git a/src/node_os.h b/src/node_os.h new file mode 100644 index 0000000000..dcc705c71d --- /dev/null +++ b/src/node_os.h @@ -0,0 +1,17 @@ +#ifndef node_os_h +#define node_os_h + +#include +#include + +namespace node { + +class OS { +public: + static void Initialize (v8::Handle target); +}; + + +} // namespace node + +#endif // node_os_h diff --git a/test/simple/test-os-hostname.js b/test/simple/test-os-hostname.js new file mode 100644 index 0000000000..b323622717 --- /dev/null +++ b/test/simple/test-os-hostname.js @@ -0,0 +1,5 @@ +var common = require('../common'); +var assert = require('assert'); +var os = require('os'); + +assert.ok(os.getHostname().length > 0); diff --git a/wscript b/wscript index 1790a9bb72..3cc70e2610 100644 --- a/wscript +++ b/wscript @@ -598,6 +598,7 @@ def build(bld): src/node_stdio.cc src/node_timer.cc src/node_script.cc + src/node_os.cc """ node.source += bld.env["PLATFORM_FILE"] if not product_type_is_lib: