Xinqi Bao's Git

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