From 9a541c12140e8768fabbbd8834cd0e1570df344b Mon Sep 17 00:00:00 2001 From: Greg V Date: Wed, 17 Oct 2018 16:48:17 +0300 Subject: [PATCH] Add FreeBSD support to os.cpp --- src/os.cpp | 26 ++++++++++++++++++++------ src/os.hpp | 2 ++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/os.cpp b/src/os.cpp index 6df463d8a5..86bef6d5e8 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -50,10 +50,13 @@ typedef SSIZE_T ssize_t; #endif -#if defined(ZIG_OS_LINUX) +#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) #include #endif +#if defined(ZIG_OS_FREEBSD) +#include +#endif #if defined(__MACH__) #include @@ -75,7 +78,9 @@ static clock_serv_t cclock; #if defined(__APPLE__) && !defined(environ) #include #define environ (*_NSGetEnviron()) -#endif +#elif defined(ZIG_OS_FREEBSD) +extern char **environ; +#endif #if defined(ZIG_OS_POSIX) static void populate_termination(Termination *term, int status) { @@ -1442,6 +1447,15 @@ Error os_self_exe_path(Buf *out_path) { } buf_resize(out_path, amt); return ErrorNone; +#elif defined(ZIG_OS_FREEBSD) + buf_resize(out_path, PATH_MAX); + int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + size_t cb = PATH_MAX; + if (sysctl(mib, 4, buf_ptr(out_path), &cb, nullptr, 0) != 0) { + return ErrorUnexpected; + } + buf_resize(out_path, cb); + return ErrorNone; #endif return ErrorFileNotFound; } @@ -1747,7 +1761,7 @@ Error os_get_app_data_dir(Buf *out_path, const char *appname) { buf_resize(out_path, 0); buf_appendf(out_path, "%s/Library/Application Support/%s", home_dir, appname); return ErrorNone; -#elif defined(ZIG_OS_LINUX) +#elif defined(ZIG_OS_POSIX) const char *home_dir = getenv("HOME"); if (home_dir == nullptr) { // TODO use /etc/passwd @@ -1760,7 +1774,7 @@ Error os_get_app_data_dir(Buf *out_path, const char *appname) { } -#if defined(ZIG_OS_LINUX) +#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, void *data) { ZigList *libs = reinterpret_cast< ZigList *>(data); if (info->dlpi_name[0] == '/') { @@ -1771,7 +1785,7 @@ static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, #endif Error os_self_exe_shared_libs(ZigList &paths) { -#if defined(ZIG_OS_LINUX) +#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) paths.resize(0); dl_iterate_phdr(self_exe_shared_libs_callback, &paths); return ErrorNone; @@ -1940,7 +1954,7 @@ Error os_file_mtime(OsFile file, OsTimeStamp *mtime) { mtime->sec = (((ULONGLONG) last_write_time.dwHighDateTime) << 32) + last_write_time.dwLowDateTime; mtime->nsec = 0; return ErrorNone; -#elif defined(ZIG_OS_LINUX) +#elif defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) struct stat statbuf; if (fstat(file, &statbuf) == -1) return ErrorFileSystem; diff --git a/src/os.hpp b/src/os.hpp index 30083971eb..a552c4461c 100644 --- a/src/os.hpp +++ b/src/os.hpp @@ -24,6 +24,8 @@ #define ZIG_OS_WINDOWS #elif defined(__linux__) #define ZIG_OS_LINUX +#elif defined(__FreeBSD__) +#define ZIG_OS_FREEBSD #else #define ZIG_OS_UNKNOWN #endif