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