Re: Any better way to determine if file is executable?
Re: Any better way to determine if file is executable?
- Subject: Re: Any better way to determine if file is executable?
- From: Douglas Davidson <email@hidden>
- Date: Tue, 30 Oct 2007 12:10:41 -0700
On Oct 30, 2007, at 5:04 AM, Devon Ferns wrote:
I've searched the list archives and no one seems to have a good way
to do this.
As discussed before, NSFileManager's isExecutableAtPath: returns YES
for directories and any other file with the execute bit set.
This isn't what I want so I've had to fudge together a solution.
Basically, I'm using NSTask and executing /usr/bin/file on all
suspected executable files and parsing the output for Mach-O.
BTW, can anyone tell me what the program "file" will output for CFM
binaries? I don't know of any that I can test it on to see.
/usr/bin/file may not be the best match for your needs. It's designed
to be highly flexible, to handle files of many different types, and to
produce detailed human-readable output; it's not really intended to be
highly efficient or to produce output that can be readily parsed. As
it happens, executable files generally can be identified (for sniffing
purposes) simply by reading their first four bytes. For Mach-o files,
the possibilities are FAT_MAGIC, FAT_CIGAM, MH_MAGIC, MH_CIGAM,
MH_MAGIC_64, and MH_CIGAM_64. Of course, this doesn't rule out the
possibility of encountering a file that starts with one of these bytes
but isn't actually formatted as a Mach-o executable, but then neither
does using /usr/bin/file.
You might want to think about what you consider to be an executable.
Does an executable shell script count? How about a Java jar-file? Do
you care about executables from other platforms? (By the way, file
reports PowerPC PEF/CFM executables as "header for PowerPC PEF
executable", and their first four bytes are 'Joy!', but be sure to
take endianness into account.)
Douglas Davidson
_______________________________________________
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