|
| 1 | +/* ===-- os_version_check.c - OS version checking -------------------------=== |
| 2 | + * |
| 3 | + * The LLVM Compiler Infrastructure |
| 4 | + * |
| 5 | + * This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | + * Source Licenses. See LICENSE.TXT for details. |
| 7 | + * |
| 8 | + * ===----------------------------------------------------------------------=== |
| 9 | + * |
| 10 | + * This file implements the function __isOSVersionAtLeast, used by |
| 11 | + * Objective-C's @available |
| 12 | + * |
| 13 | + * ===----------------------------------------------------------------------=== |
| 14 | + */ |
| 15 | + |
| 16 | +#ifdef __APPLE__ |
| 17 | + |
| 18 | +#include <CoreFoundation/CoreFoundation.h> |
| 19 | +#include <Dispatch/Dispatch.h> |
| 20 | +#include <TargetConditionals.h> |
| 21 | +#include <stdint.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +/* These three variables hold the host's OS version. */ |
| 27 | +static int32_t GlobalMajor, GlobalMinor, GlobalSubminor; |
| 28 | +static dispatch_once_t DispatchOnceCounter; |
| 29 | + |
| 30 | +/* Find and parse the SystemVersion.plist file. */ |
| 31 | +static void parseSystemVersionPList(void *Unused) { |
| 32 | + (void)Unused; |
| 33 | + |
| 34 | + char *PListPath = "/System/Library/CoreServices/SystemVersion.plist"; |
| 35 | + |
| 36 | +#if TARGET_OS_SIMULATOR |
| 37 | + char *PListPathPrefix = getenv("IPHONE_SIMULATOR_ROOT"); |
| 38 | + if (!PListPathPrefix) |
| 39 | + return; |
| 40 | + char FullPath[strlen(PListPathPrefix) + strlen(PListPath) + 1]; |
| 41 | + strcpy(FullPath, PListPathPrefix); |
| 42 | + strcat(FullPath, PListPath); |
| 43 | + PListPath = FullPath; |
| 44 | +#endif |
| 45 | + FILE *PropertyList = fopen(PListPath, "r"); |
| 46 | + if (!PropertyList) |
| 47 | + return; |
| 48 | + |
| 49 | + /* Dynamically allocated stuff. */ |
| 50 | + CFDictionaryRef PListRef = NULL; |
| 51 | + CFDataRef FileContentsRef = NULL; |
| 52 | + UInt8 *PListBuf = NULL; |
| 53 | + |
| 54 | + fseek(PropertyList, 0, SEEK_END); |
| 55 | + long PListFileSize = ftell(PropertyList); |
| 56 | + if (PListFileSize < 0) |
| 57 | + goto Fail; |
| 58 | + rewind(PropertyList); |
| 59 | + |
| 60 | + PListBuf = malloc((size_t)PListFileSize); |
| 61 | + if (!PListBuf) |
| 62 | + goto Fail; |
| 63 | + |
| 64 | + size_t NumRead = fread(PListBuf, 1, (size_t)PListFileSize, PropertyList); |
| 65 | + if (NumRead != (size_t)PListFileSize) |
| 66 | + goto Fail; |
| 67 | + |
| 68 | + /* Get the file buffer into CF's format. We pass in a null allocator here * |
| 69 | + * because we free PListBuf ourselves */ |
| 70 | + FileContentsRef = CFDataCreateWithBytesNoCopy( |
| 71 | + NULL, PListBuf, (CFIndex)NumRead, kCFAllocatorNull); |
| 72 | + if (!FileContentsRef) |
| 73 | + goto Fail; |
| 74 | + |
| 75 | + if (&CFPropertyListCreateWithData) |
| 76 | + PListRef = CFPropertyListCreateWithData( |
| 77 | + NULL, FileContentsRef, kCFPropertyListImmutable, NULL, NULL); |
| 78 | + else |
| 79 | + PListRef = CFPropertyListCreateFromXMLData(NULL, FileContentsRef, |
| 80 | + kCFPropertyListImmutable, NULL); |
| 81 | + if (!PListRef) |
| 82 | + goto Fail; |
| 83 | + |
| 84 | + CFTypeRef OpaqueValue = |
| 85 | + CFDictionaryGetValue(PListRef, CFSTR("ProductVersion")); |
| 86 | + if (!OpaqueValue || CFGetTypeID(OpaqueValue) != CFStringGetTypeID()) |
| 87 | + goto Fail; |
| 88 | + |
| 89 | + char VersionStr[32]; |
| 90 | + if (!CFStringGetCString((CFStringRef)OpaqueValue, VersionStr, |
| 91 | + sizeof(VersionStr), kCFStringEncodingUTF8)) |
| 92 | + goto Fail; |
| 93 | + sscanf(VersionStr, "%d.%d.%d", &GlobalMajor, &GlobalMinor, &GlobalSubminor); |
| 94 | + |
| 95 | +Fail: |
| 96 | + if (PListRef) |
| 97 | + CFRelease(PListRef); |
| 98 | + if (FileContentsRef) |
| 99 | + CFRelease(FileContentsRef); |
| 100 | + free(PListBuf); |
| 101 | + fclose(PropertyList); |
| 102 | +} |
| 103 | + |
| 104 | +int32_t __isOSVersionAtLeast(int32_t Major, int32_t Minor, int32_t Subminor) { |
| 105 | + /* Populate the global version variables, if they haven't already. */ |
| 106 | + dispatch_once_f(&DispatchOnceCounter, NULL, parseSystemVersionPList); |
| 107 | + |
| 108 | + if (Major < GlobalMajor) return 1; |
| 109 | + if (Major > GlobalMajor) return 0; |
| 110 | + if (Minor < GlobalMinor) return 1; |
| 111 | + if (Minor > GlobalMinor) return 0; |
| 112 | + return Subminor <= GlobalSubminor; |
| 113 | +} |
| 114 | + |
| 115 | +#endif |
0 commit comments