Xinqi Bao's Git

Use indentation to increase readability
[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 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 fprintf(stderr, "fopen '/proc/meminfo': %s\n",
20 strerror(errno));
21 return NULL;
22 }
23
24 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1,
25 fp)) == 0) {
26 fprintf(stderr, "fread '/proc/meminfo': %s\n",
27 strerror(errno));
28 fclose(fp);
29 return NULL;
30 }
31 fclose(fp);
32
33 if ((match = strstr(buf, "SwapTotal")) == NULL)
34 return NULL;
35 sscanf(match, "SwapTotal: %ld kB\n", &total);
36
37 if ((match = strstr(buf, "SwapFree")) == NULL)
38 return NULL;
39 sscanf(match, "SwapFree: %ld kB\n", &free);
40
41 return bprintf("%f", (float)free / 1024 / 1024);
42 }
43
44 const char *
45 swap_perc(void)
46 {
47 long total, free, cached;
48 FILE *fp;
49 size_t bytes_read;
50 char *match;
51
52 fp = fopen("/proc/meminfo", "r");
53 if (fp == NULL) {
54 fprintf(stderr, "fopen '/proc/meminfo': %s\n",
55 strerror(errno));
56 return NULL;
57 }
58
59 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1,
60 fp)) == 0) {
61 fprintf(stderr, "fread '/proc/meminfo': %s\n",
62 strerror(errno));
63 fclose(fp);
64 return NULL;
65 }
66 fclose(fp);
67
68 if ((match = strstr(buf, "SwapTotal")) == NULL)
69 return NULL;
70 sscanf(match, "SwapTotal: %ld kB\n", &total);
71
72 if ((match = strstr(buf, "SwapCached")) == NULL)
73 return NULL;
74 sscanf(match, "SwapCached: %ld kB\n", &cached);
75
76 if ((match = strstr(buf, "SwapFree")) == NULL)
77 return NULL;
78 sscanf(match, "SwapFree: %ld kB\n", &free);
79
80 return bprintf("%d", 100 * (total - free - cached) / total);
81 }
82
83 const char *
84 swap_total(void)
85 {
86 long total;
87 FILE *fp;
88 size_t bytes_read;
89 char *match;
90
91 fp = fopen("/proc/meminfo", "r");
92 if (fp == NULL) {
93 fprintf(stderr, "fopen '/proc/meminfo': %s\n",
94 strerror(errno));
95 return NULL;
96 }
97 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1,
98 fp)) == 0) {
99 fprintf(stderr, "fread '/proc/meminfo': %s\n",
100 strerror(errno));
101 fclose(fp);
102 return NULL;
103 }
104 fclose(fp);
105
106 if ((match = strstr(buf, "SwapTotal")) == NULL)
107 return NULL;
108 sscanf(match, "SwapTotal: %ld kB\n", &total);
109
110 return bprintf("%f", (float)total / 1024 / 1024);
111 }
112
113 const char *
114 swap_used(void)
115 {
116 long total, free, cached;
117 FILE *fp;
118 size_t bytes_read;
119 char *match;
120
121 fp = fopen("/proc/meminfo", "r");
122 if (fp == NULL) {
123 fprintf(stderr, "fopen '/proc/meminfo': %s\n",
124 strerror(errno));
125 return NULL;
126 }
127 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf) - 1,
128 fp)) == 0) {
129 fprintf(stderr, "fread '/proc/meminfo': %s\n",
130 strerror(errno));
131 fclose(fp);
132 return NULL;
133 }
134 fclose(fp);
135
136 if ((match = strstr(buf, "SwapTotal")) == NULL)
137 return NULL;
138 sscanf(match, "SwapTotal: %ld kB\n", &total);
139
140 if ((match = strstr(buf, "SwapCached")) == NULL)
141 return NULL;
142 sscanf(match, "SwapCached: %ld kB\n", &cached);
143
144 if ((match = strstr(buf, "SwapFree")) == NULL)
145 return NULL;
146 sscanf(match, "SwapFree: %ld kB\n", &free);
147
148 return bprintf("%f", (float)(total - free - cached) / 1024 / 1024);
149 }
150 #elif defined(__OpenBSD__)
151 /* unimplemented */
152 #endif