Add file 4.12.
[dragonfly.git] / contrib / file-4.12 / doc / file.man
1 .TH FILE __CSECTION__ "Copyright but distributable"
2 .\" $Id: file.man,v 1.54 2003/10/27 18:09:08 christos Exp $
3 .SH NAME
4 file
5 \- determine file type
6 .SH SYNOPSIS
7 .B file
8 [
9 .B \-bcikLnNprsvz
10 ]
11 [
12 .B \-f
13 .I namefile
14 ]
15 [
16 .B \-F
17 .I separator
18 ]
19 [
20 .B \-m 
21 .I magicfiles
22 ]
23 .I file
24 \&...
25 .br
26 .B file
27 .B -C
28 [
29 .B \-m 
30 magicfile ]
31 .SH DESCRIPTION
32 This manual page documents version __VERSION__ of the
33 .B file
34 command.
35 .PP
36 .B File
37 tests each argument in an attempt to classify it.
38 There are three sets of tests, performed in this order:
39 filesystem tests, magic number tests, and language tests.
40 The
41 .I first
42 test that succeeds causes the file type to be printed.
43 .PP
44 The type printed will usually contain one of the words
45 .B text
46 (the file contains only
47 printing characters and a few common control
48 characters and is probably safe to read on an
49 .SM ASCII
50 terminal),
51 .B executable
52 (the file contains the result of compiling a program
53 in a form understandable to some \s-1UNIX\s0 kernel or another),
54 or
55 .B data
56 meaning anything else (data is usually `binary' or non-printable).
57 Exceptions are well-known file formats (core files, tar archives)
58 that are known to contain binary data.
59 When modifying the file
60 .I __MAGIC__
61 or the program itself, 
62 .B "preserve these keywords" .
63 People depend on knowing that all the readable files in a directory
64 have the word ``text'' printed.
65 Don't do as Berkeley did and change ``shell commands text''
66 to ``shell script''.
67 Note that the file
68 .I __MAGIC__
69 is built mechanically from a large number of small files in
70 the subdirectory
71 .I Magdir
72 in the source distribution of this program.
73 .PP
74 The filesystem tests are based on examining the return from a
75 .BR stat (2)
76 system call.
77 The program checks to see if the file is empty,
78 or if it's some sort of special file.
79 Any known file types appropriate to the system you are running on
80 (sockets, symbolic links, or named pipes (FIFOs) on those systems that
81 implement them)
82 are intuited if they are defined in
83 the system header file
84 .IR <sys/stat.h>  .
85 .PP
86 The magic number tests are used to check for files with data in
87 particular fixed formats.
88 The canonical example of this is a binary executable (compiled program)
89 .I a.out
90 file, whose format is defined in 
91 .I a.out.h
92 and possibly
93 .I exec.h
94 in the standard include directory.
95 These files have a `magic number' stored in a particular place
96 near the beginning of the file that tells the \s-1UNIX\s0 operating system
97 that the file is a binary executable, and which of several types thereof.
98 The concept of `magic number' has been applied by extension to data files.
99 Any file with some invariant identifier at a small fixed
100 offset into the file can usually be described in this way.
101 The information identifying these files is read from the compiled
102 magic file
103 .I __MAGIC__.mgc ,
104 or 
105 .I __MAGIC__
106 if the compile file does not exist.
107 .PP
108 If a file does not match any of the entries in the magic file,
109 it is examined to see if it seems to be a text file.
110 ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets
111 (such as those used on Macintosh and IBM PC systems),
112 UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC
113 character sets can be distinguished by the different
114 ranges and sequences of bytes that constitute printable text
115 in each set.
116 If a file passes any of these tests, its character set is reported.
117 ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified
118 as ``text'' because they will be mostly readable on nearly any terminal;
119 UTF-16 and EBCDIC are only ``character data'' because, while
120 they contain text, it is text that will require translation
121 before it can be read.
122 In addition,
123 .B file
124 will attempt to determine other characteristics of text-type files.
125 If the lines of a file are terminated by CR, CRLF, or NEL, instead
126 of the Unix-standard LF, this will be reported.
127 Files that contain embedded escape sequences or overstriking
128 will also be identified.
129 .PP
130 Once
131 .B file
132 has determined the character set used in a text-type file,
133 it will
134 attempt to determine in what language the file is written.
135 The language tests look for particular strings (cf
136 .IR names.h )
137 that can appear anywhere in the first few blocks of a file.
138 For example, the keyword
139 .B .br
140 indicates that the file is most likely a
141 .BR troff (1)
142 input file, just as the keyword 
143 .B struct
144 indicates a C program.
145 These tests are less reliable than the previous
146 two groups, so they are performed last.
147 The language test routines also test for some miscellany
148 (such as 
149 .BR tar (1)
150 archives).
151 .PP
152 Any file that cannot be identified as having been written
153 in any of the character sets listed above is simply said to be ``data''.
154 .SH OPTIONS
155 .TP 8
156 .B "\-b, \-\-brief"
157 Do not prepend filenames to output lines (brief mode).
158 .TP 8
159 .B "\-c, \-\-checking\-printout"
160 Cause a checking printout of the parsed form of the magic file.
161 This is usually used in conjunction with 
162 .B \-m
163 to debug a new magic file before installing it.
164 .TP 8
165 .B "\-C, \-\-compile"
166 Write a magic.mgc output file that contains a pre-parsed version of
167 file.
168 .TP 8
169 .BI "\-f, \-\-files\-from" " namefile"
170 Read the names of the files to be examined from 
171 .I namefile
172 (one per line) 
173 before the argument list.
174 Either 
175 .I namefile
176 or at least one filename argument must be present;
177 to test the standard input, use ``\-'' as a filename argument.
178 .TP 8
179 .BI "\-F, \-\-separator" " separator"
180 Use the specified string as the separator between the filename and the
181 file result returned. Defaults to ``:''.
182 .TP 8
183 .B "\-i, \-\-mime"
184 Causes the file command to output mime type strings rather than the more
185 traditional human readable ones. Thus it may say
186 ``text/plain; charset=us-ascii''
187 rather
188 than ``ASCII text''.
189 In order for this option to work, file changes the way
190 it handles files recognised by the command itself (such as many of the
191 text file types, directories etc), and makes use of an alternative
192 ``magic'' file.
193 (See ``FILES'' section, below).
194 .TP 8
195 .B "\-k, \-\-keep\-going"
196 Don't stop at the first match, keep going.
197 .TP 8
198 .B "\-L, \-\-dereference"
199 option causes symlinks to be followed, as the like-named option in
200 .BR ls (1).
201 (on systems that support symbolic links).
202 .TP 8
203 .BI "\-m, \-\-magic\-file" " list"
204 Specify an alternate list of files containing magic numbers.
205 This can be a single file, or a colon-separated list of files.
206 If a compiled magic file is found alongside, it will be used instead.
207 With the \-i or \-\-mime option, the program adds ".mime" to each file name.
208 .TP 8
209 .B "\-n, \-\-no\-buffer"
210 Force stdout to be flushed after checking each file.
211 This is only useful if checking a list of files.
212 It is intended to be used by programs that want filetype output from a pipe.
213 .TP 8
214 .B "\-N, \-\-no\-pad"
215 Don't pad filenames so that they align in the output.
216 .TP 8
217 .B "\-p, \-\-preserve\-date"
218 On systems that support
219 .BR utime (2)
220 or
221 .BR utimes(2),
222 attempt to preserve the access time of files analyzed, to pretend that
223 .BR file (2)
224 never read them.
225 .TP 8
226 .B "\-r, \-\-raw"
227 Don't translate unprintable characters to \eooo.
228 Normally
229 .B file
230 translates unprintable characters to their octal representation.
231 .TP 8
232 .B "\-s, \-\-special\-files"
233 Normally,
234 .B file
235 only attempts to read and determine the type of argument files which
236 .BR stat (2)
237 reports are ordinary files.
238 This prevents problems, because reading special files may have peculiar
239 consequences.
240 Specifying the
241 .BR \-s
242 option causes
243 .B file
244 to also read argument files which are block or character special files.
245 This is useful for determining the filesystem types of the data in raw
246 disk partitions, which are block special files.
247 This option also causes
248 .B file
249 to disregard the file size as reported by
250 .BR stat (2)
251 since on some systems it reports a zero size for raw disk partitions.
252 .TP 8
253 .B "\-v, \-\-version"
254 Print the version of the program and exit.
255 .TP 8
256 .B "\-z, \-\-uncompress"
257 Try to look inside compressed files.
258 .TP 8
259 .B "\-\-help"
260 Print a help message and exit.
261 .SH FILES
262 .TP
263 .I __MAGIC__.mgc
264 Default compiled list of magic numbers
265 .TP
266 .I __MAGIC__
267 Default list of magic numbers
268 .TP
269 .I __MAGIC__.mime.mgc
270 Default compiled list of magic numbers, used to output mime types when
271 the -i option is specified.
272 .TP
273 .I __MAGIC__.mime
274 Default list of magic numbers, used to output mime types when the -i option
275 is specified.
276 .TP
277 .I /etc/magic
278 Local additions to magic wisdom.
279
280 .SH ENVIRONMENT
281 The environment variable
282 .B MAGIC
283 can be used to set the default magic number file name.
284 .B file
285 adds ".mime" and/or ".mgc" to the value of this variable as appropriate.
286 .SH SEE ALSO
287 .BR magic (__FSECTION__)
288 \- description of magic file format.
289 .br
290 .BR strings (1), " od" (1), " hexdump(1)"
291 \- tools for examining non-textfiles.
292 .SH STANDARDS CONFORMANCE
293 This program is believed to exceed the System V Interface Definition
294 of FILE(CMD), as near as one can determine from the vague language
295 contained therein. 
296 Its behaviour is mostly compatible with the System V program of the same name.
297 This version knows more magic, however, so it will produce
298 different (albeit more accurate) output in many cases. 
299 .PP
300 The one significant difference 
301 between this version and System V
302 is that this version treats any white space
303 as a delimiter, so that spaces in pattern strings must be escaped.
304 For example,
305 .br
306 >10     string  language impress\       (imPRESS data)
307 .br
308 in an existing magic file would have to be changed to
309 .br
310 >10     string  language\e impress      (imPRESS data)
311 .br
312 In addition, in this version, if a pattern string contains a backslash,
313 it must be escaped.
314 For example
315 .br
316 0       string          \ebegindata     Andrew Toolkit document
317 .br
318 in an existing magic file would have to be changed to
319 .br
320 0       string          \e\ebegindata   Andrew Toolkit document
321 .br
322 .PP
323 SunOS releases 3.2 and later from Sun Microsystems include a
324 .BR file (1)
325 command derived from the System V one, but with some extensions.
326 My version differs from Sun's only in minor ways.
327 It includes the extension of the `&' operator, used as,
328 for example,
329 .br
330 >16     long&0x7fffffff >0              not stripped
331 .SH MAGIC DIRECTORY
332 The magic file entries have been collected from various sources,
333 mainly USENET, and contributed by various authors.
334 Christos Zoulas (address below) will collect additional
335 or corrected magic file entries.
336 A consolidation of magic file entries 
337 will be distributed periodically.
338 .PP
339 The order of entries in the magic file is significant.
340 Depending on what system you are using, the order that
341 they are put together may be incorrect.
342 If your old
343 .B file
344 command uses a magic file,
345 keep the old magic file around for comparison purposes
346 (rename it to 
347 .IR __MAGIC__.orig ).
348 .SH EXAMPLES
349 .nf
350 $ file file.c file /dev/{wd0a,hda}
351 file.c:   C program text
352 file:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
353           dynamically linked (uses shared libs), stripped
354 /dev/wd0a: block special (0/0)
355 /dev/hda: block special (3/0)
356 $ file -s /dev/wd0{b,d}
357 /dev/wd0b: data
358 /dev/wd0d: x86 boot sector
359 $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
360 /dev/hda:   x86 boot sector
361 /dev/hda1:  Linux/i386 ext2 filesystem
362 /dev/hda2:  x86 boot sector
363 /dev/hda3:  x86 boot sector, extended partition table
364 /dev/hda4:  Linux/i386 ext2 filesystem
365 /dev/hda5:  Linux/i386 swap file
366 /dev/hda6:  Linux/i386 swap file
367 /dev/hda7:  Linux/i386 swap file
368 /dev/hda8:  Linux/i386 swap file
369 /dev/hda9:  empty
370 /dev/hda10: empty
371
372 $ file -i file.c file /dev/{wd0a,hda}
373 file.c:      text/x-c
374 file:        application/x-executable, dynamically linked (uses shared libs),
375 not stripped
376 /dev/hda:    application/x-not-regular-file
377 /dev/wd0a:   application/x-not-regular-file
378
379 .fi
380 .SH HISTORY
381 There has been a 
382 .B file
383 command in every \s-1UNIX\s0 since at least Research Version 4
384 (man page dated November, 1973).
385 The System V version introduced one significant major change:
386 the external list of magic number types.
387 This slowed the program down slightly but made it a lot more flexible.
388 .PP
389 This program, based on the System V version,
390 was written by Ian Darwin <ian@darwinsys.com>
391 without looking at anybody else's source code.
392 .PP
393 John Gilmore revised the code extensively, making it better than
394 the first version.
395 Geoff Collyer found several inadequacies
396 and provided some magic file entries.
397 Contributions by the `&' operator by Rob McMahon, cudcv@warwick.ac.uk, 1989.
398 .PP
399 Guy Harris, guy@netapp.com, made many changes from 1993 to the present.
400 .PP
401 Primary development and maintenance from 1990 to the present by
402 Christos Zoulas (christos@astron.com).
403 .PP
404 Altered by Chris Lowth, chris@lowth.com, 2000:
405 Handle the ``-i'' option to output mime type strings and using an alternative
406 magic file and internal logic.
407 .PP
408 Altered by Eric Fischer (enf@pobox.com), July, 2000,
409 to identify character codes and attempt to identify the languages
410 of non-ASCII files.
411 .PP
412 The list of contributors to the "Magdir" directory (source for the
413 /etc/magic
414 file) is too long to include here.
415 You know who you are; thank you.
416 .SH LEGAL NOTICE
417 Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
418 Covered by the standard Berkeley Software Distribution copyright; see the file
419 LEGAL.NOTICE in the source distribution.
420 .PP
421 The files
422 .I tar.h
423 and
424 .I is_tar.c
425 were written by John Gilmore from his public-domain
426 .B tar
427 program, and are not covered by the above license.
428 .SH BUGS
429 There must be a better way to automate the construction of the Magic
430 file from all the glop in magdir.
431 What is it?
432 Better yet, the magic file should be compiled into binary (say,
433 .BR ndbm (3)
434 or, better yet, fixed-length
435 .SM ASCII
436 strings for use in heterogenous network environments) for faster startup.
437 Then the program would run as fast as the Version 7 program of the same name,
438 with the flexibility of the System V version.
439 .PP
440 .B File
441 uses several algorithms that favor speed over accuracy,
442 thus it can be misled about the contents of
443 text
444 files.
445 .PP
446 The support for
447 text
448 files (primarily for programming languages)
449 is simplistic, inefficient and requires recompilation to update.
450 .PP
451 There should be an ``else'' clause to follow a series of continuation lines.
452 .PP
453 The magic file and keywords should have regular expression support.
454 Their use of
455 .SM "ASCII TAB"
456 as a field delimiter is ugly and makes
457 it hard to edit the files, but is entrenched.
458 .PP
459 It might be advisable to allow upper-case letters in keywords
460 for e.g.,
461 .BR troff (1)
462 commands vs man page macros.
463 Regular expression support would make this easy.
464 .PP
465 The program doesn't grok \s-2FORTRAN\s0.
466 It should be able to figure \s-2FORTRAN\s0 by seeing some keywords which 
467 appear indented at the start of line.
468 Regular expression support would make this easy.
469 .PP
470 The list of keywords in 
471 .I ascmagic
472 probably belongs in the Magic file.
473 This could be done by using some keyword like `*' for the offset value.
474 .PP
475 Another optimisation would be to sort
476 the magic file so that we can just run down all the
477 tests for the first byte, first word, first long, etc, once we
478 have fetched it.
479 Complain about conflicts in the magic file entries.
480 Make a rule that the magic entries sort based on file offset rather
481 than position within the magic file?
482 .PP
483 The program should provide a way to give an estimate 
484 of ``how good'' a guess is.
485 We end up removing guesses (e.g. ``From '' as first 5 chars of file) because
486 they are not as good as other guesses (e.g. ``Newsgroups:'' versus
487 ``Return-Path:'').
488 Still, if the others don't pan out, it should be possible to use the
489 first guess.  
490 .PP
491 This program is slower than some vendors' file commands.
492 The new support for multiple character codes makes it even slower.
493 .PP
494 This manual page, and particularly this section, is too long.
495 .SH AVAILABILITY
496 You can obtain the original author's latest version by anonymous FTP
497 on
498 .B ftp.astron.com
499 in the directory
500 .I /pub/file/file-X.YZ.tar.gz