author | Stiletto <blasux@blasux.ru> |
Sat, 17 Aug 2019 15:23:15 +0400 | |
changeset 16 | e08bdbec1843 |
parent 15 | 163e96e2b941 |
permissions | -rw-r--r-- |
1 | 1 |
#define _DEFAULT_SOURCE |
2 |
#include <stdio.h> |
|
3 |
#include <stdlib.h> |
|
4 |
#include <unistd.h> |
|
5 |
#include <string.h> |
|
6 |
#include <errno.h> |
|
7 |
||
8 |
#include "config.h" |
|
9 |
||
10 |
int |
|
11 |
main(void) |
|
12 |
{ |
|
13 |
while (1) { |
|
14 |
char line[513]; |
|
15 |
FILE *fd; |
|
16 |
||
17 |
fd = fopen("/proc/meminfo", "r"); |
|
18 |
if (fd == NULL) { |
|
19 |
perror("fopen"); |
|
20 |
exit(1); |
|
21 |
} |
|
22 |
||
23 |
int free = 0; |
|
24 |
int total = 0; |
|
15
163e96e2b941
[status-mem] MemAvailable support
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
25 |
int avail = -1; |
1 | 26 |
int sub; |
27 |
while (!feof(fd)) { |
|
28 |
if (fgets(line, sizeof(line)-1, fd) == NULL) |
|
29 |
break; |
|
30 |
||
31 |
if (!strncmp(line, "MemTotal", 8)) |
|
32 |
sscanf(line+9, "%*[ ]%d%*[^\n]", &total); |
|
33 |
||
34 |
if (!strncmp(line, "MemFree", 7)) |
|
35 |
sscanf(line+8, "%*[ ]%d%*[^\n]", &free); |
|
36 |
||
15
163e96e2b941
[status-mem] MemAvailable support
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
37 |
if (!strncmp(line, "MemAvailable", 12)) { |
163e96e2b941
[status-mem] MemAvailable support
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
38 |
sscanf(line+13, "%*[ ]%d%*[^\n]", &sub); |
163e96e2b941
[status-mem] MemAvailable support
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
39 |
avail = sub; |
163e96e2b941
[status-mem] MemAvailable support
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
40 |
} |
163e96e2b941
[status-mem] MemAvailable support
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
41 |
|
1 | 42 |
if (!strncmp(line, "Buffers", 7)) { |
43 |
sscanf(line+8, "%*[ ]%d%*[^\n]", &sub); |
|
44 |
free += sub; |
|
45 |
} |
|
46 |
if (!strncmp(line, "Cached", 6)) { |
|
47 |
sscanf(line+7, "%*[ ]%d%*[^\n]", &sub); |
|
48 |
free += sub; |
|
49 |
} |
|
50 |
} |
|
51 |
fclose(fd); |
|
15
163e96e2b941
[status-mem] MemAvailable support
Stiletto <blasux@blasux.ru>
parents:
13
diff
changeset
|
52 |
if (avail != -1) free = avail; |
1 | 53 |
int used = total - free; |
54 |
||
3 | 55 |
#ifdef PANGO_MARKUP |
1 | 56 |
char *color; |
13 | 57 |
if (used*1.0/total < GREEN) color = "color='#0f0'"; |
58 |
else if (used*1.0/total > RED) color = "color='#f33'"; |
|
59 |
else if (used*1.0/total > ORANGE) color = "color='#fa0'"; |
|
1 | 60 |
else color = ""; |
61 |
printf("<span %s>%d/%d</span>\n", color, used/1024, total/1024); |
|
3 | 62 |
#else |
63 |
printf("%d/%d\n", used/1024, total/1024); |
|
64 |
#endif |
|
1 | 65 |
fflush(stdout); |
66 |
sleep(UPDATE_PERIOD); |
|
67 |
} |
|
68 |
} |