Fix os.cpus() on cygwin
authorBrian White <mscdex@mscdex.net>
Sat, 19 Feb 2011 00:02:16 +0000 (19:02 -0500)
committerRyan Dahl <ry@tinyclouds.org>
Sat, 19 Feb 2011 23:14:07 +0000 (15:14 -0800)
src/platform_cygwin.cc
src/platform_linux.cc

index 416cb17..9338d4f 100644 (file)
@@ -99,8 +99,7 @@ static inline char* _getProcessTitle() {
     if (GetLastError()) {
       _winapi_perror("GetConsoleTitleW");
       return NULL;
-    }
-    else {
+    } else {
       // The title is empty, so return empty string
       process_title = strdup("\0");
       return process_title;
@@ -252,6 +251,7 @@ int Platform::GetExecutablePath(char* buffer, size_t* size) {
 }
 
 int Platform::GetCPUInfo(Local<Array> *cpus) {
+  HandleScope scope;
   Local<Object> cpuinfo;
   Local<Object> cputimes;
   unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
@@ -285,14 +285,17 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
 
   if (fpStat) {
     while (fgets(line, 511, fpStat) != NULL) {
-      if (strncmp(line, "cpu ", 4) == 0)
+      if (strncmp(line, "cpu ", 4) == 0) {
         continue;
-      else if (strncmp(line, "intr ", 5) == 0)
+      } else if (strncmp(line, "cpu", 3) != 0) {
         break;
+      }
+
       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");
       if (fpSpeed) {
         if (fgets(line, 511, fpSpeed) != NULL) {
@@ -355,4 +358,5 @@ int Platform::GetLoadAvg(Local<Array> *loads) {
   return -1;
 }
 
+
 }  // namespace node
index 00732a0..4739977 100644 (file)
@@ -183,15 +183,19 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
 
   if (fpStat) {
     while (fgets(line, 511, fpStat) != NULL) {
-      if (strncmp(line, "cpu ", 4) == 0)
+      if (strncmp(line, "cpu ", 4) == 0) {
         continue;
-      else if (strncmp(line, "intr ", 5) == 0)
+      } else if (strncmp(line, "cpu", 3) != 0) {
         break;
+      }
+
       sscanf(line, "%*s %llu %llu %llu %llu %*llu %llu",
              &ticks_user, &ticks_nice, &ticks_sys, &ticks_idle, &ticks_intr);
       snprintf(speedPath, sizeof(speedPath),
                "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq", i);
+
       fpSpeed = fopen(speedPath, "r");
+
       if (fpSpeed) {
         if (fgets(line, 511, fpSpeed) != NULL) {
           sscanf(line, "%u", &cpuspeed);
@@ -199,6 +203,7 @@ int Platform::GetCPUInfo(Local<Array> *cpus) {
         }
         fclose(fpSpeed);
       }
+
       cpuinfo = Object::New();
       cputimes = Object::New();
       cputimes->Set(String::New("user"), Number::New(ticks_user * multiplier));