diff --git a/source/lib/sysdep/os/unix/unix_executable_pathname.cpp b/source/lib/sysdep/os/unix/unix_executable_pathname.cpp deleted file mode 100644 index 5dc779583b..0000000000 --- a/source/lib/sysdep/os/unix/unix_executable_pathname.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* Copyright (C) 2025 Wildfire Games. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include "precompiled.h" - -#include "lib/sysdep/sysdep.h" - -#include "lib/os_path.h" -#include "lib/posix/posix_dlfcn.h" - -#include -#include -#include -#include - -#define GNU_SOURCE -#include "mocks/dlfcn.h" -#include "mocks/unistd.h" - -OsPath unix_ExecutablePathname() -{ - // Find the executable's filename - Dl_info dl_info; - memset(&dl_info, 0, sizeof(dl_info)); - if (!T::dladdr((void *)sys_ExecutablePathname, &dl_info) || !dl_info.dli_fname) - return OsPath(); - const char* path = dl_info.dli_fname; - - // If this looks like an absolute path, use realpath to get the normalized - // path (with no '.' or '..') - if (path[0] == '/') - { - char resolved[PATH_MAX]; - if (!realpath(path, resolved)) - return OsPath(); - return resolved; - } - - // If this looks like a relative path, resolve against cwd and use realpath - if (strchr(path, '/')) - { - char cwd[PATH_MAX]; - if (!T::getcwd(cwd, PATH_MAX)) - return OsPath(); - - char absolute[PATH_MAX]; - int ret = snprintf(absolute, PATH_MAX, "%s/%s", cwd, path); - if (ret < 0 || ret >= PATH_MAX) - return OsPath(); // path too long, or other error - char resolved[PATH_MAX]; - if (!realpath(absolute, resolved)) - return OsPath(); - return resolved; - } - - // If it's not a path at all, i.e. it's just a filename, we'd - // probably have to search through PATH to find it. - // That's complex and should be uncommon, so don't bother. - return OsPath(); -} diff --git a/source/lib/sysdep/os/unix/unix_executable_pathname.h b/source/lib/sysdep/os/unix/unix_executable_pathname.h deleted file mode 100644 index 78fbeb6dd6..0000000000 --- a/source/lib/sysdep/os/unix/unix_executable_pathname.h +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright (C) 2014 Wildfire Games. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef INCLUDED_UNIX_EXECUTABLE_PATHNAME -#define INCLUDED_UNIX_EXECUTABLE_PATHNAME - -#include "lib/os_path.h" - -OsPath unix_ExecutablePathname(); - -#endif // INCLUDED_UNIX_EXECUTABLE_PATHNAME diff --git a/source/lib/sysdep/tests/test_sysdep.h b/source/lib/sysdep/tests/test_sysdep.h index a217f30d7f..657de94037 100644 --- a/source/lib/sysdep/tests/test_sysdep.h +++ b/source/lib/sysdep/tests/test_sysdep.h @@ -39,12 +39,6 @@ #include #include -#if OS_BSD || OS_LINUX -# include "lib/sysdep/os/unix/unix_executable_pathname.h" -# include "mocks/dlfcn.h" -# include "mocks/unistd.h" -#endif - class TestSysdep : public CxxTest::TestSuite { public: @@ -69,127 +63,6 @@ public: TSM_ASSERT_EQUALS(L"Path: "+path.string(), wstat(path, &s), 0); } - void test_unix_ExecutablePathname() - { -#if !(OS_BSD || OS_LINUX) - } -#else - // Since the implementation uses realpath, the tested files need to - // really exist. So set up a directory tree for testing: - - const char* tmpdir = getenv("TMPDIR"); - if (! tmpdir) tmpdir = P_tmpdir; - - char root[PATH_MAX]; - sprintf_s(root, ARRAY_SIZE(root), "%s/pyrogenesis-test-sysdep-XXXXXX", tmpdir); - TS_ASSERT(mkdtemp(root)); - - char rootres[PATH_MAX]; - TS_ASSERT(realpath(root, rootres)); - - std::string rootstr(rootres); - OsPath rootstrw(rootstr); - - const char* dirs[] = { - "/example", - "/example/a", - "/example/a/b", - "/example/a/b/c", - "/example/a/b/d", - "/example/a/e", - "/example/a/f" - }; - const char* files[] = { - "/example/executable", - "/example/a/f/executable", - }; - for (size_t i = 0; i < ARRAY_SIZE(dirs); ++i) - { - std::string name = rootstr + dirs[i]; - TS_ASSERT_EQUALS(mkdir(name.c_str(), 0700), 0); - } - for (size_t i = 0; i < ARRAY_SIZE(files); ++i) - { - std::string name = rootstr + files[i]; - FILE* f = fopen(name.c_str(), "w"); - TS_ASSERT(f); - fclose(f); - } - - // Try with absolute paths - { - Mock_dladdr d(rootstr+"/example/executable"); - TS_ASSERT_PATH_EQUALS(unix_ExecutablePathname(), rootstrw/L"example/executable"); - } - { - Mock_dladdr d(rootstr+"/example/./a/b/../e/../../executable"); - TS_ASSERT_PATH_EQUALS(unix_ExecutablePathname(), rootstrw/L"example/executable"); - } - - // Try with relative paths - { - Mock_dladdr d("./executable"); - Mock_getcwd m(rootstr+"/example"); - TS_ASSERT_PATH_EQUALS(unix_ExecutablePathname(), rootstrw/L"example/executable"); - } - { - Mock_dladdr d("./executable"); - Mock_getcwd m(rootstr+"/example/"); - TS_ASSERT_PATH_EQUALS(unix_ExecutablePathname(), rootstrw/L"example/executable"); - } - { - Mock_dladdr d("../d/../../f/executable"); - Mock_getcwd m(rootstr+"/example/a/b/c"); - TS_ASSERT_PATH_EQUALS(unix_ExecutablePathname(), rootstrw/L"example/a/f/executable"); - } - - // Try with pathless names - { - Mock_dladdr d("executable"); - TS_ASSERT_PATH_EQUALS(unix_ExecutablePathname(), OsPath()); - } - - // Clean up the temporary files - for (size_t i = 0; i < ARRAY_SIZE(files); ++i) - { - std::string name = rootstr + files[i]; - TS_ASSERT_EQUALS(unlink(name.c_str()), 0); - } - for (ssize_t i = ARRAY_SIZE(dirs)-1; i >= 0; --i) // reverse order - { - std::string name(root); - name += dirs[i]; - TS_ASSERT_EQUALS(rmdir(name.c_str()), 0); - } - TS_ASSERT_EQUALS(rmdir(root), 0); - } - - // Mock classes for test_unix_ExecutablePathname - class Mock_dladdr : public T::Base_dladdr - { - public: - Mock_dladdr(const std::string& fname) : fname_(fname) { } - int dladdr(void* /*addr*/, Dl_info *info) { - info->dli_fname = fname_.c_str(); - return 1; - } - private: - std::string fname_; - }; - - class Mock_getcwd : public T::Base_getcwd - { - public: - Mock_getcwd(const std::string& buf) : buf_(buf) { } - char* getcwd(char* buf, size_t size) { - strncpy_s(buf, size, buf_.c_str(), buf_.length()); - return buf; - } - private: - std::string buf_; - }; -#endif // !(OS_BSD || OS_LINUX) - private: bool path_is_absolute(const wchar_t* path) {