Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of Path::self() #4319

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Foundation/include/Poco/Path.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ class Foundation_API Path
/// this is the semicolon ';'. On OpenVMS systems, this is the
/// comma ','.

static std::string self();
/// Return path to the executable file, empty string if failed.
/// The path is absolute one.

static std::string current();
/// Returns the current working directory.

Expand Down
2 changes: 2 additions & 0 deletions Foundation/include/Poco/Path_UNIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Poco {
class PathImpl
{
public:
static std::string selfImpl();
static std::string currentImpl();
static std::string homeImpl();
static std::string configHomeImpl();
Expand All @@ -39,6 +40,7 @@ class PathImpl
static std::string nullImpl();
static std::string expandImpl(const std::string& path);
static void listRootsImpl(std::vector<std::string>& roots);

};


Expand Down
1 change: 1 addition & 0 deletions Foundation/include/Poco/Path_WIN32U.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Poco {
class Foundation_API PathImpl
{
public:
static std::string selfImpl();
static std::string currentImpl();
static std::string homeImpl();
static std::string configHomeImpl();
Expand Down
1 change: 1 addition & 0 deletions Foundation/include/Poco/Path_WINCE.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Poco {
class Foundation_API PathImpl
{
public:
static std::string selfImpl();
static std::string currentImpl();
static std::string homeImpl();
static std::string configHomeImpl();
Expand Down
6 changes: 6 additions & 0 deletions Foundation/src/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,12 @@ Path& Path::clear()
}


std::string Path::self()
{
return PathImpl::selfImpl();
}


std::string Path::current()
{
return PathImpl::currentImpl();
Expand Down
53 changes: 50 additions & 3 deletions Foundation/src/Path_UNIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,66 @@
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <climits>

#if !defined(POCO_VXWORKS)
#include <pwd.h>
#endif
#include <climits>

#if POCO_OS == POCO_OS_MAC_OS_X
#include <mach-o/dyld.h>
#elif POCO_OS == POCO_OS_FREE_BSD
#include <sys/sysctl.h>
#elif POCO_OS == POCO_OS_LINUX
#include <fcntl.h>
#endif


#ifndef PATH_MAX
#define PATH_MAX 1024 // fallback
#define PATH_MAX 4096 // fallback
#endif


namespace Poco {

std::string PathImpl::selfImpl()
{
std::string path;
char buf[PATH_MAX + 1] {0};

#if POCO_OS == POCO_OS_MAC_OS_X
std::uint32_t size = sizeof(buf);
if (_NSGetExecutablePath(buf, &size) == 0)
path = buf;
#elif POCO_OS == POCO_OS_FREE_BSD
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
std::size_t size = sizeof(buf);
if (sysctl(mib, 4, buf, &size, NULL, 0) == 0)
path = buf;
#elif POCO_OS == POCO_OS_NET_BSD
std::size_t size = sizeof(buf);
int n = readlink("/proc/curproc/exe", buf, size);
if (n > 0 && n < PATH_MAX)
path = buf;
#elif POCO_OS == POCO_OS_SOLARIS
char * execName = getexecname();
if (execName)
path = execName;
#elif POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_ANDROID
const std::size_t size = sizeof(buf);
int n = readlink("/proc/self/exe", buf, size);
if (n > 0 && n < PATH_MAX)
path = buf;
#else
throw Poco::NotImplementedException("File path of the current program not implemented on this platform.");
#endif
matejk marked this conversation as resolved.
Show resolved Hide resolved

return path;
}


std::string PathImpl::currentImpl()
{
Expand Down
11 changes: 10 additions & 1 deletion Foundation/src/Path_WIN32U.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
#include "Poco/Exception.h"
#include "Poco/UnWindows.h"


namespace Poco {

std::string PathImpl::selfImpl()
{
std::string path;
Buffer<wchar_t> buf(MAX_PATH_LEN);
DWORD n = GetModuleFileNameW(NULL, buf.begin(), MAX_PATH_LEN);

if (n > 0 && n < MAX_PATH_LEN)
UnicodeConverter::toUTF8(buf.begin(), path);
return path;
}

std::string PathImpl::currentImpl()
{
Expand Down
4 changes: 4 additions & 0 deletions Foundation/src/Path_WINCE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

namespace Poco {

std::string PathImpl::selfImpl()
{
return("");
}

std::string PathImpl::currentImpl()
{
Expand Down
25 changes: 25 additions & 0 deletions Foundation/testsuite/src/PathTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,30 @@ void PathTest::testWindowsSystem()
#endif
}

void PathTest::testSelf()
{
std::string self = Path::self();
std::cout << self << std::endl;

#if POCO_OS == POCO_OS_MAC_OS_X \
|| POCO_OS == POCO_OS_FREE_BSD \
|| POCO_OS == POCO_OS_NET_BSD \
|| POCO_OS == POCO_OS_SOLARIS \
|| POCO_OS == POCO_OS_LINUX \
|| POCO_OS == POCO_OS_ANDROID \
|| POCO_OS == POCO_OS_WINDOWS_NT

assertTrue(!self.empty());
Path p(self);

assertTrue(p.isAbsolute());
assertTrue(p.isFile());
assertTrue(self.find("testrunner") != std::string::npos);
#else
std::cout << "Path::self() not implemented for this platform."
#endif
}


void PathTest::setUp()
{
Expand Down Expand Up @@ -1662,6 +1686,7 @@ CppUnit::Test* PathTest::suite()
CppUnit_addTest(pSuite, PathTest, testResolve);
CppUnit_addTest(pSuite, PathTest, testPushPop);
CppUnit_addTest(pSuite, PathTest, testWindowsSystem);
CppUnit_addTest(pSuite, PathTest, testSelf);

return pSuite;
}
1 change: 1 addition & 0 deletions Foundation/testsuite/src/PathTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class PathTest: public CppUnit::TestCase
void testResolve();
void testPushPop();
void testWindowsSystem();
void testSelf();

void setUp();
void tearDown();
Expand Down
Loading