Merge pull request #199 from TheDarkBug/openbsd-port

Openbsd port (close #198)
This commit is contained in:
TheDarkBug 2022-06-24 20:10:07 +02:00 committed by GitHub
commit e851d7dcb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 109 additions and 61 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
uwufetch uwufetch
*.core
*.zip *.zip
*.idea *.idea
*.vscode *.vscode

View file

@ -22,8 +22,15 @@ else ifeq ($(PLATFORM), Darwin)
ETC_DIR = /etc ETC_DIR = /etc
MANDIR = local/share/man/man1 MANDIR = local/share/man/man1
else ifeq ($(PLATFORM), FreeBSD) else ifeq ($(PLATFORM), FreeBSD)
CFLAGS += -D__FREEBSD__ CFLAGS += -D__FREEBSD__ -D__BSD__
CFLAGS_DEBUG += -D__FREEBSD__ CFLAGS_DEBUG += -D__FREEBSD__ -D__BSD__
PREFIX = bin
LIBDIR = lib
ETC_DIR = /etc
MANDIR = share/man/man1
else ifeq ($(PLATFORM), OpenBSD)
CFLAGS += -D__OPENBSD__ -D__BSD__
CFLAGS_DEBUG += -D__OPENBSD__ -D__BSD__
PREFIX = bin PREFIX = bin
LIBDIR = lib LIBDIR = lib
ETC_DIR = /etc ETC_DIR = /etc

View file

@ -28,19 +28,26 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#if defined(__APPLE__) || defined(__FREEBSD__) #if defined(__APPLE__) || defined(__BSD__)
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <time.h> #if defined(__OPENBSD__)
#else // defined(__APPLE__) || defined(__FREEBSD__) #include <sys/time.h>
#ifdef __FREEBSD__ #else
#else // defined(__FREEBSD__) || defined(_WIN32) #include <time.h>
#endif // defined(__OPENBSD__)
#else // defined(__APPLE__) || defined(__BSD__)
#ifdef __BSD__
#else // defined(__BSD__) || defined(_WIN32)
#ifndef _WIN32 #ifndef _WIN32
#include <sys/sysinfo.h> #ifndef __OPENBSD__
#else // _WIN32 #include <sys/sysinfo.h>
#else // __OPENBSD__
#endif // __OPENBSD__
#else // _WIN32
#include <sysinfoapi.h> #include <sysinfoapi.h>
#endif // _WIN32 #endif // _WIN32
#endif // defined(__FREEBSD__) || defined(_WIN32) #endif // defined(__BSD__) || defined(_WIN32)
#endif // defined(__APPLE__) || defined(__FREEBSD__) #endif // defined(__APPLE__) || defined(__BSD__)
#ifndef _WIN32 #ifndef _WIN32
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/utsname.h> #include <sys/utsname.h>
@ -263,6 +270,7 @@ int pkgman(struct info* user_info)
{"nix-store -q --requisites /run/current-system/sw 2> /dev/null | wc -l", "(nix)"}, {"nix-store -q --requisites /run/current-system/sw 2> /dev/null | wc -l", "(nix)"},
{"pacman -Qq 2> /dev/null | wc -l", "(pacman)"}, {"pacman -Qq 2> /dev/null | wc -l", "(pacman)"},
{"pkg info 2>/dev/null | wc -l", "(pkg)"}, {"pkg info 2>/dev/null | wc -l", "(pkg)"},
{"pkg_info 2>/dev/null | wc -l | sed \"s/ //g\"", "(pkg)"},
{"port installed 2> /dev/null | tail -n +2 | wc -l", "(port)"}, {"port installed 2> /dev/null | tail -n +2 | wc -l", "(port)"},
{"brew list 2> /dev/null | wc -l", "(brew)"}, {"brew list 2> /dev/null | wc -l", "(brew)"},
{"rpm -qa --last 2> /dev/null | wc -l", "(rpm)"}, {"rpm -qa --last 2> /dev/null | wc -l", "(rpm)"},
@ -327,7 +335,7 @@ int uptime_apple() {
} }
#endif #endif
#ifdef __FREEBSD__ #ifdef __BSD__
// gets the uptime for freebsd // gets the uptime for freebsd
int uptime_freebsd() { // this code is from coreutils uptime: https://github.com/coreutils/coreutils/blob/master/src/uptime.c int uptime_freebsd() { // this code is from coreutils uptime: https://github.com/coreutils/coreutils/blob/master/src/uptime.c
int boot_time = 0; int boot_time = 0;
@ -512,7 +520,7 @@ int print_info(struct configuration* config_flags, struct info* user_info) {
#ifdef __APPLE__ #ifdef __APPLE__
user_info->uptime = uptime_apple(); user_info->uptime = uptime_apple();
#else #else
#ifdef __FREEBSD__ #ifdef __BSD__
user_info->uptime = uptime_freebsd(); user_info->uptime = uptime_freebsd();
#else #else
#ifdef _WIN32 #ifdef _WIN32
@ -553,11 +561,11 @@ void write_cache(struct info* user_info) {
fprintf(stderr, "Failed to write to %s!", cache_file); fprintf(stderr, "Failed to write to %s!", cache_file);
return; return;
} }
// writing all info to the cache file // writing all info to the cache file
#ifdef __APPLE__ #ifdef __APPLE__
user_info->uptime = uptime_apple(); user_info->uptime = uptime_apple();
#else #else
#ifdef __FREEBSD__ #ifdef __BSD__
user_info->uptime = uptime_freebsd(); user_info->uptime = uptime_freebsd();
#else #else
#ifndef _WIN32 #ifndef _WIN32
@ -580,9 +588,9 @@ void write_cache(struct info* user_info) {
#ifdef __APPLE__ #ifdef __APPLE__
/* char brew_command[2048]; /* char brew_command[2048];
sprintf(brew_command, "ls $(brew --cellar) | wc -l | awk -F' ' '{print \" sprintf(brew_command, "ls $(brew --cellar) | wc -l | awk -F' ' '{print \"
\x1b[34mw w \x1b[0m\x1b[1mPKGS\x1b[0m \"$1 \" (brew)\"}' \x1b[34mw w \x1b[0m\x1b[1mPKGS\x1b[0m \"$1 \" (brew)\"}'
> %s", cache_file); system(brew_command); */ > %s", cache_file); system(brew_command); */
#endif #endif
fclose(cache_fp); fclose(cache_fp);
return; return;
@ -617,10 +625,10 @@ int read_cache(struct info* user_info) {
} }
/* /*
This replaces all terms in a string with another term. This replaces all terms in a string with another term.
replace("Hello World!", "World", "everyone") replace("Hello World!", "World", "everyone")
This returns "Hello everyone!". This returns "Hello everyone!".
*/ */
void replace(char* original, char* search, char* replacer) { void replace(char* original, char* search, char* replacer) {
char* ch; char* ch;
char buffer[1024]; char buffer[1024];
@ -635,10 +643,10 @@ void replace(char* original, char* search, char* replacer) {
} }
/* /*
This replaces all terms in a string with another term, case insensitive This replaces all terms in a string with another term, case insensitive
replace("Hello wOrLd!", "WoRlD", "everyone") replace("Hello wOrLd!", "WoRlD", "everyone")
This returns "Hello everyone!". This returns "Hello everyone!".
*/ */
void replace_ignorecase(char* original, char* search, char* replacer) { void replace_ignorecase(char* original, char* search, char* replacer) {
char* ch; char* ch;
char buffer[1024]; char buffer[1024];
@ -712,13 +720,13 @@ void print_ascii(struct info* user_info) {
int print_cache(struct configuration* config_flags, struct info* user_info) { int print_cache(struct configuration* config_flags, struct info* user_info) {
#ifndef __APPLE__ #ifndef __APPLE__
#ifndef _WIN32 #ifndef _WIN32
#ifndef __FREEBSD__ #ifndef __BSD__
sysinfo(&user_info->sys); // to get uptime sysinfo(&user_info->sys); // to get uptime
#endif #endif
#endif #endif
FILE* meminfo; FILE* meminfo;
#ifdef __FREEBSD__ #ifdef __BSD__
meminfo = popen("LANG=EN_us freecolor -om 2> /dev/null", "r"); // free alternative meminfo = popen("LANG=EN_us freecolor -om 2> /dev/null", "r"); // free alternative
#else #else
// getting memory info from /proc/meminfo: https://github.com/KittyKatt/screenFetch/issues/386#issuecomment-249312716 // getting memory info from /proc/meminfo: https://github.com/KittyKatt/screenFetch/issues/386#issuecomment-249312716
@ -915,7 +923,7 @@ struct info get_info()
struct info user_info = {0}; struct info user_info = {0};
char buffer[256]; // line buffer char buffer[256]; // line buffer
// get terminal width used to truncate long names // get terminal width used to truncate long names
#ifndef _WIN32 #ifndef _WIN32
ioctl(STDOUT_FILENO, TIOCGWINSZ, &user_info.win); ioctl(STDOUT_FILENO, TIOCGWINSZ, &user_info.win);
user_info.target_width = user_info.win.ws_col - 30; user_info.target_width = user_info.win.ws_col - 30;
@ -926,11 +934,15 @@ struct info get_info()
#endif // _WIN32 #endif // _WIN32
// os version, cpu and board info // os version, cpu and board info
FILE* os_release = fopen("/etc/os-release", "r"); // os name file #ifdef __OPENBSD__
#ifndef __FREEBSD__ FILE* os_release = popen("echo ID=openbsd", "r"); // os-release does not exist in OpenBSD
#else
FILE* os_release = fopen("/etc/os-release", "r"); // os name file
#endif
#ifndef __BSD__
FILE* cpuinfo = fopen("/proc/cpuinfo", "r"); // cpu name file for not-freebsd systems FILE* cpuinfo = fopen("/proc/cpuinfo", "r"); // cpu name file for not-freebsd systems
#else #else
FILE* cpuinfo = popen("sysctl -a | egrep -i 'hw.model'", "r"); // cpu name command for freebsd FILE* cpuinfo = popen("sysctl hw.model", "r"); // cpu name command for freebsd
#endif #endif
// trying to get some kind of information about the name of the computer (hopefully a product full name) // trying to get some kind of information about the name of the computer (hopefully a product full name)
FILE* model_fp /* = fopen("/sys/devices/virtual/dmi/id/product_version", "r") */; // trying to get product version FILE* model_fp /* = fopen("/sys/devices/virtual/dmi/id/product_version", "r") */; // trying to get product version
@ -970,15 +982,23 @@ struct info get_info()
break; break;
} }
} }
#elif defined(__FREEBSD__) || defined(__APPLE__) #elif defined(__BSD__) || defined(__APPLE__)
#if defined(__FREEBSD__) #if defined(__BSD__) && !defined(__OPENBSD__)
#define HOSTCTL "hw.hv_vendor" #define HOSTCTL "hw.hv_vendor"
#elif defined(__APPLE__) #elif defined(__APPLE__)
#define HOSTCTL "hw.model" #define HOSTCTL "hw.model"
#elif defined(__OPENBSD__)
#define HOSTCTL "hw.product"
#endif #endif
model_fp = popen("sysctl -a " HOSTCTL, "r"); model_fp = popen("sysctl " HOSTCTL, "r");
while (fgets(buffer, sizeof(buffer), model_fp)) while (fgets(buffer, sizeof(buffer), model_fp))
if (sscanf(buffer, HOSTCTL ": %[^\n]", user_info.model)) break; if (sscanf(buffer, HOSTCTL
#ifndef __OPENBSD__
": "
#else
"="
#endif
"%[^\n]", user_info.model)) break;
#endif // _WIN32 #endif // _WIN32
if (os_release) { // get normal vars if os_release exists if (os_release) { // get normal vars if os_release exists
while (fgets(buffer, sizeof(buffer), os_release) && !(sscanf(buffer, "\nID=\"%s\"", user_info.os_name) || sscanf(buffer, "\nID=%s", user_info.os_name))) while (fgets(buffer, sizeof(buffer), os_release) && !(sscanf(buffer, "\nID=\"%s\"", user_info.os_name) || sscanf(buffer, "\nID=%s", user_info.os_name)))
@ -989,7 +1009,7 @@ struct info get_info()
user_info.os_name[os_name_len - 1] = '\0'; user_info.os_name[os_name_len - 1] = '\0';
} }
/* trying to detect amogos because in its os-release file ID value is just "debian", /* trying to detect amogos because in its os-release file ID value is just "debian",
will be removed when amogos will have an os-release file with ID=amogos */ will be removed when amogos will have an os-release file with ID=amogos */
if (strcmp(user_info.os_name, "debian") == 0 || strcmp(user_info.os_name, "raspbian") == 0) { if (strcmp(user_info.os_name, "debian") == 0 || strcmp(user_info.os_name, "raspbian") == 0) {
DIR* amogos_plymouth = opendir("/usr/share/plymouth/themes/amogos"); DIR* amogos_plymouth = opendir("/usr/share/plymouth/themes/amogos");
if (amogos_plymouth) { if (amogos_plymouth) {
@ -998,27 +1018,33 @@ struct info get_info()
} }
} }
/* if (model_fp) { // what the fuck is this? I don't remember writing this code /* if (model_fp) { // what the fuck is this? I don't remember writing this code
while (fgets(buffer, sizeof(buffer), model_fp) && !(sscanf(buffer, "%[^\n]", user_info.model))) while (fgets(buffer, sizeof(buffer), model_fp) && !(sscanf(buffer, "%[^\n]", user_info.model)))
; ;
char version[32]; char version[32];
while (fgets(buffer, sizeof(buffer), model_fp)) { while (fgets(buffer, sizeof(buffer), model_fp)) {
if (sscanf(buffer, "%[^\n]", version)) { if (sscanf(buffer, "%[^\n]", version)) {
strcat(user_info.model, " "); strcat(user_info.model, " ");
strcat(user_info.model, version); strcat(user_info.model, version);
break; break;
} }
} }
} */ } */
// getting cpu name // getting cpu name
while (fgets(buffer, sizeof(buffer), cpuinfo)) { while (fgets(buffer, sizeof(buffer), cpuinfo)) {
#ifdef __BSD__
if (sscanf(buffer, "hw.model"
#ifdef __FREEBSD__ #ifdef __FREEBSD__
if (sscanf(buffer, "hw.model: %[^\n]", user_info.cpu_model)) ": "
#elif defined(__OPENBSD__)
"="
#endif
"%[^\n]", user_info.cpu_model))
break; break;
#else #else
if (sscanf(buffer, "model name : %[^\n]", user_info.cpu_model)) if (sscanf(buffer, "model name : %[^\n]", user_info.cpu_model))
break; break;
#endif // __FREEBSD__ #endif // __BSD__
} }
// getting username // getting username
char* tmp_user = getenv("USER"); char* tmp_user = getenv("USER");
@ -1045,7 +1071,7 @@ struct info get_info()
model_fp = popen("getprop ro.product.model", "r"); model_fp = popen("getprop ro.product.model", "r");
while (fgets(buffer, sizeof(buffer), model_fp) && !sscanf(buffer, "%[^\n]", user_info.model)) while (fgets(buffer, sizeof(buffer), model_fp) && !sscanf(buffer, "%[^\n]", user_info.model))
; ;
#ifndef __FREEBSD__ #ifndef __BSD__
while (fgets(buffer, sizeof(buffer), cpuinfo) && !sscanf(buffer, "Hardware : %[^\n]", user_info.cpu_model)) while (fgets(buffer, sizeof(buffer), cpuinfo) && !sscanf(buffer, "Hardware : %[^\n]", user_info.cpu_model))
; ;
#endif #endif
@ -1063,7 +1089,7 @@ struct info get_info()
} else } else
sprintf(user_info.os_name, "unknown"); // if no option before is working, the system is unknown sprintf(user_info.os_name, "unknown"); // if no option before is working, the system is unknown
} }
#ifndef __FREEBSD__ #ifndef __BSD__
fclose(cpuinfo); fclose(cpuinfo);
#endif #endif
#ifndef _WIN32 #ifndef _WIN32
@ -1073,8 +1099,10 @@ struct info get_info()
sprintf(user_info.shell, "%s", ""); sprintf(user_info.shell, "%s", "");
else else
sprintf(user_info.shell, "%s", tmp_shell); sprintf(user_info.shell, "%s", tmp_shell);
if (strlen(user_info.shell) > 16) // android shell was too long, this works only for termux #ifdef __linux__
if (strlen(user_info.shell) > 16) // android shell name was too long
memmove(&user_info.shell, &user_info.shell[27], strlen(user_info.shell)); memmove(&user_info.shell, &user_info.shell[27], strlen(user_info.shell));
#endif
#else // if _WIN32 #else // if _WIN32
// cpu name // cpu name
cpuinfo = popen("wmic cpu get caption", "r"); cpuinfo = popen("wmic cpu get caption", "r");
@ -1109,12 +1137,12 @@ struct info get_info()
#endif // _WIN32 #endif // _WIN32
truncate_str(user_info.cpu_model, user_info.target_width); truncate_str(user_info.cpu_model, user_info.target_width);
// system resources // system resources
#ifndef _WIN32 #ifndef _WIN32
uname(&user_info.sys_var); uname(&user_info.sys_var);
#endif // _WIN32 #endif // _WIN32
#ifndef __APPLE__ #ifndef __APPLE__
#ifndef __FREEBSD__ #ifndef __BSD__
#ifndef _WIN32 #ifndef _WIN32
sysinfo(&user_info.sys); // somehow this function has to be called again in print_info() sysinfo(&user_info.sys); // somehow this function has to be called again in print_info()
#else #else
@ -1143,7 +1171,7 @@ struct info get_info()
if (kernel_fp) pclose(kernel_fp); if (kernel_fp) pclose(kernel_fp);
#endif // _WIN32 #endif // _WIN32
// ram // ram
#ifndef __APPLE__ #ifndef __APPLE__
#ifdef _WIN32 #ifdef _WIN32
FILE* mem_used_fp = popen("wmic os get freevirtualmemory", "r"); // free memory FILE* mem_used_fp = popen("wmic os get freevirtualmemory", "r"); // free memory
@ -1172,25 +1200,37 @@ struct info get_info()
#else // if not _WIN32 #else // if not _WIN32
FILE* meminfo; FILE* meminfo;
#ifdef __FREEBSD__ #ifdef __BSD__
#ifndef __OPENBSD__
meminfo = popen("LANG=EN_us freecolor -om 2> /dev/null", "r"); // free alternative for freebsd meminfo = popen("LANG=EN_us freecolor -om 2> /dev/null", "r"); // free alternative for freebsd
#else
meminfo = popen("LANG=EN_us vmstat 2> /dev/null | grep -v 'procs' | grep -v 'r' | awk '{print $3 \" / \" $4}'", "r"); // free alternative for openbsd
#endif
#else #else
// getting memory info from /proc/meminfo: https://github.com/KittyKatt/screenFetch/issues/386#issuecomment-249312716 // getting memory info from /proc/meminfo: https://github.com/KittyKatt/screenFetch/issues/386#issuecomment-249312716
meminfo = fopen("/proc/meminfo", "r"); // popen("LANG=EN_us free -m 2> /dev/null", "r"); // get ram info with free meminfo = fopen("/proc/meminfo", "r"); // popen("LANG=EN_us free -m 2> /dev/null", "r"); // get ram info with free
#endif #endif
// brackets are here to restrict the access to this int variables, which are temporary // brackets are here to restrict the access to this int variables, which are temporary
{ {
#ifndef __OPENBSD__
int memtotal = 0, shmem = 0, memfree = 0, buffers = 0, cached = 0, sreclaimable = 0; int memtotal = 0, shmem = 0, memfree = 0, buffers = 0, cached = 0, sreclaimable = 0;
#endif
while (fgets(buffer, sizeof(buffer), meminfo)) { while (fgets(buffer, sizeof(buffer), meminfo)) {
#ifndef __OPENBSD__
sscanf(buffer, "MemTotal: %d", &memtotal); sscanf(buffer, "MemTotal: %d", &memtotal);
sscanf(buffer, "Shmem: %d", &shmem); sscanf(buffer, "Shmem: %d", &shmem);
sscanf(buffer, "MemFree: %d", &memfree); sscanf(buffer, "MemFree: %d", &memfree);
sscanf(buffer, "Buffers: %d", &buffers); sscanf(buffer, "Buffers: %d", &buffers);
sscanf(buffer, "Cached: %d", &cached); sscanf(buffer, "Cached: %d", &cached);
sscanf(buffer, "SReclaimable: %d", &sreclaimable); sscanf(buffer, "SReclaimable: %d", &sreclaimable);
#else
sscanf(buffer, "%dM / %dM", &user_info.ram_used, &user_info.ram_total);
#endif
} }
#ifndef __OPENBSD__
user_info.ram_total = memtotal / 1024; user_info.ram_total = memtotal / 1024;
user_info.ram_used = ((memtotal + shmem) - (memfree + buffers + cached + sreclaimable)) / 1024; user_info.ram_used = ((memtotal + shmem) - (memfree + buffers + cached + sreclaimable)) / 1024;
#endif
} }
// while (fgets(buffer, sizeof(buffer), meminfo)) // old way to get ram usage that uses the "free" command // while (fgets(buffer, sizeof(buffer), meminfo)) // old way to get ram usage that uses the "free" command
@ -1271,7 +1311,7 @@ struct info get_info()
truncate_str(user_info.gpu_model[i], user_info.target_width); truncate_str(user_info.gpu_model[i], user_info.target_width);
} }
// Resolution // Resolution
#ifndef _WIN32 #ifndef _WIN32
FILE* resolution = popen("xwininfo -root 2> /dev/null | grep -E 'Width|Height'", "r"); FILE* resolution = popen("xwininfo -root 2> /dev/null | grep -E 'Width|Height'", "r");
while (fgets(buffer, sizeof(buffer), resolution)) { while (fgets(buffer, sizeof(buffer), resolution)) {
@ -1281,7 +1321,7 @@ struct info get_info()
#endif #endif
if (strcmp(user_info.os_name, "windows")) MOVE_CURSOR = "\033[21C"; // to print windows logo on not windows systems if (strcmp(user_info.os_name, "windows")) MOVE_CURSOR = "\033[21C"; // to print windows logo on not windows systems
// package count // package count
#ifdef _WIN32 #ifdef _WIN32
user_info.pkgs = pkgman(&user_info, config_flags); user_info.pkgs = pkgman(&user_info, config_flags);
#else // _WIN32 #else // _WIN32
@ -1297,8 +1337,8 @@ struct info get_info()
} }
/* prints distribution list /* prints distribution list
distributions are listed by distribution branch distributions are listed by distribution branch
to make the output easier to understand by the user.*/ to make the output easier to understand by the user.*/
void list(char* arg) { void list(char* arg) {
printf("%s -d <options>\n" printf("%s -d <options>\n"
" Available distributions:\n" " Available distributions:\n"