Example #1
0
void GetExePath(int argc, char *argv[], boost::filesystem::path & path, boost::system::error_code & ec)
{
    struct pst_status pst;
    std::memset(&pst, 0, sizeof(pst));

    int res, pid = ::getpid();
    // Acquire proc info, it contains file id of text file(executable)
    res = ::pstat_getproc(&pst, sizeof(pst), 0, pid);
    if (res < 0)
    {
        ec.assign(errno, boost::system::generic_category());
        return;
    }

    pst_fid * fid_text = &pst.pst_fid_text;
    char buffer[PATH_MAX];

    // Now get pathname. According to man pstat_getpathname returns name from system cache,
    // and it actually can be missing this information.
    // In this case function will return 0, errno will be unchanged
    res = ::pstat_getpathname(buffer, PATH_MAX, fid_text);
    if (res < 0)
    {
        ec.assign(errno, boost::system::generic_category());
        return;
    }

    path.assign(buffer, buffer + res);
}
Example #2
0
void GetExePath(int argc, char *argv[], boost::filesystem::path & path, boost::system::error_code & ec)
{
    char buffer[PATH_MAX];
    if (::realpath(getexecname(), buffer) == NULL)
        ec.assign(errno, boost::system::generic_category());
    else
        path.assign(buffer);
}
Example #3
0
void GetExePath(int argc, char *argv[], boost::filesystem::path & path, boost::system::error_code & ec)
{
    char buffer[PATH_MAX];

    ssize_t len = ::readlink("/proc/self/exe", buffer, sizeof(buffer));
    if (len == -1 || len == sizeof(buffer))
        ec.assign(errno, boost::system::generic_category());
    else
        path.assign(buffer, buffer + len);
}
Example #4
0
void GetExePath(int argc, char *argv[], boost::filesystem::path & path, boost::system::error_code & ec)
{
    int mib[4];
    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PATHNAME;
    mib[3] = -1;

    char buffer[2048];
    size_t len = sizeof(buffer);

    if (sysctl(mib, 4, buffer, &len, NULL, 0) != 0)
        ec.assign(errno, boost::system::generic_category());
    else
        path.assign(buffer, buffer + len);
}
Example #5
0
void GetExePath(int argc, char *argv[], boost::filesystem::path & path, boost::system::error_code & ec)
{
    std::vector<wchar_t> buffer(MAX_PATH);

    DWORD sz = ::GetModuleFileNameW(nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
    while (buffer.size() == sz) // if buffer size is not enough, increase by 2
    {
        buffer.resize(buffer.size() * 2);
        sz = ::GetModuleFileNameW(nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
    }

    if (!sz)
        ec.assign(::GetLastError(), boost::system::system_category());
    else
        path.assign(buffer.data(), buffer.data() + sz);

}
Example #6
0
void GetExePath(int argc, char *argv[], boost::filesystem::path & path, boost::system::error_code & ec)
{
    char buffer[PATH_MAX];
    uint32_t len = sizeof(buffer);

    if (_NSGetExecutablePath(buffer, &len) != 0)
    {
        ec.assign(errno, boost::system::system_category());
    }
    else
    {
        // resolve symlinks, ., .. if possible
        auto * canonicalPath = ::realpath(buffer, NULL);
        if (canonicalPath != NULL)
        {
            strncpy(buffer, canonicalPath, len);
            free(canonicalPath);
            path.assign(buffer, buffer + len);
        }
    }
}