Started porting to openbsd, indented some code

This commit is contained in:
adri 2022-06-24 14:24:03 +02:00
parent 3ce863a90a
commit efa5970da0
2 changed files with 49 additions and 35 deletions

View file

@ -28,6 +28,13 @@ else ifeq ($(PLATFORM), FreeBSD)
LIBDIR = lib LIBDIR = lib
ETC_DIR = /etc ETC_DIR = /etc
MANDIR = share/man/man1 MANDIR = share/man/man1
else ifeq ($(PLATFORM), BSD)
CFLAGS += -D__OPENBSD__
CFLAGS_DEBUG += -D__OPENBSD__
PREFIX = bin
LIBDIR = lib
ETC_DIR = /etc
MANDIR = share/man/man1
else ifeq ($(PLATFORM), windows32) else ifeq ($(PLATFORM), windows32)
CC = gcc CC = gcc
PREFIX = "C:\Program Files" PREFIX = "C:\Program Files"

View file

@ -30,13 +30,20 @@
#include <unistd.h> #include <unistd.h>
#if defined(__APPLE__) || defined(__FREEBSD__) #if defined(__APPLE__) || defined(__FREEBSD__)
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <time.h> #if defined(__OPENBSD__)
#else // defined(__APPLE__) || defined(__FREEBSD__) #include <sys/time.h>
#else
#include <time.h>
#endif // defined(__OPENBSD__)
#else // defined(__APPLE__) || defined(__FREEBSD__)
#ifdef __FREEBSD__ #ifdef __FREEBSD__
#else // defined(__FREEBSD__) || defined(_WIN32) #else // defined(__FREEBSD__) || 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(__FREEBSD__) || defined(_WIN32)
@ -553,7 +560,7 @@ 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
@ -580,9 +587,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 +624,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 +642,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];
@ -915,7 +922,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;
@ -989,7 +996,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,17 +1005,17 @@ 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)) {
@ -1109,7 +1116,7 @@ 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
@ -1143,7 +1150,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
@ -1271,7 +1278,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 +1288,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 +1304,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"