Remove unix_ExecutablePathname

The function was used as a fallback for when a platform specific approach
isn't available. Given that it effectively is a roundabout way to derive
it from argv[0] which we use as a generic fallback anyway just remove
it.

This further allows to make cxxtest optional.

Refs: #8618
Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
This commit is contained in:
Ralph Sennhauser 2025-12-20 14:09:55 +01:00
parent ae6d3bfc4e
commit 2d317d509d
No known key found for this signature in database
3 changed files with 0 additions and 236 deletions

View file

@ -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 <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#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();
}

View file

@ -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

View file

@ -39,12 +39,6 @@
#include <cwchar> #include <cwchar>
#include <string> #include <string>
#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 class TestSysdep : public CxxTest::TestSuite
{ {
public: public:
@ -69,127 +63,6 @@ public:
TSM_ASSERT_EQUALS(L"Path: "+path.string(), wstat(path, &s), 0); 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: private:
bool path_is_absolute(const wchar_t* path) bool path_is_absolute(const wchar_t* path)
{ {