Detecting type of file in a program

Guy Harris guy at auspex.UUCP
Sun Feb 5 07:41:41 AEST 1989


 >Anything you do will be pretty much what "file" does,
 >namely, inspect yay many bytes of the file to see if there is a
 >known "magic number" header present or if there are non-diplayable
 >byte values present.

Note, of course, that:

	1) the set of known magic numbers is not "constant" in any
	   sense, so you have to pick a set and hope it's sufficient (or
	   let "file" do it, if your system supports an S5-style "file",
	   and make sure the "/etc/magic" file is up-to-date and
	   complete);

	2) you should use "isprint" to determine what is a
	   "non-displayable byte value"; do NOT assume that any byte
	   with its 8th bit set is necessarily non-printable (actually,
	   if you have files with character codes longer than 1 byte,
	   e.g. files containing Kanji, it gets even more complicated).



More information about the Comp.unix.questions mailing list