Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!batcomputer!itsgw!steinmetz!uunet!auspex!guy From: guy@auspex.UUCP (Guy Harris) Newsgroups: comp.unix.questions Subject: Re: Detecting type of file in a program Message-ID: <1007@auspex.UUCP> Date: 12 Feb 89 23:18:25 GMT References: <192@henry.ece.utexas.edu> <9587@smoke.BRL.MIL> <949@auspex.UUCP> <2104@buengc.BU.EDU> Reply-To: guy@auspex.UUCP (Guy Harris) Organization: Auspex Systems, Santa Clara Lines: 40 >Just what are magic numbers for Some software subsystems mark files of some format that they know about with some particular number or set of characters, usually at the front of the file. The name, at least as I remember it being used in UNIX systems, originally referred to stuff stuck at the end of object and executable files that flagged the type of executable file. Back in the dark ages, I think an executable file began with a PDP-11 branch instruction that jumped around a header that specified the size of the code+data in the file; the instruction was a 16-bit number, value octal 407. When separate text&data, and split I&D, executables were added, "407" as the first 16 bits of the executable file was treated as a flag indicating that the image was a non-separate text&data image, and 410 was used for separate text&data and 411 (? - I haven't dealt with PDP-11s in ages...) meant separate I&D. (Also, the header was no longer part of the image itself, since 410 and 411 would branch further than 407 did and I guess they thought it was silly to pad the header to cope with this...). Anyway, the term "magic number" then applied to other flags stuck at the front of files by subsystems; e.g, 0177555 (word size) for a very old archive format, 0177545 (16 bits) for an older archive format, "!\n" as a string for the "modern" archive format (4.xBSD/S5R2 and later), etc. >and of what significance is the /etc/magic file. The S5 version of the "file" command (S5R2 and later, anyway) has a file "/etc/magic" that it can read to find out some of the magic numbers/strings it should look for; that way, you can extend its repertoire without having to hack the source code. SunOS 3.2 and later use a "file" based on this, with some extensions. >Every once in a while I get an error saying something about bad >magic numbers or whatnot, and I usually just punt. What's the scoop? That probably means you tried to link or execute a file that the linker or the "exec" call didn't recognize as an object or executable file, because it didn't have a proper magic number at the front of it; hence, "bad magic number".