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>
#if defined(__OPENBSD__)
#include <sys/time.h>
#else
#include <time.h> #include <time.h>
#else // defined(__APPLE__) || defined(__FREEBSD__) #endif // defined(__OPENBSD__)
#ifdef __FREEBSD__ #else // defined(__APPLE__) || defined(__BSD__)
#else // defined(__FREEBSD__) || defined(_WIN32) #ifdef __BSD__
#else // defined(__BSD__) || defined(_WIN32)
#ifndef _WIN32 #ifndef _WIN32
#ifndef __OPENBSD__
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#else // __OPENBSD__
#endif // __OPENBSD__
#else // _WIN32 #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
@ -557,7 +565,7 @@ void write_cache(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
#ifndef _WIN32 #ifndef _WIN32
@ -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
@ -926,11 +934,15 @@ struct info get_info()
#endif // _WIN32 #endif // _WIN32
// os version, cpu and board info // os version, cpu and board info
#ifdef __OPENBSD__
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 FILE* os_release = fopen("/etc/os-release", "r"); // os name file
#ifndef __FREEBSD__ #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)))
@ -1012,13 +1032,19 @@ struct info get_info()
// 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");
@ -1114,7 +1142,7 @@ struct info get_info()
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
@ -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 #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
// 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