File: programming/cocoa/

//  UKSystemInfo.m
//  UKSystemInfo
//  Created by Uli Kusterer on 23.09.04.
//  Copyright 2004 M. Uli Kusterer. All rights reserved.
#import "UKSystemInfo.h"
#include <Carbon/Carbon.h>
#include <sys/types.h>
#include <sys/sysctl.h>
"%ld.%ld.%ld""hw.ncpu""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",
					@"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",
					@"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 rev. 2",@"RackMac1,2",
					@"XServe G5",@"RackMac3,1",
					@"Mac Mini",@"Macmini1,1",	// Core Duo?
					@"Mac Mini",@"Macmini",		// generic
// 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.
// We didn't have an exact match yet?
// If it was a generic name, include the ugly name so we can add it to the list:
",""%@ (%@)", humanReadableName, foundKey] retain];
		// If nothing was found, at least show the ugly name so we have some hint:
"Unknown (%@)""Motorola 68000""Motorola 68010""Motorola 68020""Motorola 68030""Motorola 68040""PowerPC 601""PowerPC 603""PowerPC 604""PowerPC 603e""PowerPC 603ev""PowerPC G3""PowerPC 604e""PowerPC 604ev""PowerPC G4""PowerPC G4"// All valid as characters?
"Unknown (%d/%s)""Unknown (%d)", cpu, &cpu];
		[cpuName retain];		// Yeah, I know, I'm paranoid.
/*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],
	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];
	mach_port_deallocate( mach_task_self(), masterPort );
	return str;

This code uses the PclZip Zip File reading code, which is subject to the GNU LGPL. It also uses the GeSHi syntax highlighter, subject to the GPL. Ask if you want this for your own web site, it's free.