Re: determine whether an ancillary program/task can run
Re: determine whether an ancillary program/task can run
- Subject: Re: determine whether an ancillary program/task can run
- From: Ken Thomases <email@hidden>
- Date: Thu, 01 Sep 2011 22:13:16 -0500
On Sep 1, 2011, at 6:42 PM, Rainer Brockerhoff wrote:
> On 01/09/2011, at 20:17, email@hidden wrote:
>> From: Martin Wierschin <email@hidden>
>> Date: 1 de setembro de 2011 19:11:24 BRT
>> To: Dave DeLong <email@hidden>
>> Cc: Cocoa Dev List <email@hidden>
>>
>> Offhand does anyone know how to inspect the architecture(s) of a plain executable file? I've been googling for a little bit and haven't hit upon anything that works yet.
>
> Sure, that's easy to do. Define a C function like this:
>
> #include <mach-o/fat.h>
> char* FindMachoHeader(char* input, cpu_type_t cpu) {
> struct fat_header* header = (struct fat_header*)input;
> if (OSSwapBigToHostInt32(header->magic)==FAT_MAGIC) {
> struct fat_arch* arch = (struct fat_arch*)(input+sizeof(struct fat_header));
> for (NSUInteger i=0;i<OSSwapBigToHostInt32(header->nfat_arch);i++) {
> if (OSSwapBigToHostInt32(arch[i].cputype)==cpu) {
> return input+OSSwapBigToHostInt32(arch[i].offset);
> }
> }
> } else if (OSSwapBigToHostInt32(((struct fat_arch*)input)->cputype)==cpu) {
> return input;
> }
> return NULL;
> }
>
> Then get the executable file with something like:
> NSData* executable = [NSData dataWithContentsOfMappedFile:pathToExecutableFile];
> and call the function like
> if (FindMachoHeader([executable bytes],CPU_TYPE_X86)) { // or CPU_TYPE_POWERPC if applicable
> // if it returns non-null that architecture is present
> }
You might be interested in the functions listed in the arch(3) man page.
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/arch.3.html
Alternatively, you might just resort to manually fork()ing and exec()ing, in which case you can handle the error yourself. Or check out posix_spawn() and friends. In general, it is preferred to make the attempt and handle failure rather than trying to figure out beforehand if you'd succeed or fail (and NSTask ought to be better at allowing this).
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden