views:

255

answers:

3

I need to get it as a string to use elsewhere in the program, I'm not worried about compiler settings.

I found HowToGetHardwareAndNetworkInfo on CocoaDev, but it seemed a little intense when all I wanted to know is PPC vs. Intel.

+6  A: 

If your application is built fat (i.e. isn't running under rosetta on intel), you don't need to make any calls to get this information, because different code will be running, depending on which architecture you're on. Thus, the information is available at compile time:

#if defined __i386__ || defined __x86_64__
NSString *processorType = @"Intel";
#elif defined __ppc__ || defined __ppc64__
NSString *processorType = @"PPC";
#elif defined __arm__
NSString *processorType = @"ARM";
#else
NSString *processorType = @"Unknown Architecture";
#endif

If you really want to do the determination at runtime for some perverse reason, you should be able to use the sysctlbyname function, defined in <sys/sysctl.h>.

Stephen Canon
Depending on the purpose, you may find the `__LITTLE_ENDIAN__` and `__BIG_ENDIAN__` macros more useful (especially if you intend to also support the iPhone, which is an ARM platform).
Peter Hosey
Given that the questioner wanted the values as strings, I assumed they weren't intending to use them to determine endianness. I modified the example to include ARM, just in case.
Stephen Canon
I would prefer to figure out at runtime. It's nothing perverse, I just need to write it to a file config file. (For reasons are stupid and out of my control.)
zekel
Stephen's answer gives it to you at runtime. Do that, then write `processorType` into your config file.
Ken
With what I gave you the answer is still *available* at runtime; it just avoids needing to do the *decision* at runtime.
Stephen Canon
A: 

The only part of that mess which you actually care about is here:

host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);

See the Mach headers in Kernel.framework for struct and constant definitions.

Azeem.Butt
zekel
That's something you might want to read up on before writing software in C.
Azeem.Butt
A: 

How about uname?

struct utsname uts;
uname(&uts);
printf("%s\n", uts.machine);

Will print like PPC or i386 or x86_64 depending on the machine.

Dave DeLong
That won't compile? "Storage size of 'uts' isn't known"
zekel
#include <sys/utsname.h> (http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man3/uname.3.html)
Quinn Taylor