forked from jhx0/bsdfetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenfetch.c
411 lines (353 loc) · 11.1 KB
/
openfetch.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright (c) 2022-2023 jhx <jhx0x00@gmail.com>
* Copyright (c) 2024-2025 David Uhden Collado <david@uhden.dev>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/utsname.h>
#ifdef __OpenBSD__
#include <sys/time.h>
#include <sys/sensors.h>
#include "sysctlbyname.h"
#endif
#include <err.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/resource.h>
#include <libgen.h>
#define VERSION "1.0"
#define LOGO_PATH1 "./logo/"
#define LOGO_PATH2 "/usr/local/share/doc/logo/"
#define MAX_LOGO_LINES 30
#define MAX_LINE_LENGTH 256
// Structure to store logo information
typedef struct {
const char *name;
char lines[MAX_LOGO_LINES][MAX_LINE_LENGTH];
} Logo;
// Buffer for general use
static char buf[BUFSIZ];
// Function to read logo from a file
void read_logo(Logo *logo, const char *filename) {
FILE *file;
char filepath1[MAX_LINE_LENGTH], filepath2[MAX_LINE_LENGTH];
// Construct file paths
snprintf(filepath1, sizeof(filepath1), "%s%s", LOGO_PATH1, filename);
snprintf(filepath2, sizeof(filepath2), "%s%s", LOGO_PATH2, filename);
// Try to open the logo file from the first path
if ((file = fopen(filepath1, "r")) == NULL) {
// If it fails, try the second path
if ((file = fopen(filepath2, "r")) == NULL) {
fprintf(stderr, "Error: Unable to open logo file from either path.\n");
exit(1);
}
}
// Read the file line by line
int i = 0;
while (fgets(logo->lines[i], MAX_LINE_LENGTH, file) && i < MAX_LOGO_LINES) {
logo->lines[i][strcspn(logo->lines[i], "\n")] = 0; // Remove newline character
i++;
}
fclose(file);
}
// Function to append formatted information to the info array
void append_info(char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int *info_lines, const char *fmt, ...) {
if (*info_lines >= MAX_LOGO_LINES) {
return;
}
va_list args;
va_start(args, fmt);
vsnprintf(info[*info_lines], MAX_LINE_LENGTH, fmt, args);
va_end(args);
(*info_lines)++;
}
// Function to get system information
void get_sysinfo(char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int *info_lines) {
struct utsname un;
char *p;
// Get system information using uname
if (uname(&un)) {
err(1, "uname() failed");
}
append_info(info, info_lines, "OS: %s", un.sysname);
append_info(info, info_lines, "Release: %s", un.release);
if ((p = strchr(un.version, ':')) != NULL) {
*p = '\0'; // Truncate build-strings for NetBSD
}
append_info(info, info_lines, "Version: %s", un.version);
append_info(info, info_lines, "Arch: %s", un.machine);
}
// Function to get hostname
void get_hostname(char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int *info_lines) {
// Get the hostname
if (gethostname(buf, sizeof buf) == -1) {
err(1, "gethostname() failed");
}
append_info(info, info_lines, "Host: %s", buf);
}
// Function to get shell information
void get_shell(char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int *info_lines) {
struct passwd *pw;
char *sh, *p;
// Get the shell from environment variable or passwd entry
if ((sh = getenv("SHELL")) == NULL || *sh == '\0') {
if ((pw = getpwuid(getuid())) == NULL) {
err(1, "getpwuid() failed");
}
sh = pw->pw_shell;
}
if ((p = strrchr(sh, '/')) != NULL && *(p + 1) != '\0') {
sh = ++p;
}
append_info(info, info_lines, "Shell: %s", sh);
}
// Function to get user information
void get_user(char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int *info_lines) {
struct passwd *pw;
char *p;
// Get the user from environment variable or passwd entry
if ((p = getenv("USER")) == NULL || *p == '\0') {
if ((pw = getpwuid(getuid())) == NULL) {
err(1, "getpwuid() failed");
}
p = pw->pw_name;
}
append_info(info, info_lines, "User: %s", p);
}
// Function to get the number of installed packages
void get_packages(char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int *info_lines) {
const char *cmd;
// Select the appropriate command based on the OS
#if defined(__OpenBSD__) || defined(__NetBSD__)
cmd = "/usr/sbin/pkg_info";
#elif defined(__FreeBSD__) || defined(__DragonFly__)
cmd = "/usr/sbin/pkg info";
#else
#error Unsupported BSD variant
#endif
// Execute the command and count the number of lines in the output
FILE *f = popen(cmd, "r");
if (f == NULL) {
err(1, "popen(%s) failed", cmd);
}
size_t npkg = 0;
while (fgets(buf, sizeof buf, f) != NULL) {
if (strchr(buf, '\n') != NULL) {
npkg++;
}
}
if (pclose(f) != 0) {
err(1, "pclose(%s) failed", cmd);
}
append_info(info, info_lines, "Packages: %zu", npkg);
}
// Function to get system uptime
void get_uptime(char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int *info_lines) {
long up, days, hours, mins;
struct timeval t;
size_t tsz = sizeof t;
// Get the system boot time
if (sysctlbyname("kern.boottime", &t, &tsz, NULL, 0) == -1) {
err(1, "failed to get kern.boottime");
}
// Calculate the uptime
up = (long)(time(NULL) - t.tv_sec + 30);
days = up / 86400;
up %= 86400;
hours = up / 3600;
up %= 3600;
mins = up / 60;
append_info(info, info_lines, "Uptime: %ldd %ldh %ldm", days, hours, mins);
}
// Function to get memory information
void get_memory(char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int *info_lines) {
unsigned long long ramsz;
long pagesz, npages;
// Get the system page size and number of pages
if ((pagesz = sysconf(_SC_PAGESIZE)) == -1) {
err(1, "error getting system page-size");
}
if ((npages = sysconf(_SC_PHYS_PAGES)) == -1) {
err(1, "error getting no. of system pages");
}
// Calculate the total RAM size
ramsz = (unsigned long long)(pagesz * npages) / (1024 * 1024);
append_info(info, info_lines, "RAM: %llu MB", ramsz);
}
// Function to get load average
void get_loadavg(char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int *info_lines) {
double lavg[3] = {0.0};
// Get the load average
if (getloadavg(lavg, 3) == -1) {
err(1, "getloadavg() failed");
}
append_info(info, info_lines, "Load average (1 min): %.2lf", lavg[0]);
append_info(info, info_lines, "Load average (5 min): %.2lf", lavg[1]);
append_info(info, info_lines, "Load average (15 min): %.2lf", lavg[2]);
}
// Function to get CPU information
void get_cpu(char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int *info_lines) {
long ncpu, nmax;
size_t sz;
// Get the number of online and configured processors
if ((ncpu = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
err(1, "sysconf(_SC_NPROCESSORS_ONLN) failed");
}
if ((nmax = sysconf(_SC_NPROCESSORS_CONF)) == -1) {
err(1, "sysconf(_SC_NPROCESSORS_CONF) failed");
}
// Get the CPU brand or model
sz = sizeof buf;
if (sysctlbyname("machdep.cpu_brand", buf, &sz, NULL, 0) == -1) {
if (sysctlbyname("hw.model", buf, &sz, NULL, 0) == -1) {
err(1, "error getting CPU info.");
}
}
buf[sz] = '\0';
append_info(info, info_lines, "CPU: %s", buf);
append_info(info, info_lines, "Cores: %ld of %ld processors online", ncpu, nmax);
#if defined(__FreeBSD__) || defined(__DragonFly__)
#define CELSIUS 273.15
// Get the temperature of each CPU core
for (int i = 0; i < (int)ncpu; i++) {
int temp = 0;
sz = sizeof temp;
snprintf(buf, sizeof buf, "dev.cpu.%d.temperature", i);
if (sysctlbyname(buf, &temp, &sz, NULL, 0) == -1) {
return;
}
snprintf(buf, sizeof buf, "Core [%d] Temp", i + 1);
append_info(info, info_lines, "%s: %.1f °C", buf, (temp * 0.1) - CELSIUS);
}
#elif defined(__OpenBSD__)
// Get the CPU temperature on OpenBSD
struct sensor sensors;
int mib[5];
mib[0] = CTL_HW;
mib[1] = HW_SENSORS;
mib[2] = 0;
mib[3] = SENSOR_TEMP;
mib[4] = 0;
sz = sizeof sensors;
if (sysctl(mib, 5, &sensors, &sz, NULL, 0) == -1) {
return;
}
append_info(info, info_lines, "CPU Temp: %d °C", (int)((float)(sensors.value - 273150000) / 1E6));
#elif defined(__NetBSD__)
// Get the CPU temperature on NetBSD
const char *const cmd = "/usr/sbin/envstat | awk '/ cpu[0-9]+ temperature: / { print $3 }'";
FILE *f = popen(cmd, "r");
if (f == NULL) {
err(1, "popen(%s) failed", cmd);
}
int i = 0;
while (fgets(buf, sizeof buf, f) != NULL) {
float temp;
if (sscanf(buf, "%f", &temp) != 1) {
break;
}
snprintf(buf, sizeof buf, "Core [%d] Temp", ++i);
append_info(info, info_lines, "%s: %.1f °C", buf, temp);
}
if (pclose(f) != 0) {
err(1, "pclose(%s) failed", cmd);
}
#endif
}
// Function to print the logo and system information side by side
void print_logo_and_info(Logo *logo, char info[MAX_LOGO_LINES][MAX_LINE_LENGTH], int info_lines) {
int logo_lines = 0;
while (logo->lines[logo_lines][0] != '\0' && logo_lines < MAX_LOGO_LINES) {
logo_lines++;
}
int max_lines = logo_lines > info_lines ? logo_lines : info_lines;
// Print the logo and system information side by side
for (int i = 0; i < max_lines; i++) {
if (i < logo_lines) {
printf("%-40s", logo->lines[i]);
} else {
printf("%-40s", "");
}
if (i < info_lines) {
printf(" %s", info[i]);
}
printf("\n");
}
}
// Function to detect the OS and print the corresponding logo and system information
void detect_and_print_logo(void) {
Logo logo = {0};
char info[MAX_LOGO_LINES][MAX_LINE_LENGTH] = {{0}};
int info_lines = 0;
// Select the appropriate logo file based on the OS
#if defined(__FreeBSD__)
read_logo(&logo, "freebsd.txt");
#elif defined(__OpenBSD__)
read_logo(&logo, "openbsd.txt");
#elif defined(__NetBSD__)
read_logo(&logo, "netbsd.txt");
#elif defined(__DragonFly__)
read_logo(&logo, "dragonfly.txt");
#else
#error Unsupported BSD variant
#endif
// Get system information
get_sysinfo(info, &info_lines);
get_hostname(info, &info_lines);
get_shell(info, &info_lines);
get_user(info, &info_lines);
get_packages(info, &info_lines);
get_uptime(info, &info_lines);
get_memory(info, &info_lines);
get_loadavg(info, &info_lines);
get_cpu(info, &info_lines);
// Print the logo and system information
print_logo_and_info(&logo, info, info_lines);
}
// Function to print the version information and exit
_Noreturn static void version(void) {
printf("%s - version %s (%s)\n",
basename((char *)getprogname()),
VERSION,
__DATE__);
exit(EXIT_SUCCESS);
}
// Function to print the usage information and exit
_Noreturn static void usage(void) {
printf("USAGE: %s [-h|-v]\n"
" -h Show help this text.\n"
" -v Show version information.\n",
basename((char *)getprogname()));
exit(EXIT_SUCCESS);
}
// Main function
int main(int argc, char **argv) {
if (argc == 2) {
if (strcmp(argv[1], "-h") == 0) {
usage();
} else if (strcmp(argv[1], "-v") == 0) {
version();
}
}
// Detect the OS and print the corresponding logo and system information
detect_and_print_logo();
return EXIT_SUCCESS;
}