Fix Cygwin compatibility in the os module
authorBrian White <mscdex@mscdex.net>
Thu, 23 Dec 2010 13:06:03 +0000 (08:06 -0500)
committerRyan Dahl <ry@tinyclouds.org>
Thu, 23 Dec 2010 17:52:04 +0000 (09:52 -0800)
src/node_os.cc
src/platform_cygwin.cc

index ad7b71a..5019df7 100644 (file)
@@ -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));
 }
index de0a3ba..6a601ae 100644 (file)
@@ -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