Added __BSD__ preprocessor directive to be used with all bsd OSes

This commit is contained in:
adri 2022-06-24 14:30:18 +02:00
parent efa5970da0
commit 2292074ec4
2 changed files with 25 additions and 25 deletions

View file

@ -22,15 +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 PREFIX = bin
LIBDIR = lib LIBDIR = lib
ETC_DIR = /etc ETC_DIR = /etc
MANDIR = share/man/man1 MANDIR = share/man/man1
else ifeq ($(PLATFORM), BSD) else ifeq ($(PLATFORM), OpenBSD)
CFLAGS += -D__OPENBSD__ CFLAGS += -D__OPENBSD__ -D__BSD__
CFLAGS_DEBUG += -D__OPENBSD__ CFLAGS_DEBUG += -D__OPENBSD__ -D__BSD__
PREFIX = bin PREFIX = bin
LIBDIR = lib LIBDIR = lib
ETC_DIR = /etc ETC_DIR = /etc

View file

@ -28,16 +28,16 @@
#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__) #if defined(__OPENBSD__)
#include <sys/time.h> #include <sys/time.h>
#else #else
#include <time.h> #include <time.h>
#endif // defined(__OPENBSD__) #endif // defined(__OPENBSD__)
#else // defined(__APPLE__) || defined(__FREEBSD__) #else // defined(__APPLE__) || defined(__BSD__)
#ifdef __FREEBSD__ #ifdef __BSD__
#else // defined(__FREEBSD__) || defined(_WIN32) #else // defined(__BSD__) || defined(_WIN32)
#ifndef _WIN32 #ifndef _WIN32
#ifndef __OPENBSD__ #ifndef __OPENBSD__
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
@ -46,8 +46,8 @@
#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>
@ -334,7 +334,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;
@ -519,7 +519,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
@ -564,7 +564,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
@ -719,13 +719,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
@ -934,7 +934,7 @@ struct info get_info()
// os version, cpu and board info // os version, cpu and board info
FILE* os_release = fopen("/etc/os-release", "r"); // os name file FILE* os_release = fopen("/etc/os-release", "r"); // os name file
#ifndef __FREEBSD__ #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 -a | egrep -i 'hw.model'", "r"); // cpu name command for freebsd
@ -977,8 +977,8 @@ struct info get_info()
break; break;
} }
} }
#elif defined(__FREEBSD__) || defined(__APPLE__) #elif defined(__BSD__) || defined(__APPLE__)
#if defined(__FREEBSD__) #if defined(__BSD__)
#define HOSTCTL "hw.hv_vendor" #define HOSTCTL "hw.hv_vendor"
#elif defined(__APPLE__) #elif defined(__APPLE__)
#define HOSTCTL "hw.model" #define HOSTCTL "hw.model"
@ -1019,13 +1019,13 @@ struct info get_info()
// getting cpu name // getting cpu name
while (fgets(buffer, sizeof(buffer), cpuinfo)) { while (fgets(buffer, sizeof(buffer), cpuinfo)) {
#ifdef __FREEBSD__ #ifdef __BSD__
if (sscanf(buffer, "hw.model: %[^\n]", user_info.cpu_model)) if (sscanf(buffer, "hw.model: %[^\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");
@ -1052,7 +1052,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
@ -1070,7 +1070,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
@ -1121,7 +1121,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
@ -1179,7 +1179,7 @@ struct info get_info()
#else // if not _WIN32 #else // if not _WIN32
FILE* meminfo; FILE* meminfo;
#ifdef __FREEBSD__ #ifdef __BSD__
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
// 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