William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | //
// SystemVersionCheck.m
// SystemVersionCheck
//
// Created by Chris Campbell on 12/03/2005.
// Copyright Big Nerd Ranch 2005. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#include <stdlib.h>
#include <unistd.h>
static void GetVersionComponentsFromString(int *majorPtr, int *minorPtr, int *bugfixPtr, NSString *versionString)
{
int major = 0;
int minor = 0;
int bugfix = 0;
NSArray *array = [versionString componentsSeparatedByString:@"."];
if ([array count] > 0) {
major = [[array objectAtIndex:0] intValue];
if ([array count] > 1) {
minor = [[array objectAtIndex:1] intValue];
if ([array count] > 2) {
bugfix = [[array objectAtIndex:2] intValue];
}
}
}
if (majorPtr != NULL) {
*majorPtr = major;
}
if (minorPtr != NULL) {
*minorPtr = minor;
}
if (bugfixPtr != NULL) {
*bugfixPtr = bugfix;
}
}
static BOOL CheckSystemVersion(NSString **requiredVersionPtr, BOOL *sameMajorMinorPtr)
{
// Determine the required version
NSString *requiredVersion = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"LSEnvironment"] objectForKey:@"MinimumSystemVersion"];
if ([requiredVersion length] == 0) {
requiredVersion = @"10.3.9";
}
if (requiredVersionPtr != NULL) {
*requiredVersionPtr = requiredVersion;
}
int requiredMajor, requiredMinor, requiredBugfix;
GetVersionComponentsFromString(&requiredMajor, &requiredMinor, &requiredBugfix, requiredVersion);
// Determine the system version
NSString *systemVersion = [[NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"] objectForKey:@"ProductVersion"];
if ([systemVersion length] == 0) {
// Can't parse the system version
if (sameMajorMinorPtr != NULL) {
*sameMajorMinorPtr = NO;
return NO;
}
}
int systemMajor, systemMinor, systemBugfix;
GetVersionComponentsFromString(&systemMajor, &systemMinor, &systemBugfix, systemVersion);
if (sameMajorMinorPtr != NULL) {
*sameMajorMinorPtr = (systemMajor == requiredMajor && systemMinor == requiredMinor);
}
if (systemMajor < requiredMajor) {
return NO;
} else if (systemMajor > requiredMajor) {
return YES;
}
// systemMajor == requiredMajor...
if (systemMinor < requiredMinor) {
return NO;
} else if (systemMinor > requiredMinor) {
return YES;
}
// systemMinor == requiredMinor...
if (systemBugfix < requiredBugfix) {
return NO;
}
// syhsemBugfix >= requiredBugfix...
return YES;
}
static NSString *LocalizedInfoStringForKey(NSString *key)
{
// First, look for an InfoPlist.strings entry
NSBundle *bundle = [NSBundle mainBundle];
NSString *value = [bundle localizedStringForKey:key value:nil table:@"InfoPlist"];
if (key != value) {
return value;
}
// Otherwise, look in the Info.plist file
return [[bundle infoDictionary] objectForKey:key];
}
static NSString *LocalizedApplicationName()
{
NSString *value = LocalizedInfoStringForKey(@"CFBundleDisplayName");
if (!value) {
value = LocalizedInfoStringForKey(@"CFBundleName");
}
return value;
}
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Grab the executable name from the Info.plist.
// If there isn't one, error out and exit
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
if (!executableName) {
fprintf(stderr, "ERROR: %s must be run from inside a .app wrapper\n", argv[0]);
return EXIT_FAILURE;
}
NSString *requiredVersion;
BOOL sameMajorMinor;
BOOL canLaunch = CheckSystemVersion(&requiredVersion, &sameMajorMinor);
if (!canLaunch) {
NSString *applicationName = LocalizedApplicationName();
// Use the name of the application as the alert window's title
NSString *windowTitle = applicationName;
if ([windowTitle length] == 0) {
windowTitle = @"";
}
if ([applicationName length] == 0) {
applicationName = @"This application";
}
NSString *title = [NSString stringWithFormat:@"%@ requires Mac OS X %@ or later", applicationName, requiredVersion];
NSString *messageFormat;
NSString *defaultButton;
NSString *alternateButton;
if (sameMajorMinor) {
messageFormat = @"Please use Software Update to upgrade to Mac OS X %@ or later.\n";
defaultButton = @"Open Software Update";
alternateButton = @"Quit";
} else {
messageFormat = @"Please install Mac OS X %@ or later.\n";
defaultButton = @"Quit";
alternateButton = nil;
}
// Load NSApplication and all the GUI stuff
[NSApplication sharedApplication];
NSPanel *alertPanel = NSGetAlertPanel(title, messageFormat, defaultButton, alternateButton, nil, requiredVersion);
[alertPanel setTitle:windowTitle];
int choice = [NSApp runModalForWindow:alertPanel];
NSReleaseAlertPanel(alertPanel);
alertPanel = nil;
if (sameMajorMinor && choice == NSAlertDefaultReturn) {
// Open Software Update
[[NSWorkspace sharedWorkspace] openFile:@"/System/Library/CoreServices/Software Update.app"];
}
return EXIT_SUCCESS;
}
// Get the path of the real executable
NSString *path = [[NSBundle mainBundle] pathForAuxiliaryExecutable:[NSString stringWithFormat:@"%@.real", executableName]];
if (!path) {
return EXIT_FAILURE;
}
// Construct a new argv array for the exec'ed process
NSMutableData *argvData = [[NSMutableData alloc] initWithBytes:argv length:(argc * sizeof(*argv))];
// Change the first argument to the path of the new executable
((const char **)[argvData mutableBytes])[0] = [path UTF8String];
// Append a NULL char* to the end of the array
char *nullPtr = NULL;
[argvData appendBytes:&nullPtr length:sizeof(nullPtr)];
[pool release];
pool = [[NSAutoreleasePool alloc] init];
execv(((const char **)[argvData bytes])[0], (char * const *)[argvData bytes]);
// This should never be reached
[argvData release];
argvData = nil;
[pool release];
pool = nil;
return EXIT_FAILURE;
}
|