Repository navigation
Expand file tree
/
Copy pathbsd.cpp
More file actions
214 lines (166 loc) · 5.1 KB
/
Copy pathbsd.cpp
File metadata and controls
214 lines (166 loc) · 5.1 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <iostream>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <string.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/vmmeter.h>
#include <vm/vm_param.h>
#include <memstat.h>
#include <X11/Xlib.h>
#include <stdlib.h>
#include <regex>
using namespace std;
char* get_cpu_brand(void) {
char* brand = (char*) malloc(128);
char final[128];
size_t size_b = sizeof(final);
if (sysctlbyname("hw.model", &final, &size_b, NULL, 0) < 0)
{
perror("sysctl");
}
regex reg("\\(TM\\)|\\(R\\)");
if (regex_search(final, reg)) {
brand = strdup(regex_replace(final, reg, "").c_str());
} else {
strncpy(brand, final, 128);
}
return brand;
}
float get_total_system_memory()
{
int mib[2] = { CTL_HW, HW_REALMEM };
u_int namelen = sizeof(mib) / sizeof(mib[0]);
long long size;
size_t len = sizeof(size);
if (sysctl(mib, namelen, &size, &len, NULL, 0) < 0)
{
perror("sysctl");
}
return size;
}
float get_used_system_memory()
{
float mem_used;
int mem_wire, mem_inactive,mem_cache;
int mem_free;
size_t size_ty = sizeof(128);
if (sysctlbyname("vm.stats.vm.v_wire_count", &mem_wire, &size_ty, NULL, 0) < 0) { perror("sysctl"); }
if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &size_ty, NULL, 0) < 0) { perror("sysctl"); }
if (sysctlbyname("vm.stats.vm.v_cache_count", &mem_cache, &size_ty, NULL, 0) < 0) { perror("sysctl"); }
if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &size_ty, NULL, 0) < 0) { perror("sysctl"); }
mem_used = get_total_system_memory() - (4096 * (mem_inactive + mem_cache + mem_free));
return mem_used;
}
char* get_hostname()
{
char* hostname = (char*) malloc(128*sizeof(char));
char final[128];
size_t size_h = sizeof(final);
if (sysctlbyname("kern.hostname", &final, &size_h, NULL, 0) < 0)
{
perror("sysctl");
}
strncpy(hostname, final, 128);
return hostname;
}
float get_uptime()
{
struct timeval boottime;
size_t len = sizeof(boottime);
int mib[2] = { CTL_KERN, KERN_BOOTTIME };
if( sysctl(mib, 2, &boottime, &len, NULL, 0) < 0 )
{
return -1.0;
}
time_t bsec = boottime.tv_sec, csec = time(NULL);
return difftime(csec, bsec);
}
char* get_gpu_model()
{
FILE *gpu;
char* buffer = (char*)malloc(128*sizeof(char));
gpu = popen("pciconf -l -v | grep -A2 vga | cut -d \\' -f 2 | tail -n +2 | tr '\n' ' '", "r");
fgets(buffer, 128, gpu);
pclose(gpu);
strtok(buffer, "\n");
return buffer;
}
char* get_shell()
{
char* shell = (char*) malloc(128*sizeof(char));
strncpy(shell, getenv("SHELL"), 128);
return shell;
}
char* get_terminal()
{
char* terminal = (char*) malloc(128*sizeof(char));
strncpy(terminal, getenv("TERM"), 128);
return terminal;
}
void get_disk_usage(float _disk[2])
{
struct statfs statf;
statfs(".", &statf);
_disk[0] = statf.f_bsize * (statf.f_blocks - statf.f_bavail);
_disk[1] = statf.f_bsize * statf.f_blocks;
}
char* get_username()
{
return getpwuid(geteuid())->pw_name;
}
char* get_os_version()
{
char* osstring = (char*) malloc(256);
char finaltype[128];
char finalrelease[128];
size_t size_ty = sizeof(finaltype);
size_t size_re = sizeof(finaltype);
if (sysctlbyname("kern.ostype", &finaltype, &size_ty, NULL, 0) < 0)
{
perror("sysctl");
}
if (sysctlbyname("kern.osrelease", &finalrelease, &size_re, NULL, 0) < 0)
{
perror("sysctl");
}
snprintf(osstring, 256, "%s %s", finaltype, finalrelease);
return osstring;
}
char* get_resolution()
{
Display* disp = XOpenDisplay(NULL);
if(disp == false)
{
return "Unknown (X Server not running?)";
}
Screen* scrn = DefaultScreenOfDisplay(disp);
int height = scrn->height;
int width = scrn->width;
char* res = (char*) malloc(128*sizeof(char));
sprintf(res, "%ix%i", width, height);
return res;
}
char* art()
{
char* art_txt =
"\e[31;1m ``` ` \n "
"\e[31;1m s` `.....---.......--.``` -/ \e[91m%s\e[0m@%s\n"
"\e[31;1m +o .--` /y:` +. \e[0;1mCPU:\e[0m %s\n"
"\e[31;1m yo`:. :o `+- \e[0;1mMemory:\e[0m %s/%s (used/total)\n"
"\e[31;1m y/ -/` -o/ \e[0;1mUptime:\e[0m %s\n"
"\e[31;1m .- ::/sy+:. \e[0;1mGPU:\e[0m %s\n"
"\e[31;1m / `-- / \e[0;1mShell:\e[0m %s\n"
"\e[31;1m `: :` \e[0;1mTerminal:\e[0m %s\n"
"\e[31;1m `: :` \e[0;1mDisk:\e[0m %s/%s (used/total)\n"
"\e[31;1m / / \e[0;1mVersion:\e[0m %s\n"
"\e[31;1m .- -. \e[0;1mResolution:\e[0m %s\n"
"\e[31;1m -- -. \e[0m\n"
"\e[31;1m `:` `:` \e[0m\n"
"\e[31;1m .-- `--. \e[0m\n"
"\e[31;1m .---.....----. \e[0m\n";
return art_txt;
}