/
UKSystemInfo.m
344 lines (291 loc) · 10.3 KB
/
UKSystemInfo.m
1
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//
// UKSystemInfo.m
// UKSystemInfo
//
// Created by Uli Kusterer on 23.09.04.
// Copyright 2004 M. Uli Kusterer. Uli Kusterer
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
#import "UKSystemInfo.h"
#include <Carbon/Carbon.h>
#include <sys/types.h>
#include <sys/sysctl.h>
unsigned UKPhysicalRAMSize()
{
long ramSize;
if( Gestalt( gestaltPhysicalRAMSizeInMegabytes, &ramSize ) == noErr )
return ramSize;
else
return 0;
}
NSString* UKSystemVersionString()
{
long vMajor = 10, vMinor = 0, vBugfix = 0;
UKGetSystemVersionComponents( &vMajor, &vMinor, &vBugfix );
return [NSString stringWithFormat: @"%ld.%ld.%ld", vMajor, vMinor, vBugfix];
}
void UKGetSystemVersionComponents( long* outMajor, long* outMinor, long* outBugfix )
{
long sysVersion = UKSystemVersion();
if( sysVersion >= MAC_OS_X_VERSION_10_4 )
{
Gestalt( gestaltSystemVersionMajor, outMajor );
Gestalt( gestaltSystemVersionMinor, outMinor );
Gestalt( gestaltSystemVersionBugFix, outBugfix );
}
else
{
*outMajor = ((sysVersion & 0x0000F000) >> 12) * 10 + ((sysVersion & 0x00000F00) >> 8);
*outMinor = (sysVersion & 0x000000F0) >> 4;
*outBugfix = sysVersion & 0x0000000F;
}
}
long UKSystemVersion()
{
long sysVersion;
if( Gestalt( gestaltSystemVersion, &sysVersion ) != noErr )
return 0;
return sysVersion;
}
unsigned UKClockSpeed()
{
long speed;
if( Gestalt( gestaltProcClkSpeed, &speed ) == noErr )
return speed / 1000000;
else
return 0;
}
unsigned UKCountCores()
{
unsigned count = 0;
size_t size = sizeof(count);
if( sysctlbyname( "hw.ncpu", &count, &size, NULL, 0 ) )
return 1;
return count;
}
NSString* UKMachineName()
{
static NSString* cpuName = nil;
if( cpuName )
return cpuName;
char* machineName = NULL;
if( Gestalt( gestaltUserVisibleMachineName, (long*) &machineName ) == noErr )
{
NSString* internalName = [[[NSString alloc] initWithBytes: machineName +1 length: machineName[0] encoding: NSMacOSRomanStringEncoding] autorelease];
static NSDictionary* translationDictionary = nil;
if( !translationDictionary )
translationDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
@"PowerMac 8500/8600",@"AAPL,8500",
@"PowerMac 9500/9600",@"AAPL,9500",
@"PowerMac 7200",@"AAPL,7200",
@"PowerMac 7200/7300",@"AAPL,7300",
@"PowerMac 7500",@"AAPL,7500",
@"Apple Network Server",@"AAPL,ShinerESB",
@"Alchemy(Performa 6400 logic-board design)",@"AAPL,e407",
@"Gazelle(5500)",@"AAPL,e411",
@"PowerBook 3400",@"AAPL,3400/2400",
@"PowerBook 3500",@"AAPL,3500",
@"PowerMac G3 (Gossamer)",@"AAPL,Gossamer",
@"PowerMac G3 (Silk)",@"AAPL,PowerMac G3",
@"PowerBook G3 (Wallstreet)",@"AAPL,PowerBook1998",
@"Yikes! Old machine",@"AAPL", // generic.
@"iMac (first generation)",@"iMac,1",
@"iMac",@"iMac", // generic.
@"PowerBook G3 (Lombard)",@"PowerBook1,1",
@"iBook (clamshell)",@"PowerBook2,1",
@"iBook FireWire (clamshell)",@"PowerBook2,2",
@"PowerBook G3 (Pismo)",@"PowerBook3,1",
@"PowerBook G4 (Titanium)",@"PowerBook3,2",
@"PowerBook G4 (Titanium w/ Gigabit Ethernet)",@"PowerBook3,3",
@"PowerBook G4 (Titanium w/ DVI)",@"PowerBook3,4",
@"PowerBook G4 (Titanium 1GHZ)",@"PowerBook3,5",
@"iBook (12in May 2001)",@"PowerBook4,1",
@"iBook (May 2002)",@"PowerBook4,2",
@"iBook 2 rev. 2 (w/ or w/o 14in LCD) (Nov 2002)",@"PowerBook4,3",
@"iBook 2 (w/ or w/o 14in LDC)",@"PowerBook4,4",
@"PowerBook G4 (Aluminum 17in)",@"PowerBook5,1",
@"PowerBook G4 (Aluminum 15in)",@"PowerBook5,2",
@"PowerBook G4 (Aluminum 17in rev. 2)",@"PowerBook5,3",
@"PowerBook G4 (Aluminum 12in)",@"PowerBook6,1",
@"PowerBook G4 (Aluminum 12in)",@"PowerBook6,2",
@"iBook G4",@"PowerBook6,3",
@"PowerBook or iBook",@"PowerBook", // generic.
@"Blue & White G3",@"PowerMac1,1",
@"PowerMac G4 PCI Graphics",@"PowerMac1,2",
@"iMac FireWire (CRT)",@"PowerMac2,1",
@"iMac FireWire (CRT)",@"PowerMac2,2",
@"PowerMac G4 AGP Graphics",@"PowerMac3,1",
@"PowerMac G4 AGP Graphics",@"PowerMac3,2",
@"PowerMac G4 AGP Graphics",@"PowerMac3,3",
@"PowerMac G4 (QuickSilver)",@"PowerMac3,4",
@"PowerMac G4 (QuickSilver)",@"PowerMac3,5",
@"PowerMac G4 (MDD/Windtunnel)",@"PowerMac3,6",
@"iMac (Flower Power)",@"PowerMac4,1",
@"iMac (Flat Panel 15in)",@"PowerMac4,2",
@"eMac",@"PowerMac4,4",
@"iMac (Flat Panel 17in)",@"PowerMac4,5",
@"PowerMac G4 Cube",@"PowerMac5,1",
@"PowerMac G4 Cube",@"PowerMac5,2",
@"iMac (Flat Panel 17in)",@"PowerMac6,1",
@"PowerMac G5",@"PowerMac7,2",
@"PowerMac G5",@"PowerMac7,3",
@"PowerMac",@"PowerMac", // generic.
@"XServe",@"RackMac1,1",
@"XServe rev. 2",@"RackMac1,2",
@"XServe G5",@"RackMac3,1",
@"XServe",@"RackMac",
@"Mac Mini Core",@"Macmini1,1", // Core Duo?
@"Mac Mini",@"Macmini", // generic
@"Mac Pro",@"MacPro", // generic
@"MacBook Pro Core 2",@"MacBookPro4,1",
@"MacBook Pro",@"MacBookPro", // generic
nil];
NSRange r;
NSString* aKey;
NSString* foundKey = nil;
NSString* humanReadableName = nil;
// Find the corresponding entry in the NSDictionary
// Keys should be sorted to distinguish 'generic' from 'specific' names.
// So we can overwrite generic names with the more specific ones as we
// progress through the list.
NSEnumerator *e=[[[translationDictionary allKeys]
sortedArrayUsingSelector:@selector(compare:)]
objectEnumerator];
while( aKey = [e nextObject] )
{
r = [internalName rangeOfString: aKey];
if( r.location != NSNotFound )
{
if( humanReadableName == nil || [foundKey length] != [internalName length] ) // We didn't have an exact match yet?
{
humanReadableName = [translationDictionary objectForKey:aKey];
foundKey = aKey;
}
}
}
// If it was a generic name, include the ugly name so we can add it to the list:
if( [foundKey rangeOfString: @","].location == NSNotFound )
humanReadableName = [[NSString stringWithFormat: @"%@ (%@)", humanReadableName, foundKey] retain];
// If nothing was found, at least show the ugly name so we have some hint:
if( humanReadableName == nil )
cpuName = [[NSString stringWithFormat: @"Unknown (%@)", internalName] retain];
else
cpuName = humanReadableName;
}
return cpuName;
}
NSString* UKCPUName()
{
return UKAutoreleasedCPUName( NO );
}
NSString* UKAutoreleasedCPUName( BOOL releaseIt )
{
long cpu;
static NSString* cpuName = nil;
if( Gestalt( gestaltNativeCPUtype, &cpu ) == noErr )
{
if( !cpuName )
{
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Motorola 68000", [NSNumber numberWithLong: gestaltCPU68000],
@"Motorola 68010", [NSNumber numberWithLong: gestaltCPU68010],
@"Motorola 68020", [NSNumber numberWithLong: gestaltCPU68020],
@"Motorola 68030", [NSNumber numberWithLong: gestaltCPU68030],
@"Motorola 68040", [NSNumber numberWithLong: gestaltCPU68040],
@"PowerPC 601", [NSNumber numberWithLong: gestaltCPU601],
@"PowerPC 603", [NSNumber numberWithLong: gestaltCPU603],
@"PowerPC 604", [NSNumber numberWithLong: gestaltCPU604],
@"PowerPC 603e", [NSNumber numberWithLong: gestaltCPU603e],
@"PowerPC 603ev", [NSNumber numberWithLong: gestaltCPU603ev],
@"PowerPC G3", [NSNumber numberWithLong: gestaltCPU750],
@"PowerPC 604e", [NSNumber numberWithLong: gestaltCPU604e],
@"PowerPC 604ev", [NSNumber numberWithLong: gestaltCPU604ev],
@"PowerPC G4", [NSNumber numberWithLong: gestaltCPUG4],
@"PowerPC G4", [NSNumber numberWithLong: gestaltCPUG47450],
nil
];
cpuName = [dict objectForKey: [NSNumber numberWithLong: cpu]];
}
if( cpuName == nil )
{
char cpuCStr[5] = { 0 };
memmove( cpuCStr, &cpu, 4 );
if( (cpu & 0xff000000) >= 0x20000000 && (cpu & 0x00ff0000) >= 0x00200000
&& (cpu & 0x0000ff00) >= 0x00002000 && (cpu & 0x000000ff) >= 0x00000020) // All valid as characters?
cpuName = [NSString stringWithFormat: @"Unknown (%d/%s)", cpu, &cpu];
else
cpuName = [NSString stringWithFormat: @"Unknown (%d)", cpu, &cpu];
}
[cpuName retain]; // Yeah, I know, I'm paranoid.
}
if( releaseIt )
{
NSString* cn = cpuName;
[cpuName autorelease];
cpuName = nil;
return cn;
}
return cpuName;
}
/*NSString* UKSystemSerialNumber()
{
mach_port_t masterPort;
kern_return_t kr = noErr;
io_registry_entry_t entry;
CFTypeRef prop;
CFTypeID propID;
NSString* str = nil;
kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
if( kr != noErr )
goto cleanup;
entry = IORegistryGetRootEntry( masterPort );
if( entry == MACH_PORT_NULL )
goto cleanup;
prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, CFSTR("serial-number"), nil, kIORegistryIterateRecursively);
if( prop == nil )
goto cleanup;
propID = CFGetTypeID( prop );
if( propID != CFDataGetTypeID() )
goto cleanup;
const char* buf = [(NSData*)prop bytes];
int len = [(NSData*)prop length],
x;
char secondPart[256];
char firstPart[256];
char* currStr = secondPart;
int y = 0;
for( x = 0; x < len; x++ )
{
if( buf[x] > 0 && (y < 255) )
currStr[y++] = buf[x];
else if( currStr == secondPart )
{
currStr[y] = 0; // Terminate string.
currStr = firstPart;
y = 0;
}
}
currStr[y] = 0; // Terminate string.
str = [NSString stringWithFormat: @"%s%s", firstPart, secondPart];
cleanup:
mach_port_deallocate( mach_task_self(), masterPort );
return str;
}*/