Unix binary/text files: is there a difference?

Marcus J. Ranum mjr at hussar.dco.dec.com
Thu Mar 21 07:55:27 AEST 1991


jdubb at bucsf.bu.edu (jay dubb) writes:

>    I've looked in a bunch of C and Unix books, and can't seem to find
>a good explanation of this - maybe someone can help... Is there a way
>to tell (from a C program) whether a given file contains text or data?

	Lots of applications use a "magic number" scheme, whereby the
first long int (just for example) in a file is some value, or some
string. That's why we are stuck with stuff like:
%%!PS-Adobe
	at the beginning of PostScript files, and so forth. It's a
tricky problem. Other systems (not to name names) make arbitrary
decisions based on naming conventions. If it ends in ".FOR" it is a
FORTRAN program, etc.

	The short answer to your question is, "no."

	Whenever I have to tackle this kind of problem, I usually write
a magic number in network byte order (see the man page on htonl()) at
some known offset into the file, and scream loudly if it's not there.
You can get arbitrarily insane trying to make sure that the file isn't
one that someone accidentally picked out of a hat with that value.

mjr.
-- 
             The world is just backing store for virtual reality.



More information about the Comp.unix.programmer mailing list