-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.c
executable file
·79 lines (66 loc) · 1.79 KB
/
mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "mem.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void*
smalloc(size_t size)
{
void* m = malloc(size);
if (m == NULL)
die("error: malloc() failed.");
return m;
}
void*
srealloc(void* ptr, size_t size)
{
void* m = realloc(ptr, size);
if (m == NULL)
die("error: realloc() failed.");
return m;
}
char*
get_mem(void)
{
int mtotal = 0, mfree = 0, mbuffers = 0, mcache = 0, msreclaimable = 0;
int stotal = 0, sfree = 0;
FILE* fp = fopen("/proc/meminfo", "rb");
if (!fp)
return 0;
char* cb = smalloc(sizeof(char) * 1024);
char* cb_cpy = smalloc(sizeof(char) * 1024);
strcpy(cb, "");
strcpy(cb_cpy, "");
while (fgets(cb, 1024, fp) != NULL) {
strcpy(cb_cpy, cb);
char* ptr = strtok(cb_cpy, " ");
ptr = strtok(NULL, " ");
if (ptr[strlen(ptr) - 1] == '\n')
ptr[strlen(ptr) - 1] = '\0';
char* b;
int val = (int)strtol(ptr, &b, 10);
if (strstw("MemTotal:", cb))
mtotal = val;
else if (strstw("MemFree:", cb))
mfree = val;
else if (strstw("Buffers:", cb))
mbuffers = val;
else if (strstw("Cached:", cb))
mcache = val;
else if (strstw("SwapTotal:", cb))
stotal = val;
else if (strstw("SwapFree:", cb))
sfree = val;
else if (strstw("SReclaimable:", cb))
msreclaimable = val;
}
int used = mtotal + stotal - mfree - sfree - mbuffers - mcache - msreclaimable;
used = (int)round(used / 1024);
fclose(fp);
free(cb);
free(cb_cpy);
char* str_used = smalloc(sizeof(char) * 25);
snprintf(str_used, 25, "%d", used);
return str_used;
}