Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 126 lines (91 sloc) 2.659 kB
f1762ff @mscdex Add os module and getHostname
mscdex authored
1 #include <node_os.h>
2
3 #include <node.h>
4 #include <v8.h>
5
d75c338 @mscdex Add more functionality to the os module
mscdex authored
6 #include "platform.h"
7
f1762ff @mscdex Add os module and getHostname
mscdex authored
8 #include <errno.h>
d75c338 @mscdex Add more functionality to the os module
mscdex authored
9 #include <unistd.h> // gethostname, sysconf
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored
10 #include <sys/utsname.h>
11 #include <string.h>
f1762ff @mscdex Add os module and getHostname
mscdex authored
12
13 namespace node {
14
15 using namespace v8;
16
17 static Handle<Value> GetHostname(const Arguments& args) {
18 HandleScope scope;
19 char s[255];
20
d75c338 @mscdex Add more functionality to the os module
mscdex authored
21 if (gethostname(s, 255) < 0) {
22 return Undefined();
f1762ff @mscdex Add os module and getHostname
mscdex authored
23 }
24
25 return scope.Close(String::New(s));
26 }
27
d75c338 @mscdex Add more functionality to the os module
mscdex authored
28 static Handle<Value> GetOSType(const Arguments& args) {
29 HandleScope scope;
30 char type[256];
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored
31 struct utsname info;
d75c338 @mscdex Add more functionality to the os module
mscdex authored
32
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored
33 uname(&info);
34 strncpy(type, info.sysname, strlen(info.sysname));
35 type[strlen(info.sysname)] = 0;
d75c338 @mscdex Add more functionality to the os module
mscdex authored
36
37 return scope.Close(String::New(type));
38 }
39
40 static Handle<Value> GetOSRelease(const Arguments& args) {
41 HandleScope scope;
42 char release[256];
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored
43 struct utsname info;
d75c338 @mscdex Add more functionality to the os module
mscdex authored
44
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored
45 uname(&info);
46 strncpy(release, info.release, strlen(info.release));
47 release[strlen(info.release)] = 0;
d75c338 @mscdex Add more functionality to the os module
mscdex authored
48
49 return scope.Close(String::New(release));
50 }
51
52 static Handle<Value> GetCPUInfo(const Arguments& args) {
53 HandleScope scope;
54 Local<Array> cpus;
55 int r = Platform::GetCPUInfo(&cpus);
56
57 if (r < 0) {
58 return Undefined();
59 }
60
61 return scope.Close(cpus);
62 }
63
64 static Handle<Value> GetFreeMemory(const Arguments& args) {
65 HandleScope scope;
66 double amount = Platform::GetFreeMemory();
67
68 if (amount < 0) {
69 return Undefined();
70 }
71
72 return scope.Close(Number::New(amount));
73 }
74
75 static Handle<Value> GetTotalMemory(const Arguments& args) {
76 HandleScope scope;
77 double amount = Platform::GetTotalMemory();
78
79 if (amount < 0) {
80 return Undefined();
81 }
82
83 return scope.Close(Number::New(amount));
84 }
85
86 static Handle<Value> GetUptime(const Arguments& args) {
87 HandleScope scope;
88 double uptime = Platform::GetUptime();
89
90 if (uptime < 0) {
91 return Undefined();
92 }
93
94 return scope.Close(Number::New(uptime));
95 }
96
97 static Handle<Value> GetLoadAvg(const Arguments& args) {
98 HandleScope scope;
99 Local<Array> loads = Array::New(3);
100 int r = Platform::GetLoadAvg(&loads);
101
102 if (r < 0) {
103 return Undefined();
104 }
105
106 return scope.Close(loads);
107 }
108
f1762ff @mscdex Add os module and getHostname
mscdex authored
109 void OS::Initialize(v8::Handle<v8::Object> target) {
110 HandleScope scope;
111
112 NODE_SET_METHOD(target, "getHostname", GetHostname);
d75c338 @mscdex Add more functionality to the os module
mscdex authored
113 NODE_SET_METHOD(target, "getLoadAvg", GetLoadAvg);
114 NODE_SET_METHOD(target, "getUptime", GetUptime);
115 NODE_SET_METHOD(target, "getTotalMem", GetTotalMemory);
116 NODE_SET_METHOD(target, "getFreeMem", GetFreeMemory);
117 NODE_SET_METHOD(target, "getCPUs", GetCPUInfo);
118 NODE_SET_METHOD(target, "getOSType", GetOSType);
119 NODE_SET_METHOD(target, "getOSRelease", GetOSRelease);
f1762ff @mscdex Add os module and getHostname
mscdex authored
120 }
121
122
123 } // namespace node
124
125 NODE_MODULE(node_os, node::OS::Initialize);
Something went wrong with that request. Please try again.