From be614b914a1724a2426a2703e9dd4afa89c5de08 Mon Sep 17 00:00:00 2001 From: Robert Clausecker Date: Mon, 22 May 2023 14:33:20 +0200 Subject: [PATCH] Utility: implement Path::executableLocation() on FreeBSD. --- src/Corrade/Utility/Path.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Corrade/Utility/Path.cpp b/src/Corrade/Utility/Path.cpp index ddc6d8a9e..16984604c 100644 --- a/src/Corrade/Utility/Path.cpp +++ b/src/Corrade/Utility/Path.cpp @@ -71,6 +71,12 @@ #endif #endif +/* For executableLocation() on FreeBSD */ +#ifdef __FreeBSD__ +#include +#include +#endif + /* Windows */ /** @todo remove the superfluous includes when mingw is fixed (otherwise causes undefined EXTERN_C error) */ #ifdef CORRADE_TARGET_WINDOWS @@ -521,6 +527,18 @@ Containers::Optional executableLocation() { CORRADE_INTERNAL_ASSERT_OUTPUT(_NSGetExecutablePath(path.data(), &size) == 0); return path; + /* FreeBSD */ + #elif defined(__FreeBSD__) + /* Get path size, it's returned excluding the null terminator */ + std::size_t size; + constexpr int mib[4]{CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; + sysctl(mib, 4, nullptr, &size, nullptr, 0); + + /* Allocate a string of proper size and retreive the path into it */ + Containers::String path{NoInit, size} + sysctl(mib, 4, path.data(), &size, nullptr, 0); + return path; + /* Windows (not RT) */ #elif defined(CORRADE_TARGET_WINDOWS) && !defined(CORRADE_TARGET_WINDOWS_RT) /** @todo get rid of MAX_PATH */