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; |
|
25 |
int sub; |
|
26 |
while (!feof(fd)) { |
|
27 |
if (fgets(line, sizeof(line)-1, fd) == NULL) |
|
28 |
break; |
|
29 |
|
|
30 |
if (!strncmp(line, "MemTotal", 8)) |
|
31 |
sscanf(line+9, "%*[ ]%d%*[^\n]", &total); |
|
32 |
|
|
33 |
if (!strncmp(line, "MemFree", 7)) |
|
34 |
sscanf(line+8, "%*[ ]%d%*[^\n]", &free); |
|
35 |
|
|
36 |
if (!strncmp(line, "Buffers", 7)) { |
|
37 |
sscanf(line+8, "%*[ ]%d%*[^\n]", &sub); |
|
38 |
free += sub; |
|
39 |
} |
|
40 |
if (!strncmp(line, "Cached", 6)) { |
|
41 |
sscanf(line+7, "%*[ ]%d%*[^\n]", &sub); |
|
42 |
free += sub; |
|
43 |
} |
|
44 |
} |
|
45 |
fclose(fd); |
|
46 |
|
|
47 |
int used = total - free; |
|
48 |
|
3
|
49 |
#ifdef PANGO_MARKUP |
1
|
50 |
char *color; |
13
|
51 |
if (used*1.0/total < GREEN) color = "color='#0f0'"; |
|
52 |
else if (used*1.0/total > RED) color = "color='#f33'"; |
|
53 |
else if (used*1.0/total > ORANGE) color = "color='#fa0'"; |
1
|
54 |
else color = ""; |
|
55 |
printf("<span %s>%d/%d</span>\n", color, used/1024, total/1024); |
3
|
56 |
#else |
|
57 |
printf("%d/%d\n", used/1024, total/1024); |
|
58 |
#endif |
1
|
59 |
fflush(stdout); |
|
60 |
sleep(UPDATE_PERIOD); |
|
61 |
} |
|
62 |
} |