|
6 | 6 | #if defined(HOST_OS_MACOS)
|
7 | 7 |
|
8 | 8 | #include "bin/platform.h"
|
| 9 | +#include "bin/platform_macos.h" |
9 | 10 |
|
10 | 11 | #include <CoreFoundation/CoreFoundation.h>
|
11 | 12 |
|
|
14 | 15 | #endif // !HOST_OS_IOS
|
15 | 16 | #include <errno.h> // NOLINT
|
16 | 17 | #include <mach-o/dyld.h>
|
17 |
| -#include <signal.h> // NOLINT |
18 |
| -#include <string.h> // NOLINT |
| 18 | +#include <signal.h> // NOLINT |
19 | 19 | #include <sys/resource.h> // NOLINT
|
20 |
| -#include <sys/sysctl.h> // NOLINT |
21 |
| -#include <sys/types.h> // NOLINT |
22 |
| -#include <sys/utsname.h> // NOLINT |
23 |
| -#include <unistd.h> // NOLINT |
| 20 | +#include <sys/sysctl.h> // NOLINT |
| 21 | +#include <sys/types.h> // NOLINT |
| 22 | +#include <sys/utsname.h> // NOLINT |
| 23 | +#include <unistd.h> // NOLINT |
24 | 24 |
|
25 | 25 | #include "bin/console.h"
|
26 | 26 | #include "bin/file.h"
|
@@ -114,7 +114,70 @@ const char* Platform::OperatingSystem() {
|
114 | 114 | #endif
|
115 | 115 | }
|
116 | 116 |
|
| 117 | +char* ExtractsOSVersionFromString(char* str) { |
| 118 | + char* pos = strstr(str, "<key>ProductVersion</key>"); |
| 119 | + if (pos == NULL) { |
| 120 | + return NULL; |
| 121 | + } |
| 122 | + pos = strstr(pos, "<string>"); |
| 123 | + if (pos == NULL) { |
| 124 | + return NULL; |
| 125 | + } |
| 126 | + // Shift the index by the length of "<string>". |
| 127 | + pos += 8; |
| 128 | + char* end_pos = strstr(pos, "</string>"); |
| 129 | + if (end_pos == NULL) { |
| 130 | + return NULL; |
| 131 | + } |
| 132 | + |
| 133 | + int length = end_pos - pos; |
| 134 | + char* result = |
| 135 | + reinterpret_cast<char*>(Dart_ScopeAllocate(length * sizeof(char)) + 1); |
| 136 | + strncpy(result, pos, length); |
| 137 | + result[length] = '\0'; |
| 138 | + return result; |
| 139 | +} |
| 140 | + |
| 141 | +static char* GetOSVersionFromPlist() { |
| 142 | + const char* path = "/System/Library/CoreServices/SystemVersion.plist"; |
| 143 | + File* file = File::Open(NULL, path, File::kRead); |
| 144 | + if (file == NULL) { |
| 145 | + return NULL; |
| 146 | + } |
| 147 | + int length = file->Length(); |
| 148 | + if (length < 0) { |
| 149 | + return NULL; |
| 150 | + } |
| 151 | + char* buffer = |
| 152 | + reinterpret_cast<char*>(Dart_ScopeAllocate(length * sizeof(char) + 1)); |
| 153 | + int bytes = file->ReadFully(buffer, length); |
| 154 | + buffer[length * sizeof(char)] = '\0'; |
| 155 | + file->Close(); |
| 156 | + file->Release(); |
| 157 | + if (bytes < 0) { |
| 158 | + return NULL; |
| 159 | + } |
| 160 | + return ExtractsOSVersionFromString(buffer); |
| 161 | +} |
| 162 | + |
117 | 163 | const char* Platform::OperatingSystemVersion() {
|
| 164 | + char str[64]; |
| 165 | + size_t size = sizeof(str); |
| 166 | + // This is only available to some versions later than 10.13.*. If it failed, |
| 167 | + // try to read from "SystemVersion.plist". |
| 168 | + int res = sysctlbyname("kern.osproductversion", str, &size, NULL, 0); |
| 169 | + if (res == 0) { |
| 170 | + int len = snprintf(NULL, 0, "%s", str); |
| 171 | + char* result_string = DartUtils::ScopedCString(len + 1); |
| 172 | + strncpy(result_string, str, len); |
| 173 | + result_string[len] = '\0'; |
| 174 | + return result_string; |
| 175 | + } |
| 176 | + char* result_string = GetOSVersionFromPlist(); |
| 177 | + if (result_string != NULL) { |
| 178 | + return result_string; |
| 179 | + } |
| 180 | + |
118 | 181 | struct utsname info;
|
119 | 182 | int ret = uname(&info);
|
120 | 183 | if (ret != 0) {
|
|
0 commit comments