Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExecutablePath.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string>
6 
7 #include <boost/filesystem/operations.hpp>
8 #include <boost/filesystem/path.hpp>
9 #include <boost/predef.h>
10 
11 namespace inexor {
12 namespace filesystem {
13 
14 std::string ExecutablePathWithoutBinary(const char *argv0);
15 std::string ExecutablePath(const char *argv0);
16 std::string ExecutablePathFallback(const char *argv0);
17 
18 #if (BOOST_OS_CYGWIN || BOOST_OS_WINDOWS)
19 
20 #include <Windows.h>
21 
22 std::string ExecutablePath(const char *argv0)
23 {
24  char buf[1024] = {0};
25  DWORD ret = GetModuleFileNameA(NULL, buf, sizeof(buf));
26  if (ret == 0 || ret == sizeof(buf))
27  {
28  return ExecutablePathFallback(argv0);
29  }
30  return buf;
31 }
32 
33 #elif (BOOST_OS_MACOS)
34 
35 #include <mach-o/dyld.h>
36 
37 std::string ExecutablePath(const char *argv0)
38 {
39  char buf[1024] = {0};
40  uint32_t size = sizeof(buf);
41  int ret = _NSGetExecutablePath(buf, &size);
42  if (0 != ret)
43  {
44  return ExecutablePathFallback(argv0);
45  }
46  boost::system::error_code ec;
48  boost::filesystem::canonical(buf, boost::filesystem::current_path(), ec));
49  return p.make_preferred().string();
50 }
51 
52 #elif (BOOST_OS_SOLARIS)
53 
54 #include <stdlib.h>
55 
56 std::string ExecutablePath(const char *argv0)
57 {
58  std::string ret = getexecname();
59  if (ret.empty())
60  {
61  return ExecutablePathFallback(argv0);
62  }
64  if (!p.has_root_directory())
65  {
66  boost::system::error_code ec;
67  p = boost::filesystem::canonical(
68  p, boost::filesystem::current_path(), ec);
69  ret = p.make_preferred().string();
70  }
71  return ret;
72 }
73 
74 #elif (BOOST_OS_BSD)
75 
76 #include <sys/sysctl.h>
77 
78 std::string ExecutablePath(const char *argv0)
79 {
80  int mib[4] = {0};
81  mib[0] = CTL_KERN;
82  mib[1] = KERN_PROC;
83  mib[2] = KERN_PROC_PATHNAME;
84  mib[3] = -1;
85  char buf[1024] = {0};
86  size_t size = sizeof(buf);
87  sysctl(mib, 4, buf, &size, NULL, 0);
88  if (size == 0 || size == sizeof(buf))
89  {
90  return ExecutablePathFallback(argv0);
91  }
92  std::string path(buf, size);
93  boost::system::error_code ec;
95  boost::filesystem::canonical(
96  path, boost::filesystem::current_path(), ec));
97  return p.make_preferred().string();
98 }
99 
100 #elif (BOOST_OS_LINUX)
101 
102 #include <unistd.h>
103 
104 std::string ExecutablePath(const char *argv0)
105 {
106  char buf[1024] = {0};
107  ssize_t size = readlink("/proc/self/exe", buf, sizeof(buf));
108  if (size == 0 || size == sizeof(buf))
109  {
110  return ExecutablePathFallback(argv0);
111  }
112  std::string path(buf, size);
113  boost::system::error_code ec;
115  boost::filesystem::canonical(
116  path, boost::filesystem::current_path(), ec));
117  return p.make_preferred().string();
118 }
119 
120 #else
121 
122 std::string ExecutablePath(const char *argv0)
123 {
124  return ExecutablePathFallback(argv0);
125 }
126 
127 #endif
128 
130 {
132  return p.parent_path().make_preferred().string();
133 }
134 
136 {
137  if (argv0 == nullptr || argv0[0] == 0)
138  {
139  return "";
140  }
141  boost::system::error_code ec;
143  boost::filesystem::canonical(
144  argv0, boost::filesystem::current_path(), ec));
145  return p.make_preferred().string();
146 }
147 
148 }
149 }
150 
std::string ExecutablePath(const char *argv0)
Definition: ExecutablePath.hpp:122
std::string ExecutablePathFallback(const char *argv0)
Definition: ExecutablePath.hpp:135
#define NULL
Definition: cube_types.hpp:35
char * path(char *s)
Modifies the input string to only contain slashes in the direction the platform allows.
Definition: stream.cpp:63
GLuint GLuint GLintptr GLsizeiptr size
Definition: glexts.hpp:412
char string[MAXSTRLEN]
cube-strings (char arrays of fixed size).
Definition: cube_types.hpp:18
std::string ExecutablePathWithoutBinary(const char *argv0)
Definition: ExecutablePath.hpp:129