• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Any better way to determine if file is executable?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Any better way to determine if file is executable?


  • Subject: Re: Any better way to determine if file is executable?
  • From: Devon Ferns <email@hidden>
  • Date: Tue, 30 Oct 2007 15:32:32 -0400

Thanks Doug and Shawn. I had considered reading the file but was hoping for some framework function might already do something like what I need.
For this assignment I suppose it's "easy" enough to use file because it's not really a big deal and I have many many other things to do at the moment.
For the assignment it doesn't matter the language or how I accomplish the end goal so for now, I think I might stick with /usr/bin/file.


Although it doesn't seem like too much work to read 4 bytes from a file, I'll probably look at it anyways. It doesn't matter to me the endianness at the moment because I'm running this on my MBP and I just have to demonstrate that it works.

Now that I know what to look for(FAT_MAGIC, FAT_CIGAM, etc.) I came across this example from Apple. I'll post if for the sake of any people in the future searching the list archives. Really, it's almost exactly what I was looking to do.
http://developer.apple.com/samplecode/CheckExecutableArchitecture/listing1.html


Although, it's C, not Objective-C, I'm sure it's fairly easy to do something like that in Objective-C.

Devon.


On 30-Oct-07, at 3:10 PM, Douglas Davidson wrote:


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


References: 
 >Any better way to determine if file is executable? (From: Devon Ferns <email@hidden>)
 >Re: Any better way to determine if file is executable? (From: Douglas Davidson <email@hidden>)

  • Prev by Date: Re: FSCatalogInfo randomness
  • Next by Date: Re: "report a problem" dialog box
  • Previous by thread: Re: Any better way to determine if file is executable?
  • Next by thread: Re: Any better way to determine if file is executable?
  • Index(es):
    • Date
    • Thread