Xinqi Bao's Git

components/swap.c | move duplicated code to separate function
[slstatus.git] / components / swap.c
1 /* See LICENSE file for copyright and license details. */
2 #if defined(__linux__)
3 #include <errno.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #include "../util.h"
8
9 static size_t
10 pread(const char *path, char *buf, size_t bufsiz)
11 {
12 FILE *fp;
13 size_t bytes_read;
14
15 if (!(fp = fopen(path, "r"))) {
16 fprintf(stderr, "fopen '%s': %s\n", path,
17 strerror(errno));
18 return 0;
19 }
20 if ((bytes_read = fread(buf, sizeof(char), bufsiz, fp)) == 0) {
21 fprintf(stderr, "fread '%s': %s\n", path,
22 strerror(errno));
23 fclose(fp);
24 return 0;
25 }
26 fclose(fp);
27
28 buf[bytes_read] = '\0';
29
30 return bytes_read;
31 }
32
33 const char *
34 swap_free(void)
35 {
36 long total, free;
37 char *match;
38
39 if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
40 return NULL;
41 }
42
43 if ((match = strstr(buf, "SwapTotal")) == NULL)
44 return NULL;
45 sscanf(match, "SwapTotal: %ld kB\n", &total);
46
47 if ((match = strstr(buf, "SwapFree")) == NULL)
48 return NULL;
49 sscanf(match, "SwapFree: %ld kB\n", &free);
50
51 return bprintf("%f", (float)free / 1024 / 1024);
52 }
53
54 const char *
55 swap_perc(void)
56 {
57 long total, free, cached;
58 char *match;
59
60 if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
61 return NULL;
62 }
63
64 if ((match = strstr(buf, "SwapTotal")) == NULL)
65 return NULL;
66 sscanf(match, "SwapTotal: %ld kB\n", &total);
67
68 if ((match = strstr(buf, "SwapCached")) == NULL)
69 return NULL;
70 sscanf(match, "SwapCached: %ld kB\n", &cached);
71
72 if ((match = strstr(buf, "SwapFree")) == NULL)
73 return NULL;
74 sscanf(match, "SwapFree: %ld kB\n", &free);
75
76 return bprintf("%d", 100 * (total - free - cached) / total);
77 }
78
79 const char *
80 swap_total(void)
81 {
82 long total;
83 char *match;
84
85 if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
86 return NULL;
87 }
88
89 if ((match = strstr(buf, "SwapTotal")) == NULL)
90 return NULL;
91 sscanf(match, "SwapTotal: %ld kB\n", &total);
92
93 return bprintf("%f", (float)total / 1024 / 1024);
94 }
95
96 const char *
97 swap_used(void)
98 {
99 long total, free, cached;
100 char *match;
101
102 if (!pread("/proc/meminfo", buf, sizeof(buf) - 1)) {
103 return NULL;
104 }
105
106 if ((match = strstr(buf, "SwapTotal")) == NULL)
107 return NULL;
108 sscanf(match, "SwapTotal: %ld kB\n", &total);
109
110 if ((match = strstr(buf, "SwapCached")) == NULL)
111 return NULL;
112 sscanf(match, "SwapCached: %ld kB\n", &cached);
113
114 if ((match = strstr(buf, "SwapFree")) == NULL)
115 return NULL;
116 sscanf(match, "SwapFree: %ld kB\n", &free);
117
118 return bprintf("%f", (float)(total - free - cached) / 1024 / 1024);
119 }
120 #elif defined(__OpenBSD__)
121 /* unimplemented */
122 #endif