1
0

swap: Print UNKNOWN_STR if no swap is allocated

This commit is contained in:
Jody Leonard 2016-10-15 19:50:02 -04:00
parent 4d6761e0c7
commit 9bef4e889a

View File

@ -424,7 +424,7 @@ run_command(const char *cmd)
static char * static char *
swap_free(void) swap_free(void)
{ {
long free; long total, free;
FILE *fp; FILE *fp;
char buf[2048]; char buf[2048];
size_t bytes_read; size_t bytes_read;
@ -443,6 +443,12 @@ swap_free(void)
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
match = strstr(buf, "SwapTotal");
sscanf(match, "SwapTotal: %ld kB\n", &total);
if (total == 0) {
return smprintf(UNKNOWN_STR);
}
match = strstr(buf, "SwapFree"); match = strstr(buf, "SwapFree");
sscanf(match, "SwapFree: %ld kB\n", &free); sscanf(match, "SwapFree: %ld kB\n", &free);
@ -471,11 +477,14 @@ swap_perc(void)
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
match = strstr(buf, "SwapCached");
sscanf(match, "SwapCached: %ld kB\n", &cached);
match = strstr(buf, "SwapTotal"); match = strstr(buf, "SwapTotal");
sscanf(match, "SwapTotal: %ld kB\n", &total); sscanf(match, "SwapTotal: %ld kB\n", &total);
if (total == 0) {
return smprintf(UNKNOWN_STR);
}
match = strstr(buf, "SwapCached");
sscanf(match, "SwapCached: %ld kB\n", &cached);
match = strstr(buf, "SwapFree"); match = strstr(buf, "SwapFree");
sscanf(match, "SwapFree: %ld kB\n", &free); sscanf(match, "SwapFree: %ld kB\n", &free);
@ -507,6 +516,9 @@ swap_total(void)
match = strstr(buf, "SwapTotal"); match = strstr(buf, "SwapTotal");
sscanf(match, "SwapTotal: %ld kB\n", &total); sscanf(match, "SwapTotal: %ld kB\n", &total);
if (total == 0) {
return smprintf(UNKNOWN_STR);
}
return smprintf("%f", (float)total / 1024 / 1024); return smprintf("%f", (float)total / 1024 / 1024);
} }
@ -533,11 +545,14 @@ swap_used(void)
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
} }
match = strstr(buf, "SwapCached");
sscanf(match, "SwapCached: %ld kB\n", &cached);
match = strstr(buf, "SwapTotal"); match = strstr(buf, "SwapTotal");
sscanf(match, "SwapTotal: %ld kB\n", &total); sscanf(match, "SwapTotal: %ld kB\n", &total);
if (total == 0) {
return smprintf(UNKNOWN_STR);
}
match = strstr(buf, "SwapCached");
sscanf(match, "SwapCached: %ld kB\n", &cached);
match = strstr(buf, "SwapFree"); match = strstr(buf, "SwapFree");
sscanf(match, "SwapFree: %ld kB\n", &free); sscanf(match, "SwapFree: %ld kB\n", &free);