Browse Source

Fix Cygwin compatibility in the os module

v0.7.4-release
Brian White 14 years ago
committed by Ryan Dahl
parent
commit
8275d7cd34
  1. 22
      src/node_os.cc
  2. 30
      src/platform_cygwin.cc

22
src/node_os.cc

@ -7,8 +7,8 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> // gethostname, sysconf #include <unistd.h> // gethostname, sysconf
#include <sys/param.h> // sysctl #include <sys/utsname.h>
#include <sys/sysctl.h> // sysctl #include <string.h>
namespace node { namespace node {
@ -28,12 +28,11 @@ static Handle<Value> GetHostname(const Arguments& args) {
static Handle<Value> GetOSType(const Arguments& args) { static Handle<Value> GetOSType(const Arguments& args) {
HandleScope scope; HandleScope scope;
char type[256]; char type[256];
static int which[] = {CTL_KERN, KERN_OSTYPE}; struct utsname info;
size_t size = sizeof(type);
if (sysctl(which, 2, &type, &size, NULL, 0) < 0) { uname(&info);
return Undefined(); strncpy(type, info.sysname, strlen(info.sysname));
} type[strlen(info.sysname)] = 0;
return scope.Close(String::New(type)); return scope.Close(String::New(type));
} }
@ -41,12 +40,11 @@ static Handle<Value> GetOSType(const Arguments& args) {
static Handle<Value> GetOSRelease(const Arguments& args) { static Handle<Value> GetOSRelease(const Arguments& args) {
HandleScope scope; HandleScope scope;
char release[256]; char release[256];
static int which[] = {CTL_KERN, KERN_OSRELEASE}; struct utsname info;
size_t size = sizeof(release);
if (sysctl(which, 2, &release, &size, NULL, 0) < 0) { uname(&info);
return Undefined(); strncpy(release, info.release, strlen(info.release));
} release[strlen(info.release)] = 0;
return scope.Close(String::New(release)); return scope.Close(String::New(release));
} }

30
src/platform_cygwin.cc

@ -4,7 +4,6 @@
#include <v8.h> #include <v8.h>
#include <sys/param.h> // for MAXPATHLEN #include <sys/param.h> // for MAXPATHLEN
#include <sys/sysctl.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <unistd.h> // getpagesize, sysconf #include <unistd.h> // getpagesize, sysconf
#include <stdio.h> // sscanf, snprintf #include <stdio.h> // sscanf, snprintf
@ -290,8 +289,8 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
continue; continue;
else if (strncmp(line, "intr ", 5) == 0) else if (strncmp(line, "intr ", 5) == 0)
break; break;
sscanf(line, "%*s %llu %llu %llu %llu %*llu %llu", sscanf(line, "%*s %llu %llu %llu %llu",
&ticks_user, &ticks_nice, &ticks_sys, &ticks_idle, &ticks_intr); &ticks_user, &ticks_nice, &ticks_sys, &ticks_idle);
snprintf(speedPath, sizeof(speedPath), snprintf(speedPath, sizeof(speedPath),
"/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq", i); "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq", i);
fpSpeed = fopen(speedPath, "r"); 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("nice"), Number::New(ticks_nice * multiplier));
cputimes->Set(String::New("sys"), Number::New(ticks_sys * 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("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("model"), String::New(model));
cpuinfo->Set(String::New("speed"), Number::New(cpuspeed)); cpuinfo->Set(String::New("speed"), Number::New(cpuspeed));
@ -337,26 +336,23 @@ double Platform::GetTotalMemory() {
} }
double Platform::GetUptime() { double Platform::GetUptime() {
struct sysinfo info; double amount;
char line[512];
FILE *fpUptime = fopen("/proc/uptime", "r");
if (sysinfo(&info) < 0) { if (fpUptime) {
return -1; 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) { int Platform::GetLoadAvg(Local<Array> *loads) {
struct sysinfo info; // Unsupported as of cygwin 1.7.7
if (sysinfo(&info) < 0) {
return -1; 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;
}
} // namespace node } // namespace node

Loading…
Cancel
Save