Update hhvm version.h
[hiphop-php.git] / hphp / util / current-executable.cpp
blobf1cb7f4a12a369d2eedead334fb6e48fc6e266db
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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"
18 #include <limits.h>
20 #ifdef __APPLE__
21 #include <mach-o/dyld.h>
22 #endif
24 #ifdef __FreeBSD__
25 #include <sys/sysctl.h>
26 #endif
28 #include <folly/portability/Unistd.h>
30 namespace HPHP {
32 std::string current_executable_path() {
33 #ifdef __linux__
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);
45 int mib[4];
47 mib[0] = CTL_KERN;
48 mib[1] = KERN_PROC;
49 mib[2] = KERN_PROC_PATHNAME;
50 mib[3] = -1;
52 if (sysctl(mib, 4, result, &size, nullptr, 0) < 0) {
53 return std::string();
55 return std::string(result, (size > 0) ? size : 0);
56 #else
57 // XXX: How do you do this on your platform?
58 return std::string();
59 #endif
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("/"));
66 #else
67 // XXX: How do you do this on your platform?
68 return std::string();
69 #endif