-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
172 lines (143 loc) · 4.09 KB
/
functions.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "main.h"
#include <unistd.h>
#include <dirent.h>
#include <sys/sysinfo.h>
#include <sys/utsname.h>
const char* get_username() {
return getenv("USER");
}
const char* get_hostname() {
gethostname(buffer, BUFFER_SIZE);
return buffer;
}
const char* get_distribution() {
FILE* file = fopen("/etc/os-release", "r");
if (file) {
while (fgets(buffer, BUFFER_SIZE, file)) {
if (strncmp(buffer, "PRETTY_NAME=", 12) == 0) {
char* name = buffer + 12;
name[strcspn(name, "\n")] = 0;
memmove(name, name + 1, strlen(name));
name[strlen(name) - 1] = '\0';
fclose(file);
return strdup(name);
}
}
}
fclose(file);
return "Unknown";
}
const char* get_machine_name() {
FILE* file_arm = fopen("/sys/firmware/devicetree/base/model", "r");
FILE* file_x86 = fopen("/sys/devices/virtual/dmi/id/board_name", "r");
if (file_arm) {
fgets(buffer, BUFFER_SIZE, file_arm);
fclose(file_arm);
}
else if (file_x86) {
fgets(buffer, BUFFER_SIZE, file_x86);
buffer[strcspn(buffer, "\n")] = '\0';
fclose(file_x86);
}
else {
snprintf(buffer, BUFFER_SIZE, "Unknown");
}
return buffer;
}
const char* get_kernel() {
static struct utsname kernel;
uname(&kernel);
return kernel.release;
}
const char* get_uptime() {
struct sysinfo sys_info;
sysinfo(&sys_info);
long seconds = sys_info.uptime;
int days = seconds / (24 * 3600);
int hours = (seconds % (24 * 3600)) / 3600;
int minutes = (seconds % 3600) / 60;
if (days > 0)
snprintf(buffer, BUFFER_SIZE, "%dd, %dh, %dm", days, hours, minutes);
else if (hours > 0)
snprintf(buffer, BUFFER_SIZE, "%dh, %dm", hours, minutes);
else
snprintf(buffer, BUFFER_SIZE, "%dm", minutes);
return buffer;
}
static int get_installed_packages_pacman() {
int package_count = 0;
DIR* dir = opendir("/var/lib/pacman/local");
if (dir) {
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR && entry->d_name[0] != '.') {
package_count++;
}
}
closedir(dir);
}
return package_count;
}
static int get_installed_packages_emerge() {
int package_count = 0;
DIR* dir = opendir("/var/db/pkg");
if (!dir)
return 0;
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
if (!(entry->d_type == DT_DIR && entry->d_name[0] != '.'))
continue;
snprintf(buffer, BUFFER_SIZE, "/var/db/pkg/%s", entry->d_name);
DIR* dir2 = opendir(buffer);
if (!dir2)
continue;
struct dirent* entry2;
while ((entry2 = readdir(dir2)) != NULL) {
if (entry2->d_type == DT_DIR && entry2->d_name[0] != '.') {
package_count++;
}
}
closedir(dir2);
}
closedir(dir);
return package_count;
}
static int get_installed_packages_dpkg() {
int package_count = 0;
FILE* file = fopen("/var/lib/dpkg/status", "r");
if (!file)
return 0;
while (fgets(buffer, BUFFER_SIZE, file)) {
if (strncmp(buffer, "Package:", 8) == 0) {
package_count++;
}
}
fclose(file);
return package_count;
}
int get_installed_packages() {
return get_installed_packages_pacman()
+ get_installed_packages_emerge()
+ get_installed_packages_dpkg();
}
const char* get_memory_usage() {
static char buffer[BUFFER_SIZE];
FILE* meminfo = fopen("/proc/meminfo", "r");
unsigned long total_mem = 0, free_mem = 0, buffers = 0, cached = 0, sreclaimable = 0, shmem = 0;
char label[32]; unsigned long value;
while (fscanf(meminfo, "%31s %lu kB", label, &value) == 2) {
if (!strcmp(label, "MemTotal:")) total_mem = value;
else if (!strcmp(label, "MemFree:")) free_mem = value;
else if (!strcmp(label, "Buffers:")) buffers = value;
else if (!strcmp(label, "Cached:")) cached = value;
else if (!strcmp(label, "SReclaimable:")) sreclaimable = value;
else if (!strcmp(label, "Shmem:")) shmem = value;
}
fclose(meminfo);
unsigned long used_mem = total_mem - free_mem - buffers - cached + shmem - sreclaimable;
const char* suffix = "MB";
double display_total = total_mem / 1024.0, display_used = used_mem / 1024.0;
if (display_total > 1024) { display_total /= 1024.0; display_used /= 1024.0; suffix = "GB"; }
snprintf(buffer, BUFFER_SIZE, "%.2f %s / %.2f %s", display_used, suffix, display_total, suffix);
return buffer;
}