diff options
author | Brian White <mscdex@mscdex.net> | 2010-12-23 16:06:03 +0300 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2010-12-23 20:52:04 +0300 |
commit | 8275d7cd346d32939e10300bef39ca326c288acc (patch) | |
tree | 0bf75985eca6941f10588dc093050eb42df309d0 /src | |
parent | b2fd88ef7a24ac4ccb6d329e39b2d34ec8260697 (diff) |
Fix Cygwin compatibility in the os module
Diffstat (limited to 'src')
-rw-r--r-- | src/node_os.cc | 22 | ||||
-rw-r--r-- | src/platform_cygwin.cc | 32 |
2 files changed, 24 insertions, 30 deletions
diff --git a/src/node_os.cc b/src/node_os.cc index ad7b71a80ef..5019df75ee9 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -7,8 +7,8 @@ #include <errno.h> #include <unistd.h> // gethostname, sysconf -#include <sys/param.h> // sysctl -#include <sys/sysctl.h> // sysctl +#include <sys/utsname.h> +#include <string.h> namespace node { @@ -28,12 +28,11 @@ static Handle<Value> GetHostname(const Arguments& args) { static Handle<Value> GetOSType(const Arguments& args) { HandleScope scope; char type[256]; - static int which[] = {CTL_KERN, KERN_OSTYPE}; - size_t size = sizeof(type); + struct utsname info; - if (sysctl(which, 2, &type, &size, NULL, 0) < 0) { - return Undefined(); - } + uname(&info); + strncpy(type, info.sysname, strlen(info.sysname)); + type[strlen(info.sysname)] = 0; return scope.Close(String::New(type)); } @@ -41,12 +40,11 @@ static Handle<Value> GetOSType(const Arguments& args) { static Handle<Value> GetOSRelease(const Arguments& args) { HandleScope scope; char release[256]; - static int which[] = {CTL_KERN, KERN_OSRELEASE}; - size_t size = sizeof(release); + struct utsname info; - if (sysctl(which, 2, &release, &size, NULL, 0) < 0) { - return Undefined(); - } + uname(&info); + strncpy(release, info.release, strlen(info.release)); + release[strlen(info.release)] = 0; return scope.Close(String::New(release)); } diff --git a/src/platform_cygwin.cc b/src/platform_cygwin.cc index de0a3ba5ede..6a601aeb1db 100644 --- a/src/platform_cygwin.cc +++ b/src/platform_cygwin.cc @@ -4,7 +4,6 @@ #include <v8.h> #include <sys/param.h> // for MAXPATHLEN -#include <sys/sysctl.h> #include <sys/sysinfo.h> #include <unistd.h> // getpagesize, sysconf #include <stdio.h> // sscanf, snprintf @@ -290,8 +289,8 @@ int Platform::GetCPUInfo(Local<Array> *cpus) { continue; else if (strncmp(line, "intr ", 5) == 0) break; - sscanf(line, "%*s %llu %llu %llu %llu %*llu %llu", - &ticks_user, &ticks_nice, &ticks_sys, &ticks_idle, &ticks_intr); + sscanf(line, "%*s %llu %llu %llu %llu", + &ticks_user, &ticks_nice, &ticks_sys, &ticks_idle); snprintf(speedPath, sizeof(speedPath), "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq", i); fpSpeed = fopen(speedPath, "r"); @@ -308,7 +307,7 @@ int Platform::GetCPUInfo(Local<Array> *cpus) { cputimes->Set(String::New("nice"), Number::New(ticks_nice * multiplier)); cputimes->Set(String::New("sys"), Number::New(ticks_sys * multiplier)); cputimes->Set(String::New("idle"), Number::New(ticks_idle * multiplier)); - cputimes->Set(String::New("irq"), Number::New(ticks_intr * multiplier)); + cputimes->Set(String::New("irq"), Number::New(0)); cpuinfo->Set(String::New("model"), String::New(model)); cpuinfo->Set(String::New("speed"), Number::New(cpuspeed)); @@ -337,26 +336,23 @@ double Platform::GetTotalMemory() { } double Platform::GetUptime() { - struct sysinfo info; + double amount; + char line[512]; + FILE *fpUptime = fopen("/proc/uptime", "r"); - if (sysinfo(&info) < 0) { - return -1; + if (fpUptime) { + if (fgets(line, 511, fpUptime) != NULL) { + sscanf(line, "%lf %*lf", &amount); + } + fclose(fpUptime); } - return static_cast<double>(info.uptime); + return amount; } int Platform::GetLoadAvg(Local<Array> *loads) { - struct sysinfo info; - - if (sysinfo(&info) < 0) { - return -1; - } - (*loads)->Set(0, Number::New(static_cast<double>(info.loads[0]) / 65536.0)); - (*loads)->Set(1, Number::New(static_cast<double>(info.loads[1]) / 65536.0)); - (*loads)->Set(2, Number::New(static_cast<double>(info.loads[2]) / 65536.0)); - - return 0; + // Unsupported as of cygwin 1.7.7 + return -1; } } // namespace node |