Xinqi Bao's Git

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