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
|
#include "../include/cfetch.h"
#include <stdio.h>
#include <sys/utsname.h>
void get_kernel(system_info_t *info) {
#ifdef __linux__
struct utsname u;
if (uname(&u) == 0) {
snprintf(info->kernel, sizeof(info->kernel), "%.30s %.30s", u.sysname, u.release);
printf("Kernel: %s\n", info->kernel);
} else {
perror("Error while accessing uname");
return;
}
#endif
return;
}
void get_cpu(system_info_t *info) {
FILE* file = fopen("/proc/cpuinfo", "r");
//check file existence
if (!file) {
snprintf(info->cpu, sizeof(info->cpu), "Unknown");
return;
}
char line[256];
char cpu_name[256] = "Unknown";
char *colon = NULL;
//read per line
while (fgets(line, sizeof(line), file)) {
char* p = strstr(line, "model name");
if (p != NULL) {
colon = strchr(p, ':') + 1; //skip :
break;
}
}
if (colon != NULL) {
snprintf(info->cpu, sizeof(info->cpu), "%s", trim_whitespace(colon));
printf("CPU: %s\n", info->cpu);
}
fclose(file);
return;
}
void get_memory(system_info_t *info) {
FILE *file = fopen("/proc/meminfo", "r");
if (!file) {
perror("Error fopen meminfo");
return;
}
char line[256];
long total = 0;
long available = 0;
// read per line
while (fgets(line, sizeof(line), file)) {
if (sscanf(line, "MemTotal: %ld kB", &total) == 1) continue;
if (sscanf(line, "MemAvailable: %ld kB", &available) == 1) continue;
}
fclose(file);
printf("Memory: %.2f GB / %.2f GB\n", (total - available) / (1024.0 * 1024.0), total / (1024.0 * 1024.0));
return;
}
|