2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/util/current-executable.h"
21 #include <mach-o/dyld.h>
25 #include <sys/sysctl.h>
28 #include <folly/portability/Unistd.h>
32 std::string
current_executable_path() {
34 char result
[PATH_MAX
];
35 ssize_t count
= readlink("/proc/self/exe", result
, PATH_MAX
);
36 return std::string(result
, (count
> 0) ? count
: 0);
37 #elif defined(__APPLE__)
38 char result
[PATH_MAX
];
39 uint32_t size
= sizeof(result
);
40 uint32_t success
= _NSGetExecutablePath(result
, &size
);
41 return std::string(success
== 0 ? result
: "");
42 #elif defined(__FreeBSD__)
43 char result
[PATH_MAX
];
44 size_t size
= sizeof(result
);
49 mib
[2] = KERN_PROC_PATHNAME
;
52 if (sysctl(mib
, 4, result
, &size
, nullptr, 0) < 0) {
55 return std::string(result
, (size
> 0) ? size
: 0);
57 // XXX: How do you do this on your platform?
62 std::string
current_executable_directory() {
63 #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
64 std::string path
= current_executable_path();
65 return path
.substr(0, path
.find_last_of("/"));
67 // XXX: How do you do this on your platform?