Skip to content

Commit f1762ff

Browse files
mscdexry
authored andcommitted
Add os module and getHostname
1 parent dc65cbd commit f1762ff

File tree

9 files changed

+70
-0
lines changed

9 files changed

+70
-0
lines changed

cmake/node_build.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(node_sources
3232
src/node_stdio.cc
3333
src/node_timer.cc
3434
src/node_script.cc
35+
src/node_os.cc
3536
src/node_natives.h
3637
${node_extra_src})
3738

doc/api/_toc.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* [Child Processes](child_processes.html)
2929
* [Assertion Testing](assert.html)
3030
* [TTY](tty.html)
31+
* [OS](os.html)
3132
* Appendixes
3233
* [Appendix 1: Recommended Third-party Modules](appendix_1.html)
3334
* [Appendix 2: Deprecated API's](appendix_2.html)

doc/api/os.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## os Module
2+
3+
Use `require('os')` to access this module.
4+
5+
### tls.getHostname
6+
7+
Returns the hostname of the operating system.

lib/os.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var binding = process.binding('os');
2+
3+
exports.getHostname = binding.getHostname;

src/node_extensions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ NODE_EXT_LIST_ITEM(node_net)
1212
NODE_EXT_LIST_ITEM(node_http_parser)
1313
NODE_EXT_LIST_ITEM(node_signal_watcher)
1414
NODE_EXT_LIST_ITEM(node_stdio)
15+
NODE_EXT_LIST_ITEM(node_os)
1516
NODE_EXT_LIST_END
1617

src/node_os.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <node_os.h>
2+
3+
#include <node.h>
4+
#include <v8.h>
5+
6+
#include <errno.h>
7+
#include <unistd.h> // gethostname
8+
9+
namespace node {
10+
11+
using namespace v8;
12+
13+
static Handle<Value> GetHostname(const Arguments& args) {
14+
HandleScope scope;
15+
char s[255];
16+
int r = gethostname(s, 255);
17+
18+
if (r < 0) {
19+
return ThrowException(ErrnoException(errno, "gethostname"));
20+
}
21+
22+
return scope.Close(String::New(s));
23+
}
24+
25+
void OS::Initialize(v8::Handle<v8::Object> target) {
26+
HandleScope scope;
27+
28+
NODE_SET_METHOD(target, "getHostname", GetHostname);
29+
}
30+
31+
32+
} // namespace node
33+
34+
NODE_MODULE(node_os, node::OS::Initialize);

src/node_os.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef node_os_h
2+
#define node_os_h
3+
4+
#include <node.h>
5+
#include <v8.h>
6+
7+
namespace node {
8+
9+
class OS {
10+
public:
11+
static void Initialize (v8::Handle<v8::Object> target);
12+
};
13+
14+
15+
} // namespace node
16+
17+
#endif // node_os_h

test/simple/test-os-hostname.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
var os = require('os');
4+
5+
assert.ok(os.getHostname().length > 0);

wscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ def build(bld):
598598
src/node_stdio.cc
599599
src/node_timer.cc
600600
src/node_script.cc
601+
src/node_os.cc
601602
"""
602603
node.source += bld.env["PLATFORM_FILE"]
603604
if not product_type_is_lib:

0 commit comments

Comments
 (0)