Annotation of src/usr.bin/file/file.1, Revision 1.16
1.16 ! mickey 1: .\" $OpenBSD: file.1,v 1.15 2001/10/04 23:02:32 pjanzen Exp $
1.8 aaron 2: .\" $FreeBSD: src/usr.bin/file/file.1,v 1.16 2000/03/01 12:19:39 sheldonh Exp $
3: .Dd July 30, 1997
4: .Dt FILE 1
5: .Os
6: .Sh NAME
7: .Nm file
8: .Nd determine file type
9: .Sh SYNOPSIS
10: .Nm file
11: .Op Fl vczL
12: .Op Fl f Ar namefile
13: .Op Fl m Ar magicfiles
14: .Ar file Op Ar ...
15: .Sh DESCRIPTION
1.4 millert 16: This manual page documents version 3.22 of the
1.8 aaron 17: .Nm
1.4 millert 18: command.
1.8 aaron 19: .Nm
1.1 deraadt 20: tests each argument in an attempt to classify it.
21: There are three sets of tests, performed in this order:
22: filesystem tests, magic number tests, and language tests.
1.8 aaron 23: The first test that succeeds causes the file type to be printed.
24: .Pp
1.1 deraadt 25: The type printed will usually contain one of the words
1.8 aaron 26: .Dq text
1.4 millert 27: (the file contains only
1.8 aaron 28: .Tn ASCII
1.4 millert 29: characters and is probably safe to read on an
1.8 aaron 30: .Tn ASCII
1.4 millert 31: terminal),
1.8 aaron 32: .Dq executable
1.1 deraadt 33: (the file contains the result of compiling a program
1.8 aaron 34: in a form understandable to some
35: .Ux
36: kernel or another),
1.1 deraadt 37: or
1.8 aaron 38: .Dq data
39: meaning anything else (data is usually binary or non-printable).
40: .Pp
1.1 deraadt 41: Exceptions are well-known file formats (core files, tar archives)
42: that are known to contain binary data.
43: When modifying the file
1.8 aaron 44: .Pa /etc/magic
1.6 aaron 45: or the program itself,
1.8 aaron 46: .Em "preserve these keywords" .
47: .Pp
1.1 deraadt 48: People depend on knowing that all the readable files in a directory
1.8 aaron 49: have the word
50: .Dq text
51: printed.
52: Don't do as Berkeley did; change
53: .Dq shell commands text
54: to
55: .Dq shell script .
56: .Pp
1.1 deraadt 57: The filesystem tests are based on examining the return from a
1.8 aaron 58: .Xr stat 2
1.1 deraadt 59: system call.
60: The program checks to see if the file is empty,
61: or if it's some sort of special file.
62: Any known file types appropriate to the system you are running on
63: (sockets, symbolic links, or named pipes (FIFOs) on those systems that
64: implement them)
65: are intuited if they are defined in
66: the system header file
1.9 aaron 67: .Aq Pa sys/stat.h .
1.8 aaron 68: .Pp
1.1 deraadt 69: The magic number tests are used to check for files with data in
70: particular fixed formats.
71: The canonical example of this is a binary executable (compiled program)
1.8 aaron 72: .Pa a.out
1.6 aaron 73: file, whose format is defined in
1.8 aaron 74: .Aq Pa a.out.h
1.1 deraadt 75: and possibly
1.8 aaron 76: .Aq Pa exec.h
1.1 deraadt 77: in the standard include directory.
1.8 aaron 78: These files have a
79: .Dq magic number
80: stored in a particular place
81: near the beginning of the file that tells the
82: .Ux
83: operating system
1.1 deraadt 84: that the file is a binary executable, and which of several types thereof.
1.8 aaron 85: .Pp
86: The concept of magic number has been applied by extension to data files.
1.1 deraadt 87: Any file with some invariant identifier at a small fixed
88: offset into the file can usually be described in this way.
89: The information in these files is read from the magic file
1.8 aaron 90: .Pa /etc/magic .
91: .Pp
1.1 deraadt 92: If an argument appears to be an
1.8 aaron 93: .Tn ASCII
1.1 deraadt 94: file,
1.8 aaron 95: .Nm
1.1 deraadt 96: attempts to guess its language.
1.4 millert 97: The language tests look for particular strings (cf
1.8 aaron 98: .Pa names.h )
1.1 deraadt 99: that can appear anywhere in the first few blocks of a file.
100: For example, the keyword
1.8 aaron 101: .Em .br
1.4 millert 102: indicates that the file is most likely a
1.8 aaron 103: .Xr troff 1
1.6 aaron 104: input file, just as the keyword
1.8 aaron 105: .Li struct
1.1 deraadt 106: indicates a C program.
107: These tests are less reliable than the previous
108: two groups, so they are performed last.
109: The language test routines also test for some miscellany
1.6 aaron 110: (such as
1.8 aaron 111: .Xr tar 1
1.1 deraadt 112: archives) and determine whether an unknown file should be
1.8 aaron 113: labelled as
114: .Dq ASCII text
115: or
116: .Dq data .
117: .Pp
118: The options are as follows:
1.11 aaron 119: .Bl -tag -width Ds
1.8 aaron 120: .It Fl v
1.1 deraadt 121: Print the version of the program and exit.
1.8 aaron 122: .It Fl m Ar list
123: Specify an alternate
124: .Ar list
125: of files containing magic numbers.
1.2 deraadt 126: This can be a single file, or a colon-separated list of files.
1.8 aaron 127: .It Fl z
1.1 deraadt 128: Try to look inside compressed files.
1.8 aaron 129: .It Fl c
1.1 deraadt 130: Cause a checking printout of the parsed form of the magic file.
1.6 aaron 131: This is usually used in conjunction with
1.8 aaron 132: .Fl m
1.1 deraadt 133: to debug a new magic file before installing it.
1.8 aaron 134: .It Fl f Ar namefile
1.6 aaron 135: Read the names of the files to be examined from
1.8 aaron 136: .Ar namefile
1.6 aaron 137: (one per line)
1.1 deraadt 138: before the argument list.
1.6 aaron 139: Either
1.8 aaron 140: .Ar namefile
1.1 deraadt 141: or at least one filename argument must be present;
1.8 aaron 142: to test the standard input, use
143: .Dq -
144: as a filename argument.
145: .It Fl L
146: Cause symlinks to be followed, as the like-named option in
147: .Xr ls 1 .
1.1 deraadt 148: (on systems that support symbolic links).
1.8 aaron 149: .El
150: .Sh ENVIRONMENT
151: .Bl -tag -width indent
1.13 smart 152: .It Ev MAGIC
1.8 aaron 153: Default magic number files.
154: .El
1.12 aaron 155: .Sh FILES
156: .Bl -tag -width /etc/magic -compact
157: .It Pa /etc/magic
158: default list of magic numbers
159: .El
1.8 aaron 160: .Sh SEE ALSO
161: .Xr hexdump 1 ,
162: .Xr od 1 ,
163: .Xr strings 1 ,
164: .Xr magic 5
165: .Sh STANDARDS CONFORMANCE
1.1 deraadt 166: This program is believed to exceed the System V Interface Definition
167: of FILE(CMD), as near as one can determine from the vague language
1.6 aaron 168: contained therein.
1.1 deraadt 169: Its behaviour is mostly compatible with the System V program of the same name.
170: This version knows more magic, however, so it will produce
1.6 aaron 171: different (albeit more accurate) output in many cases.
1.8 aaron 172: .Pp
1.6 aaron 173: The one significant difference
1.1 deraadt 174: between this version and System V
1.8 aaron 175: is that this version treats any white space
1.1 deraadt 176: as a delimiter, so that spaces in pattern strings must be escaped.
177: For example,
1.8 aaron 178: .Pp
179: >10 string language impress\ (imPRESS data)
180: .Pp
1.1 deraadt 181: in an existing magic file would have to be changed to
1.8 aaron 182: .Pp
183: >10 string language\e impress (imPRESS data)
184: .Pp
1.1 deraadt 185: In addition, in this version, if a pattern string contains a backslash,
1.9 aaron 186: it must be escaped.
187: For example
1.8 aaron 188: .Pp
189: 0 string \ebegindata Andrew Toolkit document
190: .Pp
1.1 deraadt 191: in an existing magic file would have to be changed to
1.8 aaron 192: .Pp
193: 0 string \e\ebegindata Andrew Toolkit document
194: .Pp
1.1 deraadt 195: SunOS releases 3.2 and later from Sun Microsystems include a
1.8 aaron 196: .Xr file 1
1.1 deraadt 197: command derived from the System V one, but with some extensions.
198: My version differs from Sun's only in minor ways.
1.8 aaron 199: It includes the extension of the
200: .Ql &
201: operator, used as,
1.1 deraadt 202: for example,
1.8 aaron 203: .Pp
204: >16 long&0x7fffffff >0 not stripped
205: .Sh MAGIC DIRECTORY
1.1 deraadt 206: The magic file entries have been collected from various sources,
207: mainly USENET, and contributed by various authors.
1.8 aaron 208: .An Christos Zoulas
209: (address below) will collect additional
1.1 deraadt 210: or corrected magic file entries.
1.6 aaron 211: A consolidation of magic file entries
1.1 deraadt 212: will be distributed periodically.
213: The order of entries in the magic file is significant.
214: Depending on what system you are using, the order that
215: they are put together may be incorrect.
216: If your old
1.8 aaron 217: .Nm
1.1 deraadt 218: command uses a magic file,
219: keep the old magic file around for comparison purposes
1.6 aaron 220: (rename it to
1.8 aaron 221: .Pa /etc/magic.orig ) .
222: .Sh HISTORY
1.6 aaron 223: There has been a
1.8 aaron 224: .Nm
225: command in every
226: .Ux
1.16 ! mickey 227: since at least Research Version 4
! 228: (man page dated November, 1973).
1.1 deraadt 229: The System V version introduced one significant major change:
230: the external list of magic number types.
231: This slowed the program down slightly but made it a lot more flexible.
1.8 aaron 232: .Pp
1.10 ian 233: This program, based on the System V version, was written by
234: .An Ian F. Darwin Aq ian@darwinisys.com
1.8 aaron 235: without looking at anybody else's source code.
236: .Pp
237: .An John Gilmore
238: revised the code extensively, making it better than
1.1 deraadt 239: the first version.
1.8 aaron 240: .An Geoff Collyer
241: found several inadequacies
1.1 deraadt 242: and provided some magic file entries.
1.8 aaron 243: .Pp
244: Altered by
245: .An Rob McMahon Aq cudcv@warwick.ac.uk ,
246: 1989, to extend the
247: .Ql &
248: operator from simple
249: .Dq x&y != 0
250: to
251: .Dq x&y op z .
252: .Pp
253: Altered by
254: .An Guy Harris Aq guy@auspex.com ,
255: 1993, to:
256: .Bl -item -offset indent
257: .It
258: put the
259: .Dq old-style
260: .Ql &
261: operator back the way it was, because
262: .Bl -enum -offset indent
263: .It
264: Rob McMahon's change broke the
265: previous style of usage,
266: .It
267: The SunOS
268: .Dq new-style
269: .Ql &
270: operator, which this version of
271: .Nm
272: supports, also handles
273: .Dq x&y op z ,
274: .It
275: Rob's change wasn't documented in any case;
276: .El
277: .It
278: put in multiple levels of
279: .Ql > ;
280: .It
281: put in
282: .Dq beshort ,
283: .Dq leshort ,
284: etc. keywords to look at numbers in the
1.1 deraadt 285: file in a specific byte order, rather than in the native byte order of
286: the process running
1.8 aaron 287: .Nm file .
288: .El
289: .Pp
1.10 ian 290: Currently maintained by
291: .An Christos Zoulas Aq christos@zoulas.com .
1.8 aaron 292: .Sh LEGAL NOTICE
1.10 ian 293: Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
294: Covered by the standard Berkeley Software Distribution copyright; see the file
295: LEGAL.NOTICE in the distribution.
1.8 aaron 296: .Pp
1.1 deraadt 297: The files
1.8 aaron 298: .Pa tar.h
1.1 deraadt 299: and
1.8 aaron 300: .Pa is_tar.c
301: were written by
302: .An John Gilmore
303: from his public-domain
304: .Nm tar
1.10 ian 305: program.
1.8 aaron 306: .Sh BUGS
1.1 deraadt 307: There must be a better way to automate the construction of the Magic
1.8 aaron 308: file from all the glop in Magdir.
309: What is it?
1.1 deraadt 310: Better yet, the magic file should be compiled into binary (say,
1.8 aaron 311: .Xr ndbm 3
1.4 millert 312: or, better yet, fixed-length
1.8 aaron 313: .Tn ASCII
1.4 millert 314: strings for use in heterogenous network environments) for faster startup.
1.1 deraadt 315: Then the program would run as fast as the Version 7 program of the same name,
316: with the flexibility of the System V version.
1.8 aaron 317: .Pp
318: .Nm
1.15 pjanzen 319: uses several algorithms that favor speed over accuracy;
1.4 millert 320: thus it can be misled about the contents of
1.8 aaron 321: .Tn ASCII
1.4 millert 322: files.
1.8 aaron 323: .Pp
1.4 millert 324: The support for
1.8 aaron 325: .Tn ASCII
1.4 millert 326: files (primarily for programming languages)
1.1 deraadt 327: is simplistic, inefficient and requires recompilation to update.
1.8 aaron 328: .Pp
329: There should be an
330: .Dq else
331: clause to follow a series of continuation lines.
332: .Pp
1.1 deraadt 333: The magic file and keywords should have regular expression support.
1.4 millert 334: Their use of
1.8 aaron 335: .Tn ASCII TAB
1.4 millert 336: as a field delimiter is ugly and makes
1.1 deraadt 337: it hard to edit the files, but is entrenched.
1.8 aaron 338: .Pp
1.1 deraadt 339: It might be advisable to allow upper-case letters in keywords
1.4 millert 340: for e.g.,
1.8 aaron 341: .Xr troff 1
1.4 millert 342: commands vs man page macros.
1.1 deraadt 343: Regular expression support would make this easy.
1.8 aaron 344: .Pp
1.1 deraadt 345: The program doesn't grok \s-2FORTRAN\s0.
1.6 aaron 346: It should be able to figure \s-2FORTRAN\s0 by seeing some keywords which
1.1 deraadt 347: appear indented at the start of line.
348: Regular expression support would make this easy.
1.8 aaron 349: .Pp
1.6 aaron 350: The list of keywords in
1.8 aaron 351: .Em ascmagic
1.1 deraadt 352: probably belongs in the Magic file.
1.8 aaron 353: This could be done by using some keyword like
354: .Ql *
355: for the offset value.
356: .Pp
357: Another optimization would be to sort
1.1 deraadt 358: the magic file so that we can just run down all the
359: tests for the first byte, first word, first long, etc, once we
1.9 aaron 360: have fetched it.
361: Complain about conflicts in the magic file entries.
1.1 deraadt 362: Make a rule that the magic entries sort based on file offset rather
363: than position within the magic file?
1.8 aaron 364: .Pp
1.6 aaron 365: The program should provide a way to give an estimate
1.8 aaron 366: of
367: .Dq how good
368: a guess is.
369: We end up removing guesses (e.g.,
370: .Dq From\
371: as first 5 chars of file) because
372: they are not as good as other guesses (e.g.,
373: .Dq Newsgroups:
374: versus
375: .Qq Return-Path: ) .
376: Still, if the others don't pan out, it should be
1.6 aaron 377: possible to use the first guess.
1.8 aaron 378: .Pp
379: This program is slower than some vendors'
380: .Nm
381: commands.
382: .Pp
1.1 deraadt 383: This manual page, and particularly this section, is too long.
1.8 aaron 384: .Sh AVAILABILITY
1.1 deraadt 385: You can obtain the original author's latest version by anonymous FTP
1.8 aaron 386: on
1.15 pjanzen 387: .Em ftp.astron.com
1.8 aaron 388: in the directory
389: .Pa /pub/file/file-X.YY.tar.gz