magic numbers? (teach me, please)

geoff at ISM780B.UUCP geoff at ISM780B.UUCP
Thu Nov 28 03:55:00 AEST 1985


> Two questions. I presume they are related.
>      1)  How does the shell (exec?) know whether the command I just typed
>      is a shell script or one of several possible types of executable?
Magic numbers are indeed the answer, if file mode allows execution, the
first few bytes are read, (probably sizeof (struct filehdr)), and if they
contain a recognized magic number,  they are exec'd appropriately, else,
a shell script is assumed.  One drawback of this approach is that if an
executable has an unknown magic number (say you got it off a different
system), the shell will try to interpret it as a script, causing various
syntax errors (the most common is "unexpected `('" ).
>        2)  Presuming the answer to #1 above has something to do with
>        magic numbers, who issues them?  is there a common (definitive)
>        base of them or does each manufacturer/environment make up their
>        own set?
On Sys5 (at least), there is an ascii file called /etc/magic, which contains
magic numbers and information on what they mean, (the `file' command uses
this).  I _think_ the numbers are made up by the implementors of the
particular a.out, but are standardized as much as possible by enlightened
self-interest.  (And by AT&T, one would have to be crazy not to use
whatever they use to describe, say, a 5.0 binary).
For further info, see file(1), /etc/magic (on any sys5), the UNIX* System
5 Support Tools Guide (various sections), all the shell documentation,
and don't assume this is a complete list of references. Hope this helps.

			   Geoffrey Kimbrough
	  INTERACTIVE Systems Corporation, Santa Monica California.
	     {decvax!vortex || ihnp4!allegra!ima}!ism780!geoff

	  Don't hold your nose up so high, it blocks the light.
Standard Disclaimers apply.
* UNIX is a trademark of AT&T.
* Shell is a trademark of Shell Oil Company. (8^)



More information about the Comp.unix.wizards mailing list