0

In Cocoa, how would I test if a file is an executable binary? Unfortunately, [NSFileManager isExecutableFileAtPath:] will also return true for scripts and directories, pretty much any file that has the executable bit set which is not what I want.

While doing it in straight-up Cocoa is my preferred approach, a Bash solution that I can easily wrap in an NSTask would be sufficient.

2 Answers 2

1

Directories you can filter out easily in code, but knowing what is a binary and what is not is a little hard because, effectively, the only way is to open the file and read it, which is something you need to do yourself.

The main problem, however, is what should be considered a binary.

I have seen executable files that had a dozen text lines in the beginning (so, effectively they were scripts) but then the rest was binary. How would you classify them?

If you are ok to classify them according to how they are loaded, you can try the command file that will try to tell you as precisely as possible what a file is.

Sign up to request clarification or add additional context in comments.

2 Comments

We're not doing anything fancy when we compile the binaries we're testing for in these directory structures. We're looking for anything compiled from C or Objective-C using gcc or clang.
added suggestion to use file (a command line tool) to get the type. It should report Mach-O executables as such.
1

I don't know Cocoa, but this is a bash solution:

find ../ -type f -perm +111 | \
  xargs -n 1 -I {} file "{}" | grep -v text | cut -d: -f1

3 Comments

find: -executable: unknown primary or operator
Remember, this is OS X, not Linux.
find: -perm: /111: illegal mode string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.