Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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