+static char *
+swap_free(void)
+{
+ long total, free;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_free: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapFree: %ld kB\n", &free);
+
+ return smprintf("%f", (float)free / 1024 / 1024);
+}
+
+static char *
+swap_perc(void)
+{
+ long total, free, cached;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_perc: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ if ((match = strstr(buf, "SwapCached")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapCached: %ld kB\n", &cached);
+
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapFree: %ld kB\n", &free);
+
+ return smprintf("%d%%", 100 * (total - free - cached) / total);
+}
+
+static char *
+swap_total(void)
+{
+ long total;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_total: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ return smprintf("%f", (float)total / 1024 / 1024);
+}
+
+static char *
+swap_used(void)
+{
+ long total, free, cached;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_used: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ if ((match = strstr(buf, "SwapCached")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapCached: %ld kB\n", &cached);
+
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapFree: %ld kB\n", &free);
+
+ return smprintf("%f", (float)(total - free - cached) / 1024 / 1024);
+}