Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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