]> git.sur5r.net Git - i3/i3status/commitdiff
Avoid division by zero when calculating CPU usage.
authorMateusz Poszwa <old4@o2.pl>
Thu, 28 Jun 2012 09:22:00 +0000 (11:22 +0200)
committerMichael Stapelberg <michael@stapelberg.de>
Thu, 28 Jun 2012 10:05:01 +0000 (12:05 +0200)
src/print_cpu_usage.c

index 782084014433e831ff6ff46984f7247012b67945..ed17db343d2d5ff857d966a849d048d2b085bb2a 100644 (file)
@@ -40,7 +40,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
         curr_total = curr_user + curr_nice + curr_system + curr_idle;
         diff_idle  = curr_idle - prev_idle;
         diff_total = curr_total - prev_total;
-        diff_usage = (1000 * (diff_total - diff_idle)/diff_total + 5)/10;
+        diff_usage = (diff_total ? (1000 * (diff_total - diff_idle)/diff_total + 5)/10 : 0);
         prev_total = curr_total;
         prev_idle  = curr_idle;
 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
@@ -71,7 +71,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
         curr_total = curr_user + curr_nice + curr_system + curr_idle;
         diff_idle  = curr_idle - prev_idle;
         diff_total = curr_total - prev_total;
-        diff_usage = (1000 * (diff_total - diff_idle)/diff_total + 5)/10;
+        diff_usage = (diff_total ? (1000 * (diff_total - diff_idle)/diff_total + 5)/10 : 0);
         prev_total = curr_total;
         prev_idle  = curr_idle;
 #else