3 .ds ;G \\*(;G\\f\\$1\\$3\\f\\$2
4 .if !
\a\\$4
\a\a .Af \\$2 \\$1 "\\$4" "\\$5" "\\$6" "\\$7" "\\$8" "\\$9"
7 .ie
\a\\$3
\a\a .ft \\$1
11 .Af "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7" "\\$8" "\\$9"
16 .aF 5 \\n(.f "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7"
19 .aF 5 1 "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7"
22 .aF 1 5 "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6" "\\$7"
24 .de EX \" start example
41 magic \- magic file interface
51 Magic_t* magicopen(unsigned long \fIflags\fP);
52 void magicclose(Magic_t* \fImagic\fP);
54 int magicload(Magic_t* \fImagic\fP, const char* \fIpath\fP, unsigned long \fIflags\fP);
55 int magiclist(Magic_t* \fImagic\fP, Sfio_t* \fIsp\fP);
57 char* magictype(Magic_t* \fImagic\fP, const char* \fIpath\fP, struct stat* \fIst\fP);
60 These routines provide an interface to the
64 returns a magic session handle that is passed to all of the other routines.
69 Return the MIME type string rather than the magic file description.
72 Don't follow symbolic links.
87 Enable verbose error messages.
90 closes the magic session.
93 loads the magic file named by
95 into the magic session.
99 More than one magic file can be loaded into a session;
100 the files are searched in load order.
105 then the default magic file is loaded.
108 lists the magic file contents on the
112 This is used for debugging magic entries.
115 returns the type string for
127 on a private stat buffer,
134 is assumed to contain valid stat information, otherwise
141 always returns a non-null string.
142 If errors are encounterd on
144 then the return value will contain information on those errors, e.g.,
147 The magic file format is a backwards compatible extension of an
148 ancient System V file implementation.
149 However, with the extended format it is possible to write a single
150 magic file that works on all platforms.
151 Most of the net magic files floating around work with
153 but they usually double up on
157 entries that are automatically handled by
160 A magic file entry describes a procedure for determining a single file type
161 based on the file pathname,
163 information, and the file data.
164 An entry is a sequence of lines, each line being a record of
167 The general record format is:
169 [op]offset type [mask]expression description [mimetype]
172 in the first column introduces a comment.
173 The first record in an entry contains no
175 the remaining records for an entry contain an
177 Integer constants are as in C:
183 for octal and decimal otherwise.
190 The previous records must match but the current record is optional.
192 is an old-style synonym for
196 The previous and current records must match.
199 Starts a nesting block that is terminated by
201 A nesting block pushes a new context for the
210 records have no other fields.
213 A function declaration and call for the single character identifier
215 The function return is a nesting block end record
217 Function may be redefined.
218 Functions have no arguments or return value.
221 A call to the function
226 field is either the offset into the data upon which the current entry operates
227 or a file metadata identifier.
228 Offsets are either integer constants or offset expressions.
229 An offset expression is contained in (...) and is a combination of
230 integral arithmetic operators and the
232 indirection operator.
233 Indirections take the form
235 where integer is the data offset for the indirection value.
236 The size of the indirection value is taken either from one of the suffixes
237 .LR B (byte, 1 char),
238 .LR H (short, 2 chars),
239 .LR L (long, 4 chars),
241 .LR Q (quead, 8 chars),
245 Valid file metadata identifiers are:
248 The string representation of
255 The string representation of
259 The string representation of
263 The string representation of
270 canonical representation (i.e., the good old octal values).
273 The string representation of
283 The file path name sans directory.
286 The string representation of
291 field specifies the type of the data at
293 Integral types may be prefixed by
297 for specifying exact little-endian or big-endian representation,
298 but the internal algorithm automatically loops through the
299 standard representations to find integral matches,
300 so representation prefixes are rarely used.
301 However, this looping may cause some magic entry conflicts; use the
305 prefix in these cases.
306 Only one representation is used for all the records in an entry.
320 Tests on this type may fail is the local compiler does not support
321 an 8 byte integral type and the corresponding value overflows 4 bytes.
326 is interpreted as a 4 byte seconds-since-the-epoch date and
327 converted to a string.
334 style substitution expression
335 \fIdel old del new del \fP [ \fI flags \fP ]
336 where the substituted value is made available to the
347 that converts the substituted value to lower case and
349 that converts the substituted value to upper case.
352 does not match the string data at
354 then the entry record fails.
360 pattern that is matched against the string data at
366 field is a string that is compared with the string data at
377 with the integral value at
383 The contents of the expression field depends on the
385 String type expression are described in the
389 means any value and applies to all types.
392 expression take the form [\fIoperator\fP] \fIoperand\P where
394 is compared with the data value at
411 may be an integral constant or one of the following builtin function calls:
414 A recursive call to the magic algorithm starting with the data at
417 \f5loop(\fIfunction\fP,\fIoffset\fP,\fIincrement\fP)\fR
426 after each iteration.
427 Iteration continues until the description text does not change.
431 field is the most important because it is this field that is presented
432 to the outside world.
433 When constructing description
434 fields one must be very careful to follow the style layed out in the
435 magic file, lest yet another layer of inconsistency creep into the system.
436 The description for each matching record in an entry are concatenated
437 to form the complete magic type.
438 If the previous matching description in the current entry does not end with
440 and the current description is not empty and does not start with
447 is placed between the descriptions
448 (most optional descriptions start with
452 can be referenced in the description using
454 for the string types and
458 for the integral types.
462 field specifies the MIME type, usually in the form
470 0 long 0x020c0108 hp s200 executable, pure
472 +36 long >0 , not stripped
473 +4 short >0 , version %ld
476 0 long 0x020c0107 hp s200 executable
479 0 long 0x020c010b hp s200 executable, demand-load
485 determines if the executable is stripped and also extracts the version number.
487 0 long 0407 bsd 386 executable
489 +16 long >0 , not stripped
491 This entry requires that the file also has execute permission.
493 file(1), mime(4), tw(1), modecanon(3)