Xinqi Bao's Git

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