@@ -165,6 +165,66 @@ bool check_timedout(std::chrono::high_resolution_clock::time_point old, double t
165165 return false ;
166166}
167167
168+ std::string get_full_exe_path ()
169+ {
170+ // https://github.com/gpakosz/whereami/blob/master/src/whereami.c
171+ // https://stackoverflow.com/q/1023306
172+
173+ static std::string exe_path{};
174+ static std::recursive_mutex mtx{};
175+
176+ if (!exe_path.empty ()) {
177+ return exe_path;
178+ }
179+
180+ std::lock_guard lock (mtx);
181+ // check again in case we didn't win this thread arbitration
182+ if (!exe_path.empty ()) {
183+ return exe_path;
184+ }
185+
186+ #if defined(__WINDOWS__)
187+ static wchar_t path[8192 ]{};
188+ auto ret = ::GetModuleFileNameW (nullptr , path, _countof (path));
189+ if (ret >= _countof (path) || 0 == ret) {
190+ path[0 ] = ' .' ;
191+ path[1 ] = 0 ;
192+ }
193+ exe_path = canonical_path (utf8_encode (path));
194+ #else
195+ // https://man7.org/linux/man-pages/man5/proc.5.html
196+ // https://linux.die.net/man/5/proc
197+ // https://man7.org/linux/man-pages/man2/readlink.2.html
198+ // https://linux.die.net/man/3/readlink
199+ static char path[8192 ]{};
200+ auto read = ::readlink (" /proc/self/exe" , path, sizeof (path) - 1 );
201+ if (-1 == read) {
202+ path[0 ] = ' .' ;
203+ read = 1 ;
204+ }
205+ path[read] = 0 ;
206+ exe_path = canonical_path (path);
207+ #endif // __WINDOWS__
208+
209+ return exe_path;
210+ }
211+
212+ std::string get_exe_dirname ()
213+ {
214+ std::string env_exe_dir = get_env_variable (" GseExeDir" );
215+ if (!env_exe_dir.empty ()) {
216+ if (env_exe_dir.back () != PATH_SEPARATOR[0 ]) {
217+ env_exe_dir = env_exe_dir.append (PATH_SEPARATOR);
218+ }
219+
220+ return env_exe_dir;
221+ }
222+
223+ std::string full_exe_path = get_full_exe_path ();
224+ return full_exe_path.substr (0 , full_exe_path.rfind (PATH_SEPARATOR)).append (PATH_SEPARATOR);
225+
226+ }
227+
168228#ifdef __LINUX__
169229std::string get_lib_path ()
170230{
@@ -692,4 +752,3 @@ void set_whitelist_ips(uint32_t *from, uint32_t *to, unsigned num_ips)
692752}
693753
694754#endif // EMU_EXPERIMENTAL_BUILD
695-
0 commit comments