William Giokas avatar William Giokas committed 18a7cf4

Fixed segfault. Reading non-existent files

Signed-off-by: William Giokas <1007380@gmail.com>;

Comments (0)

Files changed (1)

 	int descap, remcap;
     float remaining;
     float using;
-    float energy;
+    float voltage;
+    float current;
 
 	descap = -1;
 	remcap = -1;
-    energy = -1;
     using  = -1;
     remaining = -1;
     stat = "Not Present";
 	free(co);
 
     co = readfile(base, "power_now"); /* µWattage being used */
-    sscanf(co, "%f", &using);
+    if (co == NULL) {
+        co = readfile(base, "voltage_now");
+        sscanf(co, "%f", &voltage);
+        free(co);
+        co = readfile(base, "current_now");
+        sscanf(co, "%f", &current);
+        /* Remaining */
+        remcap  = (voltage / 1000.0) * ((float)remcap / 1000.0);
+        /* full design */
+        descap  = (voltage / 1000.0) * ((float)descap / 1000.0);
+        /* present rate */
+        using = (voltage / 1000.0) * ((float)current / 1000.0);
+        if (co == NULL)
+            return smprintf("");
+    }
+    else
+        sscanf(co, "%f", &using);
     free(co);
 
-    co = readfile(base, "energy_now"); /* µWatts stored */
-    sscanf(co, "%f", &energy);
-    free(co);
 
 	if (remcap < 0 || descap < 0)
 		return smprintf("invalid");
     /* Getting time remaining */
     /* First check the battery status */
     if (status == *("Discharging")) {
-        remaining = energy / using;
+        remaining = (float)remcap / using;
         stat = "Batt";
     } else if (status == *("Charging")) {
-        remaining = ((float)descap - energy) /using;
+        remaining = ((float)descap - (float)remcap) /using;
         stat = "Char";
     } else {
         remaining = 0;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.