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 #include <sys/types.h> | 9 #include <sys/types.h> |
10 #include <sys/sysctl.h> | 10 #include <sys/sysctl.h> |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 // approach needs to be devised. | 82 // approach needs to be devised. |
83 return NULL; | 83 return NULL; |
84 #else | 84 #else |
85 // Using environ directly is only safe as long as we do not | 85 // Using environ directly is only safe as long as we do not |
86 // provide access to modifying environment variables. | 86 // provide access to modifying environment variables. |
87 // On MacOS you have to do a bit of magic to get to the | 87 // On MacOS you have to do a bit of magic to get to the |
88 // environment strings. | 88 // environment strings. |
89 char** environ = *(_NSGetEnviron()); | 89 char** environ = *(_NSGetEnviron()); |
90 intptr_t i = 0; | 90 intptr_t i = 0; |
91 char** tmp = environ; | 91 char** tmp = environ; |
92 while (*(tmp++) != NULL) i++; | 92 while (*(tmp++) != NULL) { |
| 93 i++; |
| 94 } |
93 *count = i; | 95 *count = i; |
94 char** result = new char*[i]; | 96 char** result; |
| 97 result = reinterpret_cast<char**>(Dart_ScopeAllocate(i * sizeof(*result))); |
95 for (intptr_t current = 0; current < i; current++) { | 98 for (intptr_t current = 0; current < i; current++) { |
96 result[current] = environ[current]; | 99 result[current] = environ[current]; |
97 } | 100 } |
98 return result; | 101 return result; |
99 #endif | 102 #endif |
100 } | 103 } |
101 | 104 |
102 | 105 |
103 void Platform::FreeEnvironment(char** env, intptr_t count) { | 106 const char* Platform::ResolveExecutablePath() { |
104 delete[] env; | |
105 } | |
106 | |
107 | |
108 char* Platform::ResolveExecutablePath() { | |
109 // Get the required length of the buffer. | 107 // Get the required length of the buffer. |
110 uint32_t path_size = 0; | 108 uint32_t path_size = 0; |
111 char* path = NULL; | 109 if (_NSGetExecutablePath(NULL, &path_size) == 0) { |
112 if (_NSGetExecutablePath(path, &path_size) == 0) { | |
113 return NULL; | 110 return NULL; |
114 } | 111 } |
115 // Allocate buffer and get executable path. | 112 // Allocate buffer and get executable path. |
116 path = reinterpret_cast<char*>(malloc(path_size)); | 113 char* path = DartUtils::ScopedCString(path_size); |
117 if (_NSGetExecutablePath(path, &path_size) != 0) { | 114 if (_NSGetExecutablePath(path, &path_size) != 0) { |
118 free(path); | |
119 return NULL; | 115 return NULL; |
120 } | 116 } |
121 // Return the canonical path as the returned path might contain symlinks. | 117 // Return the canonical path as the returned path might contain symlinks. |
122 char* canon_path = File::GetCanonicalPath(path); | 118 const char* canon_path = File::GetCanonicalPath(path); |
123 free(path); | |
124 return canon_path; | 119 return canon_path; |
125 } | 120 } |
126 | 121 |
| 122 |
127 void Platform::Exit(int exit_code) { | 123 void Platform::Exit(int exit_code) { |
128 exit(exit_code); | 124 exit(exit_code); |
129 } | 125 } |
130 | 126 |
131 } // namespace bin | 127 } // namespace bin |
132 } // namespace dart | 128 } // namespace dart |
133 | 129 |
134 #endif // defined(TARGET_OS_MACOS) | 130 #endif // defined(TARGET_OS_MACOS) |
OLD | NEW |