]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright 2014 Renato Tegon Forti, Antony Polukhin. |
1e59de90 | 2 | // Copyright Antony Polukhin, 2015-2022. |
7c673cae FG |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. | |
5 | // (See accompanying file LICENSE_1_0.txt | |
6 | // or copy at http://www.boost.org/LICENSE_1_0.txt) | |
7 | ||
8 | #ifndef BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP | |
9 | #define BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP | |
10 | ||
92f5a8d4 | 11 | #include <boost/dll/config.hpp> |
7c673cae | 12 | #include <boost/dll/detail/system_error.hpp> |
7c673cae FG |
13 | #include <boost/predef/os.h> |
14 | ||
15 | #ifdef BOOST_HAS_PRAGMA_ONCE | |
16 | # pragma once | |
17 | #endif | |
18 | ||
19 | #if BOOST_OS_MACOS || BOOST_OS_IOS | |
20 | ||
21 | #include <mach-o/dyld.h> | |
22 | ||
23 | namespace boost { namespace dll { namespace detail { | |
92f5a8d4 | 24 | inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) { |
7c673cae FG |
25 | ec.clear(); |
26 | ||
27 | char path[1024]; | |
28 | uint32_t size = sizeof(path); | |
29 | if (_NSGetExecutablePath(path, &size) == 0) | |
92f5a8d4 TL |
30 | return boost::dll::fs::path(path); |
31 | ||
7c673cae FG |
32 | char *p = new char[size]; |
33 | if (_NSGetExecutablePath(p, &size) != 0) { | |
92f5a8d4 TL |
34 | ec = boost::dll::fs::make_error_code( |
35 | boost::dll::fs::errc::bad_file_descriptor | |
7c673cae FG |
36 | ); |
37 | } | |
38 | ||
92f5a8d4 | 39 | boost::dll::fs::path ret(p); |
7c673cae FG |
40 | delete[] p; |
41 | return ret; | |
42 | } | |
43 | }}} // namespace boost::dll::detail | |
44 | ||
45 | #elif BOOST_OS_SOLARIS | |
46 | ||
47 | #include <stdlib.h> | |
48 | namespace boost { namespace dll { namespace detail { | |
92f5a8d4 | 49 | inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code& ec) { |
7c673cae FG |
50 | ec.clear(); |
51 | ||
92f5a8d4 | 52 | return boost::dll::fs::path(getexecname()); |
7c673cae FG |
53 | } |
54 | }}} // namespace boost::dll::detail | |
55 | ||
56 | #elif BOOST_OS_BSD_FREE | |
57 | ||
58 | #include <sys/types.h> | |
59 | #include <sys/sysctl.h> | |
60 | #include <stdlib.h> | |
61 | ||
62 | namespace boost { namespace dll { namespace detail { | |
92f5a8d4 | 63 | inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code& ec) { |
7c673cae FG |
64 | ec.clear(); |
65 | ||
66 | int mib[4]; | |
67 | mib[0] = CTL_KERN; | |
68 | mib[1] = KERN_PROC; | |
69 | mib[2] = KERN_PROC_PATHNAME; | |
70 | mib[3] = -1; | |
71 | char buf[10240]; | |
72 | size_t cb = sizeof(buf); | |
73 | sysctl(mib, 4, buf, &cb, NULL, 0); | |
74 | ||
92f5a8d4 | 75 | return boost::dll::fs::path(buf); |
7c673cae FG |
76 | } |
77 | }}} // namespace boost::dll::detail | |
78 | ||
79 | ||
80 | ||
81 | #elif BOOST_OS_BSD_NET | |
82 | ||
7c673cae | 83 | namespace boost { namespace dll { namespace detail { |
92f5a8d4 TL |
84 | inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) { |
85 | return boost::dll::fs::read_symlink("/proc/curproc/exe", ec); | |
7c673cae FG |
86 | } |
87 | }}} // namespace boost::dll::detail | |
88 | ||
89 | #elif BOOST_OS_BSD_DRAGONFLY | |
90 | ||
92f5a8d4 | 91 | |
7c673cae | 92 | namespace boost { namespace dll { namespace detail { |
92f5a8d4 TL |
93 | inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) { |
94 | return boost::dll::fs::read_symlink("/proc/curproc/file", ec); | |
7c673cae FG |
95 | } |
96 | }}} // namespace boost::dll::detail | |
97 | ||
98 | #elif BOOST_OS_QNX | |
99 | ||
100 | #include <fstream> | |
101 | #include <string> // for std::getline | |
102 | namespace boost { namespace dll { namespace detail { | |
92f5a8d4 | 103 | inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) { |
7c673cae FG |
104 | ec.clear(); |
105 | ||
106 | std::string s; | |
107 | std::ifstream ifs("/proc/self/exefile"); | |
108 | std::getline(ifs, s); | |
109 | ||
110 | if (ifs.fail() || s.empty()) { | |
92f5a8d4 TL |
111 | ec = boost::dll::fs::make_error_code( |
112 | boost::dll::fs::errc::bad_file_descriptor | |
7c673cae FG |
113 | ); |
114 | } | |
115 | ||
92f5a8d4 | 116 | return boost::dll::fs::path(s); |
7c673cae FG |
117 | } |
118 | }}} // namespace boost::dll::detail | |
119 | ||
120 | #else // BOOST_OS_LINUX || BOOST_OS_UNIX || BOOST_OS_HPUX || BOOST_OS_ANDROID | |
121 | ||
7c673cae | 122 | namespace boost { namespace dll { namespace detail { |
92f5a8d4 | 123 | inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) { |
7c673cae FG |
124 | // We can not use |
125 | // boost::dll::detail::path_from_handle(dlopen(NULL, RTLD_LAZY | RTLD_LOCAL), ignore); | |
126 | // because such code returns empty path. | |
127 | ||
92f5a8d4 | 128 | return boost::dll::fs::read_symlink("/proc/self/exe", ec); // Linux specific |
7c673cae FG |
129 | } |
130 | }}} // namespace boost::dll::detail | |
131 | ||
132 | #endif | |
133 | ||
134 | #endif // BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP | |
135 |