swap: Print UNKNOWN_STR if no swap is allocated
Jody Leonard me@jodyleonard.com
Sat, 15 Oct 2016 19:50:02 -0400
1 files changed,
22 insertions(+),
7 deletions(-)
jump to
M
slstatus.c
→
slstatus.c
@@ -424,7 +424,7 @@
static char * swap_free(void) { - long free; + long total, free; FILE *fp; char buf[2048]; size_t bytes_read;@@ -440,6 +440,12 @@ buf[bytes_read] = '\0';
fclose(fp); if (bytes_read == 0 || bytes_read == sizeof(buf)) { warn("Failed to read /proc/meminfo\n"); + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapTotal"); + sscanf(match, "SwapTotal: %ld kB\n", &total); + if (total == 0) { return smprintf(UNKNOWN_STR); }@@ -471,12 +477,15 @@ warn("Failed to read /proc/meminfo\n");
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, "SwapCached"); sscanf(match, "SwapCached: %ld kB\n", &cached); - match = strstr(buf, "SwapTotal"); - sscanf(match, "SwapTotal: %ld kB\n", &total); - match = strstr(buf, "SwapFree"); sscanf(match, "SwapFree: %ld kB\n", &free);@@ -507,6 +516,9 @@ }
match = strstr(buf, "SwapTotal"); sscanf(match, "SwapTotal: %ld kB\n", &total); + if (total == 0) { + return smprintf(UNKNOWN_STR); + } return smprintf("%f", (float)total / 1024 / 1024); }@@ -533,11 +545,14 @@ warn("Failed to read /proc/meminfo\n");
return smprintf(UNKNOWN_STR); } - match = strstr(buf, "SwapCached"); - sscanf(match, "SwapCached: %ld kB\n", &cached); - match = strstr(buf, "SwapTotal"); 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"); sscanf(match, "SwapFree: %ld kB\n", &free);