Mercurial > code
changeset 607:f8616f9fc9d5
Misc: merge executable_path into one function
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 29 Jun 2017 09:20:24 +0200 |
parents | ab758b6bfdc7 |
children | 85fa9c6241ec |
files | misc/executable.cpp |
diffstat | 1 files changed, 58 insertions(+), 128 deletions(-) [+] |
line wrap: on
line diff
--- a/misc/executable.cpp Tue Dec 27 13:39:42 2016 +0100 +++ b/misc/executable.cpp Thu Jun 29 09:20:24 2017 +0200 @@ -2,157 +2,95 @@ #include <stdexcept> #if defined(__linux__) -# include <unistd.h> +# include <unistd.h> -# include <cerrno> -# include <cstring> -# include <stdexcept> +# include <cerrno> +# include <climits> +# include <cstring> #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) -# if defined(__NetBSD__) -# include <sys/param.h> -# else -# include <sys/types.h> -# endif - -# if defined(__OpenBSD__) -# include <unistd.h> -# endif +# if defined(__NetBSD__) +# include <sys/param.h> +# else +# include <sys/types.h> +# endif -# include <sys/sysctl.h> +# if defined(__OpenBSD__) +# include <unistd.h> +# endif + +# include <sys/sysctl.h> -# include <array> -# include <cerrno> -# include <climits> -# include <cstddef> -# include <cstdlib> -# include <cstring> -# include <stdexcept> +# include <cerrno> +# include <climits> +# include <cstddef> +# include <cstdlib> +# include <cstring> #elif defined(__APPLE__) -# include <cerrno> -# include <cstring> -# include <libproc.h> -# include <unistd.h> +# include <cerrno> +# include <cstring> +# include <libproc.h> +# include <unistd.h> #elif defined(_WIN32) -# include <Windows.h> +# include <Windows.h> #endif -#if defined(__linux__) - -std::string executable() +std::string executable_path() { std::string result; - result.resize(2048, '\0'); +#if defined(__linux__) + char path[PATH_MAX + 1] = {0}; - auto size = readlink("/proc/self/exe", &result[0], 2048); - - if (size < 0) + if (readlink("/proc/self/exe", path, sizeof (path) - 1) < 0) throw std::runtime_error(std::strerror(errno)); - result.resize(size, '\0'); - - return result; -} - + result = path; #elif defined(__FreeBSD__) || defined(__DragonFly__) + int size = PATH_MAX, mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + char path[PATH_MAX + 1] = {0}; -std::string executable() -{ - std::array<int, 4> mib{ CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; - std::string result; - std::size_t size = PATH_MAX + 1; - - result.resize(size, '\0'); - - if (sysctl(mib.data(), 4, &result[0], &size, nullptr, 0) < 0) + if (sysctl(mib, 4, path, &size, nullptr, 0) < 0) throw std::runtime_error(std::strerror(errno)); - result.resize(size, '\0'); - - return result; -} - + result = path; #elif defined(__APPLE__) + char path[PROC_PIDPATHINFO_MAXSIZE + 1] = {0}; -std::string executable() -{ - std::string result; - std::size_t size = PROC_PIDPATHINFO_MAXSIZE; - - result.resize(size, '\0'); - - if ((size = proc_pidpath(getpid(), &result[0], size)) == 0) + if ((proc_pidpath(getpid(), path, sizeof (path) - 1) == 0) throw std::runtime_error(std::strerror(errno)); - result.resize(size, '\0'); - - return result; -} - + result = path; #elif defined(_WIN32) + char path[PATH_MAX + 1] = {0}; -std::string executable() -{ - std::string result; - std::size_t size = PATH_MAX; - - result.resize(size, '\0'); - - if (!(size = GetModuleFileNameA(nullptr, &result[0], size))) + if (GetModuleFileNameA(nullptr, path, sizeof (path) - 1) == 0) throw std::runtime_error("GetModuleFileName error"); - result.resize(size, '\0'); - - return result; -} - + result = path; #elif defined(__NetBSD__) - -std::string executable() -{ - std::string result; + char path[4096 + 1] = {0}; -#if defined(KERN_PROC_PATHNAME) - std::array<int, 4> mib{ CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME }; - std::size_t size = MAXPATHLEN; - - result.resize(size, '\0'); - - if (sysctl(mib.data(), 4, &result[0], &size, nullptr, 0) < 0) - throw std::runtime_error(std::strerror(errno)); - - result.resize(size, '\0'); -#else - result.resize(2048, '\0'); - - auto size = readlink("/proc/curproc/exe", &result[0], 2048); +# if defined(KERN_PROC_PATHNAME) + int mib[] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME }; + int size = sizeof (path) - 1; - if (size < 0) - throw std::runtime_error(std::strerror(errno)); - - result.resize(size, '\0'); -#endif - - return result; -} - -#elif defined(__OpenBSD__) + if (sysctl(mib, 4, path, &size, nullptr, 0) < 0) + throw std::runtime_error(std::strerror(errno)); +# else + if (readlink("/proc/curproc/exe", path, sizeof (path) - 1) < 0) + throw std::runtime_error(std::strerror(errno)); +# endif -std::string executable() -{ - char **paths, *path; - std::string result; - std::size_t len; - std::array<int, 4> mib{ CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV }; + result = path; +#elif defined(__OpenBSD__) + char **paths, path[PATH_MAX + 1] = {0}; + int length, mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV }; - if (sysctl(mib.data(), 4, nullptr, &len, nullptr, 0) == -1) - throw std::runtime_error(std::strerror(errno)); - if ((paths = static_cast<char **>(malloc(len))) == nullptr) + if (sysctl(mib, 4, nullptr, &length, nullptr, 0) < 0) throw std::runtime_error(std::strerror(errno)); - - sysctl(mib.data(), 4, paths, &len, nullptr, 0); - - if ((path = static_cast<char *>(std::malloc(PATH_MAX + 1))) == nullptr) { + if ((paths = static_cast<char**>(std::malloc(length))) == nullptr) + throw std::runtime_error(std::strerror(errno)); + if (sysctl(mib, 4, paths, &length, nullptr, 0) < 0) { std::free(paths); throw std::runtime_error(std::strerror(errno)); } @@ -162,16 +100,8 @@ std::free(paths); std::free(path); - - return result; -} - #else - -std::string executable() -{ // Not supported. return ""; +#endif } - -#endif