OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
7 | 7 |
8 #include <mach-o/dyld.h> | 8 #include <mach-o/dyld.h> |
9 | 9 |
10 #include "bin/file.h" | |
10 #include "bin/platform.h" | 11 #include "bin/platform.h" |
11 | 12 |
12 #include <crt_externs.h> // NOLINT | 13 #include <crt_externs.h> // NOLINT |
13 #include <signal.h> // NOLINT | 14 #include <signal.h> // NOLINT |
14 #include <string.h> // NOLINT | 15 #include <string.h> // NOLINT |
15 #include <unistd.h> // NOLINT | 16 #include <unistd.h> // NOLINT |
16 | 17 |
17 #include "bin/fdutils.h" | 18 #include "bin/fdutils.h" |
18 | 19 |
19 | 20 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 char* path = NULL; | 85 char* path = NULL; |
85 if (_NSGetExecutablePath(path, &path_size) == 0) { | 86 if (_NSGetExecutablePath(path, &path_size) == 0) { |
86 return NULL; | 87 return NULL; |
87 } | 88 } |
88 // Allocate buffer and get executable path. | 89 // Allocate buffer and get executable path. |
89 path = reinterpret_cast<char*>(malloc(path_size)); | 90 path = reinterpret_cast<char*>(malloc(path_size)); |
90 if (_NSGetExecutablePath(path, &path_size) != 0) { | 91 if (_NSGetExecutablePath(path, &path_size) != 0) { |
91 free(path); | 92 free(path); |
92 return NULL; | 93 return NULL; |
93 } | 94 } |
95 // If the path is a symbolic link resolve that link. | |
96 File::Type type = File::GetType(path, false); | |
97 if (type == File::kIsLink) { | |
98 char* target_path = File::LinkTarget(path); | |
99 free(path); | |
100 return target_path; | |
101 } | |
102 // If the directory of the executable is a symbolic link resolve that link. | |
103 char* last_slash = strrchr(path, '/'); | |
104 if (last_slash != NULL && last_slash != path) { | |
105 // Split off the last slash and the executable. | |
106 *last_slash = '\0'; | |
107 File::Type type = File::GetType(path, false); | |
108 if (type == File::kIsLink) { | |
109 // Resolve the directory and append the executable. | |
110 char* target_path = File::LinkTarget(path); | |
111 *last_slash = '/'; | |
112 size_t new_path_len = strlen(target_path) + strlen(last_slash) + 1; | |
113 char* new_path = reinterpret_cast<char*>(malloc(new_path_len)); | |
114 snprintf(new_path, new_path_len, "%s%s", target_path, last_slash); | |
115 free(path); | |
116 free(target_path); | |
117 return new_path; | |
118 } else { | |
119 // Directory was not a symbolic link re-construct the path. | |
120 *last_slash = '/'; | |
121 } | |
kustermann
2015/06/08 08:22:25
So this only handles symlinks to the binary itself
Søren Gjesse
2015/06/09 10:46:48
Thanks Martin. This was a really crappy attempt to
| |
122 } | |
94 return path; | 123 return path; |
95 } | 124 } |
96 | 125 |
97 } // namespace bin | 126 } // namespace bin |
98 } // namespace dart | 127 } // namespace dart |
99 | 128 |
100 #endif // defined(TARGET_OS_MACOS) | 129 #endif // defined(TARGET_OS_MACOS) |
OLD | NEW |