File: execname.cc

package info (click to toggle)
performous 0.7.0%2Bgit20140715-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,900 kB
  • ctags: 3,470
  • sloc: cpp: 16,647; sh: 2,495; ansic: 2,015; python: 431; xml: 407; objc: 245; makefile: 12
file content (56 lines) | stat: -rw-r--r-- 1,257 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "execname.hh"

#if defined(_WIN32)
#include <windows.h>
fs::path execname() {
	char buf[1024];
	DWORD ret = GetModuleFileName(NULL, buf, sizeof(buf));
	if (ret == 0 || ret == sizeof(buf)) return std::string();
	return buf;
}
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
fs::path execname() {
	char buf[1024];
	uint32_t size = sizeof(buf);
	int ret = _NSGetExecutablePath(buf, &size);
	if (ret != 0) return fs::path();
	return buf;
}
#elif defined(sun) || defined(__sun)
#include <stdlib.h>
fs::path execname() {
	return getexecname();
}
#elif defined(__FreeBSD__)
#include <sys/sysctl.h>
fs::path execname() {
	int mib[4];
	mib[0] = CTL_KERN;
	mib[1] = KERN_PROC;
	mib[2] = KERN_PROC_PATHNAME;
	mib[3] = -1;
	char buf[1024];
	size_t maxchars = sizeof(buf) - 1;
	size_t size = maxchars;
	sysctl(mib, 4, buf, &size, NULL, 0);
	if (size == 0 || size >= maxchars) return fs::path();
	buf[size] = '\0';
	return buf;
}
#elif defined(__linux__)
#include <unistd.h>
fs::path execname() {
	char buf[1024];
	ssize_t maxchars = sizeof(buf) - 1;
	ssize_t size = readlink("/proc/self/exe", buf, sizeof(buf));
	if (size <= 0 || size >= maxchars) return fs::path();
	buf[size] = '\0';
	return buf;
}
#else
fs::path execname() {
	return fs::path();
}
#endif