简体   繁体   中英

Determine whether a file is a binary executable file or a script file using gcc in Linux

In Linux both a binary executable file and a script can be marked "executable". I would like to determine in my gcc program whether the file is a script or a binary executable.

I read that there is an a.out.h file which allows to analyse the header of the file but I do not know how to use this in my code. Or if there is a simple solution instead.

You can check so-called magic bytes. For elf 1st four bytes are supposed to be 7f 45 4c 46 in hex. You have to care for byte order though.

Opening file in binary mode and reading 1st four bytes should suffice.

Eg

shell$ hexdump -n 10 ./ni6_ga
0000000 457f 464c 0101 0301 0000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM