forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetexepath.h
More file actions
85 lines (73 loc) · 1.98 KB
/
getexepath.h
File metadata and controls
85 lines (73 loc) · 1.98 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#ifndef GETEXEPATH_H
#define GETEXEPATH_H
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#elif defined(__FreeBSD__)
#include <string.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
// Returns the full path to the executable for the current process, resolving symbolic links.
// The caller is responsible for releasing the buffer. Returns null on error.
extern inline char* getexepath(void)
{
#if defined(__APPLE__)
uint32_t path_length = 0;
if (_NSGetExecutablePath(NULL, &path_length) != -1)
{
errno = EINVAL;
return NULL;
}
char path_buf[path_length];
if (_NSGetExecutablePath(path_buf, &path_length) != 0)
{
errno = EINVAL;
return NULL;
}
return realpath(path_buf, NULL);
#elif defined(__FreeBSD__)
static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
char path[PATH_MAX];
size_t len = sizeof(path);
if (sysctl(name, 4, path, &len, NULL, 0) != 0)
{
return NULL;
}
return strdup(path);
#elif defined(__sun)
const char* path = getexecname();
if (path == NULL)
{
return NULL;
}
return realpath(path, NULL);
#else
#if HAVE_GETAUXVAL && defined(AT_EXECFN)
const char* path = (const char *)getauxval(AT_EXECFN);
if (path)
{
return realpath(path, NULL);
}
#endif // HAVE_GETAUXVAL && defined(AT_EXECFN)
#ifdef __linux__
const char* symlinkEntrypointExecutable = "/proc/self/exe";
#else
const char* symlinkEntrypointExecutable = "/proc/curproc/exe";
#endif
// Resolve the symlink to the executable from /proc
return realpath(symlinkEntrypointExecutable, NULL);
#endif // defined(__APPLE__)
}
#ifdef __cplusplus
}
#endif // extern "C"
#endif // GETEXEPATH_H