Files
fastfetch/src/common/impl/FFPlatform_unix.c
T

249 lines
7.6 KiB
C
Raw Normal View History

2023-01-15 15:57:26 +01:00
#include "FFPlatform_private.h"
2026-01-06 09:48:38 +08:00
#include "common/FFstrbuf.h"
#include "common/stringUtils.h"
#include "common/io.h"
2023-01-15 15:57:26 +01:00
#include "fastfetch_config.h"
#include <unistd.h>
#include <pwd.h>
2023-08-24 10:49:59 +08:00
#include <limits.h>
2023-01-15 15:57:26 +01:00
#include <sys/utsname.h>
#include <paths.h>
2023-01-15 15:57:26 +01:00
2023-08-24 10:49:59 +08:00
#ifdef __APPLE__
#include <libproc.h>
2023-11-08 00:02:00 +08:00
#include <sys/sysctl.h>
2024-10-27 11:43:36 +08:00
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
2023-08-24 10:49:59 +08:00
#include <sys/sysctl.h>
2025-02-05 02:08:28 +01:00
#elif defined(__HAIKU__)
#include <image.h>
#include <OS.h>
2023-08-24 10:49:59 +08:00
#endif
2023-08-24 14:20:48 +08:00
static void getExePath(FFPlatform* platform)
{
2025-12-12 09:03:18 +08:00
char exePath[PATH_MAX];
2025-05-19 15:07:46 +00:00
#if defined(__linux__) || defined (__GNU__)
2024-01-28 09:56:36 +08:00
ssize_t exePathLen = readlink("/proc/self/exe", exePath, sizeof(exePath) - 1);
if (exePathLen >= 0)
exePath[exePathLen] = '\0';
2023-08-24 14:20:48 +08:00
#elif defined(__APPLE__)
2026-02-12 14:08:47 +08:00
int exePathLen = proc_pidpath((pid_t) platform->pid, exePath, sizeof(exePath));
2024-10-27 11:43:36 +08:00
#elif defined(__FreeBSD__) || defined(__NetBSD__)
2024-01-28 09:56:36 +08:00
size_t exePathLen = sizeof(exePath);
2023-08-24 14:20:48 +08:00
if(sysctl(
2024-10-27 11:43:36 +08:00
(int[]){CTL_KERN,
#ifdef __FreeBSD__
2026-02-12 13:56:55 +08:00
KERN_PROC, KERN_PROC_PATHNAME, platform->pid
2024-10-27 11:43:36 +08:00
#else
2026-02-12 13:56:55 +08:00
KERN_PROC_ARGS, platform->pid, KERN_PROC_PATHNAME
2024-10-27 11:43:36 +08:00
#endif
2024-10-31 00:06:37 +08:00
}, 4,
2024-01-28 09:56:36 +08:00
exePath, &exePathLen,
2023-08-24 14:20:48 +08:00
NULL, 0
2024-01-26 10:19:21 +08:00
) < 0)
2023-08-24 14:20:48 +08:00
exePathLen = 0;
2024-01-26 10:19:21 +08:00
else
exePathLen--; // remove terminating NUL
2024-10-04 15:58:17 +08:00
#elif defined(__OpenBSD__)
size_t exePathLen = 0;
#elif defined(__sun)
ssize_t exePathLen = readlink("/proc/self/path/a.out", exePath, sizeof(exePath) - 1);
if (exePathLen >= 0)
exePath[exePathLen] = '\0';
2025-02-05 02:08:28 +01:00
#elif defined(__HAIKU__)
size_t exePathLen = 0;
image_info info;
int32 cookie = 0;
while (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) {
if (info.type == B_APP_IMAGE) {
2025-12-12 09:03:18 +08:00
exePathLen = strlcpy(exePath, info.name, sizeof(exePath));
2025-02-05 02:08:28 +01:00
break;
}
}
2023-08-24 14:20:48 +08:00
#endif
if (exePathLen > 0)
2024-01-28 09:56:36 +08:00
{
ffStrbufEnsureFree(&platform->exePath, PATH_MAX);
if (realpath(exePath, platform->exePath.chars))
2024-01-28 09:56:36 +08:00
ffStrbufRecalculateLength(&platform->exePath);
else
ffStrbufSetNS(&platform->exePath, (uint32_t) exePathLen, exePath);
}
2023-08-24 14:20:48 +08:00
}
static void platformPathAddEnv(FFlist* dirs, const char* env)
{
const char* envValue = getenv(env);
if(!ffStrSet(envValue))
return;
FF_STRBUF_AUTO_DESTROY value = ffStrbufCreateA(64);
ffStrbufAppendS(&value, envValue);
uint32_t startIndex = 0;
while (startIndex < value.length)
{
uint32_t colonIndex = ffStrbufNextIndexC(&value, startIndex, ':');
value.chars[colonIndex] = '\0';
if(!ffStrSet(value.chars + startIndex))
{
startIndex = colonIndex + 1;
continue;
}
ffPlatformPathAddAbsolute(dirs, value.chars + startIndex);
startIndex = colonIndex + 1;
}
}
2023-01-15 15:57:26 +01:00
static void getHomeDir(FFPlatform* platform, const struct passwd* pwd)
{
2023-02-21 10:02:04 +08:00
const char* home = pwd ? pwd->pw_dir : getenv("HOME");
2023-01-15 15:57:26 +01:00
ffStrbufAppendS(&platform->homeDir, home);
ffStrbufEnsureEndsWithC(&platform->homeDir, '/');
}
static void getCacheDir(FFPlatform* platform)
{
const char* cache = getenv("XDG_CACHE_HOME");
if(ffStrSet(cache))
{
ffStrbufAppendS(&platform->cacheDir, cache);
ffStrbufEnsureEndsWithC(&platform->cacheDir, '/');
}
else
{
ffStrbufAppend(&platform->cacheDir, &platform->homeDir);
ffStrbufAppendS(&platform->cacheDir, ".cache/");
}
}
static void getConfigDirs(FFPlatform* platform)
{
2025-01-08 09:44:45 +08:00
// Always make sure `${XDG_CONFIG_HOME:-$HOME/.config}` is the first entry
platformPathAddEnv(&platform->configDirs, "XDG_CONFIG_HOME");
2023-01-15 15:57:26 +01:00
ffPlatformPathAddHome(&platform->configDirs, platform, ".config/");
#if defined(__APPLE__)
2023-01-16 18:23:54 +08:00
ffPlatformPathAddHome(&platform->configDirs, platform, "Library/Preferences/");
2023-05-26 15:47:19 +08:00
ffPlatformPathAddHome(&platform->configDirs, platform, "Library/Application Support/");
2023-01-15 15:57:26 +01:00
#endif
2025-02-05 02:08:28 +01:00
#if defined(__HAIKU__)
ffPlatformPathAddHome(&platform->configDirs, platform, "config/settings/");
#endif
2023-01-15 15:57:26 +01:00
ffPlatformPathAddHome(&platform->configDirs, platform, "");
platformPathAddEnv(&platform->configDirs, "XDG_CONFIG_DIRS");
2023-01-15 15:57:26 +01:00
#if !defined(__APPLE__)
2023-02-21 10:02:04 +08:00
ffPlatformPathAddAbsolute(&platform->configDirs, FASTFETCH_TARGET_DIR_ETC "/xdg/");
2023-01-15 15:57:26 +01:00
#endif
2023-02-21 10:02:04 +08:00
ffPlatformPathAddAbsolute(&platform->configDirs, FASTFETCH_TARGET_DIR_ETC "/");
ffPlatformPathAddAbsolute(&platform->configDirs, FASTFETCH_TARGET_DIR_INSTALL_SYSCONF "/");
2023-01-15 15:57:26 +01:00
}
static void getDataDirs(FFPlatform* platform)
{
platformPathAddEnv(&platform->dataDirs, "XDG_DATA_HOME");
2023-01-15 15:57:26 +01:00
ffPlatformPathAddHome(&platform->dataDirs, platform, ".local/share/");
2023-01-15 16:26:08 +01:00
2023-08-24 10:49:59 +08:00
// Add ${currentExePath}/../share
2023-08-24 14:20:48 +08:00
if (platform->exePath.length > 0)
2023-08-24 10:49:59 +08:00
{
2023-08-24 14:20:48 +08:00
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateCopy(&platform->exePath);
ffStrbufSubstrBeforeLastC(&path, '/');
ffStrbufSubstrBeforeLastC(&path, '/');
ffStrbufAppendS(&path, "/share");
ffPlatformPathAddAbsolute(&platform->dataDirs, path.chars);
2023-08-24 10:49:59 +08:00
}
#ifdef __APPLE__
2023-02-21 10:02:04 +08:00
ffPlatformPathAddHome(&platform->dataDirs, platform, "Library/Application Support/");
2023-01-15 16:26:08 +01:00
#endif
2023-01-15 15:57:26 +01:00
ffPlatformPathAddHome(&platform->dataDirs, platform, "");
platformPathAddEnv(&platform->dataDirs, "XDG_DATA_DIRS");
#ifdef _PATH_LOCALBASE
ffPlatformPathAddAbsolute(&platform->dataDirs, _PATH_LOCALBASE "/share/");
#endif
2023-02-21 10:02:04 +08:00
ffPlatformPathAddAbsolute(&platform->dataDirs, FASTFETCH_TARGET_DIR_USR "/local/share/");
ffPlatformPathAddAbsolute(&platform->dataDirs, FASTFETCH_TARGET_DIR_USR "/share/");
2023-01-15 15:57:26 +01:00
}
static void getUserName(FFPlatform* platform, const struct passwd* pwd)
{
if (pwd)
{
ffStrbufSetS(&platform->userName, pwd->pw_name);
ffStrbufSetS(&platform->fullUserName, pwd->pw_gecos);
ffStrbufTrimSpace(&platform->fullUserName);
}
else
{
ffStrbufSetS(&platform->userName, getenv("USER"));
}
2023-01-15 15:57:26 +01:00
}
static void getHostName(FFPlatform* platform, const struct utsname* uts)
{
ffStrbufAppendS(&platform->hostName, uts->nodename);
2023-01-15 15:57:26 +01:00
}
static void getUserShell(FFPlatform* platform, const struct passwd* pwd)
{
const char* shell = getenv("SHELL");
if(!ffStrSet(shell) && pwd)
shell = pwd->pw_shell;
ffStrbufAppendS(&platform->userShell, shell);
}
2024-07-02 10:12:26 +08:00
static void getSysinfo(FFPlatformSysinfo* info, const struct utsname* uts)
2023-11-08 00:02:00 +08:00
{
2024-07-02 10:12:26 +08:00
ffStrbufAppendS(&info->name, uts->sysname);
ffStrbufAppendS(&info->release, uts->release);
ffStrbufAppendS(&info->version, uts->version);
2025-02-05 02:08:28 +01:00
#ifdef __HAIKU__
/* historical reason */
2025-02-05 10:15:41 +08:00
if (ffStrEquals(uts->machine, "BePC"))
ffStrbufSetStatic(&info->architecture, "i386");
2025-02-05 02:08:28 +01:00
else
#endif
2024-07-02 10:12:26 +08:00
ffStrbufAppendS(&info->architecture, uts->machine);
2024-10-31 00:06:37 +08:00
#if defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__)
2024-07-02 10:12:26 +08:00
size_t length = sizeof(info->pageSize);
sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &info->pageSize, &length, NULL, 0);
2023-11-08 00:02:00 +08:00
#else
2024-07-02 10:12:26 +08:00
info->pageSize = (uint32_t) sysconf(_SC_PAGESIZE);
2023-11-08 00:02:00 +08:00
#endif
}
2023-01-15 16:06:50 +01:00
void ffPlatformInitImpl(FFPlatform* platform)
2023-01-15 15:57:26 +01:00
{
2026-02-12 13:56:55 +08:00
platform->pid = (uint32_t) getpid();
2026-02-10 20:25:50 +08:00
platform->uid = getuid();
struct passwd* pwd = getpwuid(platform->uid);
2023-01-15 15:57:26 +01:00
struct utsname uts;
2024-06-16 01:02:48 +08:00
if(uname(&uts) < 0)
2023-01-15 15:57:26 +01:00
memset(&uts, 0, sizeof(uts));
2023-08-24 14:20:48 +08:00
getExePath(platform);
2023-01-15 15:57:26 +01:00
getHomeDir(platform, pwd);
getCacheDir(platform);
getConfigDirs(platform);
getDataDirs(platform);
getUserName(platform, pwd);
getHostName(platform, &uts);
getUserShell(platform, pwd);
2024-07-02 10:12:26 +08:00
getSysinfo(&platform->sysinfo, &uts);
2023-01-15 15:57:26 +01:00
}