[Scottish] type of files

Graeme Mathieson scottish at mailman.lug.org.uk
Wed Aug 28 16:22:01 2002


On Wed, Aug 28, 2002 at 03:58:02PM +0100, Huard, Elise - D C&W Consultant wrote:
> 
> may be slightly off-topic, but not completely : how do you access the type
> of a file in a C program (under Linux/Unix) ?
> using the path, i want to be able to determine whether it is a shell, an
> executable ...
> What i found looking in my C book and browsing through man pages : the
> f_flags of filehdr should give some indication, apparently.  Unfortunately i
> don't know how to get to this filehdr, and i studied every stdio function i
> could think of.

The only mention I can find of filehdr in my /usr/include is
<linux/coff.h> which has a struct COFF_filehdr that will probably give
you more details of a COFF file (I suspect will be of little use to you).

> There's always the rather inelegant call to 'system()' using 'file' command,
> but there must be a nicer way.

file(1) works by looking for magic numbers in the head of a file.  For
example, if it notes that the first 4 bytes of a file are '\177ELF', it
will decide that it's an ELF-format executable.

You could go with calling file, emulating the same functionality[1] or
cheating and hoping you can guess based on the combination of the
extension and whether the executable bits are set.

[1] If you're writing GPL code, there's no harm in directly nicking the
    code from file. :-)
-- 
mathie+sig@wossname.org.uk          "If you ever need anything please don't
http://www.wossname.org.uk/~mathie/  hesitate to ask someone else first.
                                     I'm too busy acting like I'm not naive.
                                     I've seen it all.  I was here first."
                                         -- Cobain, "Very Ape"