file.man 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. .TH FILE __CSECTION__ "Copyright but distributable"
  2. .\" $Id: file.man,v 1.31 1998/02/15 23:18:53 christos Exp $
  3. .SH NAME
  4. file
  5. \- determine file type
  6. .SH SYNOPSIS
  7. .B file
  8. [
  9. .B \-vbczL
  10. ]
  11. [
  12. .B \-f
  13. namefile ]
  14. [
  15. .B \-m
  16. magicfiles ]
  17. file ...
  18. .SH DESCRIPTION
  19. This manual page documents version __VERSION__ of the
  20. .B file
  21. command.
  22. .B File
  23. tests each argument in an attempt to classify it.
  24. There are three sets of tests, performed in this order:
  25. filesystem tests, magic number tests, and language tests.
  26. The
  27. .I first
  28. test that succeeds causes the file type to be printed.
  29. .PP
  30. The type printed will usually contain one of the words
  31. .B text
  32. (the file contains only
  33. .SM ASCII
  34. characters and is probably safe to read on an
  35. .SM ASCII
  36. terminal),
  37. .B executable
  38. (the file contains the result of compiling a program
  39. in a form understandable to some \s-1UNIX\s0 kernel or another),
  40. or
  41. .B data
  42. meaning anything else (data is usually `binary' or non-printable).
  43. Exceptions are well-known file formats (core files, tar archives)
  44. that are known to contain binary data.
  45. When modifying the file
  46. .I __MAGIC__
  47. or the program itself,
  48. .B "preserve these keywords" .
  49. People depend on knowing that all the readable files in a directory
  50. have the word ``text'' printed.
  51. Don't do as Berkeley did \- change ``shell commands text''
  52. to ``shell script''.
  53. .PP
  54. The filesystem tests are based on examining the return from a
  55. .BR stat (2)
  56. system call.
  57. The program checks to see if the file is empty,
  58. or if it's some sort of special file.
  59. Any known file types appropriate to the system you are running on
  60. (sockets, symbolic links, or named pipes (FIFOs) on those systems that
  61. implement them)
  62. are intuited if they are defined in
  63. the system header file
  64. .IR sys/stat.h .
  65. .PP
  66. The magic number tests are used to check for files with data in
  67. particular fixed formats.
  68. The canonical example of this is a binary executable (compiled program)
  69. .I a.out
  70. file, whose format is defined in
  71. .I a.out.h
  72. and possibly
  73. .I exec.h
  74. in the standard include directory.
  75. These files have a `magic number' stored in a particular place
  76. near the beginning of the file that tells the \s-1UNIX\s0 operating system
  77. that the file is a binary executable, and which of several types thereof.
  78. The concept of `magic number' has been applied by extension to data files.
  79. Any file with some invariant identifier at a small fixed
  80. offset into the file can usually be described in this way.
  81. The information in these files is read from the magic file
  82. .I __MAGIC__.
  83. .PP
  84. If an argument appears to be an
  85. .SM ASCII
  86. file,
  87. .B file
  88. attempts to guess its language.
  89. The language tests look for particular strings (cf
  90. .IR names.h )
  91. that can appear anywhere in the first few blocks of a file.
  92. For example, the keyword
  93. .B .br
  94. indicates that the file is most likely a
  95. .BR troff (1)
  96. input file, just as the keyword
  97. .B struct
  98. indicates a C program.
  99. These tests are less reliable than the previous
  100. two groups, so they are performed last.
  101. The language test routines also test for some miscellany
  102. (such as
  103. .BR tar (1)
  104. archives) and determine whether an unknown file should be
  105. labelled as `ascii text' or `data'.
  106. .SH OPTIONS
  107. .TP 8
  108. .B \-v
  109. Print the version of the program and exit.
  110. .TP 8
  111. .B \-m list
  112. Specify an alternate list of files containing magic numbers.
  113. This can be a single file, or a colon-separated list of files.
  114. .TP 8
  115. .B \-z
  116. Try to look inside compressed files.
  117. .TP 8
  118. .B \-b
  119. Do not prepend filenames to output lines (brief mode).
  120. .TP 8
  121. .B \-c
  122. Cause a checking printout of the parsed form of the magic file.
  123. This is usually used in conjunction with
  124. .B \-m
  125. to debug a new magic file before installing it.
  126. .TP 8
  127. .B \-f namefile
  128. Read the names of the files to be examined from
  129. .I namefile
  130. (one per line)
  131. before the argument list.
  132. Either
  133. .I namefile
  134. or at least one filename argument must be present;
  135. to test the standard input, use ``-'' as a filename argument.
  136. .TP 8
  137. .B \-L
  138. option causes symlinks to be followed, as the like-named option in
  139. .BR ls (1).
  140. (on systems that support symbolic links).
  141. .SH FILES
  142. .I __MAGIC__
  143. \- default list of magic numbers
  144. .SH ENVIRONMENT
  145. The environment variable
  146. .B MAGIC
  147. can be used to set the default magic number files.
  148. .SH SEE ALSO
  149. .BR magic (__FSECTION__)
  150. \- description of magic file format.
  151. .br
  152. .BR strings (1), " od" (1), " hexdump(1)"
  153. \- tools for examining non-textfiles.
  154. .SH STANDARDS CONFORMANCE
  155. This program is believed to exceed the System V Interface Definition
  156. of FILE(CMD), as near as one can determine from the vague language
  157. contained therein.
  158. Its behaviour is mostly compatible with the System V program of the same name.
  159. This version knows more magic, however, so it will produce
  160. different (albeit more accurate) output in many cases.
  161. .PP
  162. The one significant difference
  163. between this version and System V
  164. is that this version treats any white space
  165. as a delimiter, so that spaces in pattern strings must be escaped.
  166. For example,
  167. .br
  168. >10 string language impress\ (imPRESS data)
  169. .br
  170. in an existing magic file would have to be changed to
  171. .br
  172. >10 string language\e impress (imPRESS data)
  173. .br
  174. In addition, in this version, if a pattern string contains a backslash,
  175. it must be escaped. For example
  176. .br
  177. 0 string \ebegindata Andrew Toolkit document
  178. .br
  179. in an existing magic file would have to be changed to
  180. .br
  181. 0 string \e\ebegindata Andrew Toolkit document
  182. .br
  183. .PP
  184. SunOS releases 3.2 and later from Sun Microsystems include a
  185. .BR file (1)
  186. command derived from the System V one, but with some extensions.
  187. My version differs from Sun's only in minor ways.
  188. It includes the extension of the `&' operator, used as,
  189. for example,
  190. .br
  191. >16 long&0x7fffffff >0 not stripped
  192. .SH MAGIC DIRECTORY
  193. The magic file entries have been collected from various sources,
  194. mainly USENET, and contributed by various authors.
  195. Christos Zoulas (address below) will collect additional
  196. or corrected magic file entries.
  197. A consolidation of magic file entries
  198. will be distributed periodically.
  199. .PP
  200. The order of entries in the magic file is significant.
  201. Depending on what system you are using, the order that
  202. they are put together may be incorrect.
  203. If your old
  204. .B file
  205. command uses a magic file,
  206. keep the old magic file around for comparison purposes
  207. (rename it to
  208. .IR __MAGIC__.orig ).
  209. .SH HISTORY
  210. There has been a
  211. .B file
  212. command in every \s-1UNIX\s0 since at least Research Version 6
  213. (man page dated January, 1975).
  214. The System V version introduced one significant major change:
  215. the external list of magic number types.
  216. This slowed the program down slightly but made it a lot more flexible.
  217. .PP
  218. This program, based on the System V version,
  219. was written by Ian Darwin without looking at anybody else's source code.
  220. .PP
  221. John Gilmore revised the code extensively, making it better than
  222. the first version.
  223. Geoff Collyer found several inadequacies
  224. and provided some magic file entries.
  225. The program has undergone continued evolution since.
  226. .SH AUTHOR
  227. Written by Ian F. Darwin, UUCP address {utzoo | ihnp4}!darwin!ian,
  228. Internet address ian@sq.com,
  229. postal address: P.O. Box 603, Station F, Toronto, Ontario, CANADA M4Y 2L8.
  230. .PP
  231. Altered by Rob McMahon, cudcv@warwick.ac.uk, 1989, to extend the `&' operator
  232. from simple `x&y != 0' to `x&y op z'.
  233. .PP
  234. Altered by Guy Harris, guy@netapp.com, 1993, to:
  235. .RS
  236. .PP
  237. put the ``old-style'' `&'
  238. operator back the way it was, because 1) Rob McMahon's change broke the
  239. previous style of usage, 2) the SunOS ``new-style'' `&' operator,
  240. which this version of
  241. .B file
  242. supports, also handles `x&y op z', and 3) Rob's change wasn't documented
  243. in any case;
  244. .PP
  245. put in multiple levels of `>';
  246. .PP
  247. put in ``beshort'', ``leshort'', etc. keywords to look at numbers in the
  248. file in a specific byte order, rather than in the native byte order of
  249. the process running
  250. .BR file .
  251. .RE
  252. .PP
  253. Changes by Ian Darwin and various authors including
  254. Christos Zoulas (christos@astron.com), 1990-1997.
  255. .SH LEGAL NOTICE
  256. Copyright (c) Ian F. Darwin, Toronto, Canada,
  257. 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993.
  258. .PP
  259. This software is not subject to and may not be made subject to any
  260. license of the American Telephone and Telegraph Company, Sun
  261. Microsystems Inc., Digital Equipment Inc., Lotus Development Inc., the
  262. Regents of the University of California, The X Consortium or MIT, or
  263. The Free Software Foundation.
  264. .PP
  265. This software is not subject to any export provision of the United States
  266. Department of Commerce, and may be exported to any country or planet.
  267. .PP
  268. Permission is granted to anyone to use this software for any purpose on
  269. any computer system, and to alter it and redistribute it freely, subject
  270. to the following restrictions:
  271. .PP
  272. 1. The author is not responsible for the consequences of use of this
  273. software, no matter how awful, even if they arise from flaws in it.
  274. .PP
  275. 2. The origin of this software must not be misrepresented, either by
  276. explicit claim or by omission. Since few users ever read sources,
  277. credits must appear in the documentation.
  278. .PP
  279. 3. Altered versions must be plainly marked as such, and must not be
  280. misrepresented as being the original software. Since few users
  281. ever read sources, credits must appear in the documentation.
  282. .PP
  283. 4. This notice may not be removed or altered.
  284. .PP
  285. A few support files (\fIgetopt\fP, \fIstrtok\fP)
  286. distributed with this package
  287. are by Henry Spencer and are subject to the same terms as above.
  288. .PP
  289. A few simple support files (\fIstrtol\fP, \fIstrchr\fP)
  290. distributed with this package
  291. are in the public domain; they are so marked.
  292. .PP
  293. The files
  294. .I tar.h
  295. and
  296. .I is_tar.c
  297. were written by John Gilmore from his public-domain
  298. .B tar
  299. program, and are not covered by the above restrictions.
  300. .SH BUGS
  301. There must be a better way to automate the construction of the Magic
  302. file from all the glop in Magdir. What is it?
  303. Better yet, the magic file should be compiled into binary (say,
  304. .BR ndbm (3)
  305. or, better yet, fixed-length
  306. .SM ASCII
  307. strings for use in heterogenous network environments) for faster startup.
  308. Then the program would run as fast as the Version 7 program of the same name,
  309. with the flexibility of the System V version.
  310. .PP
  311. .B File
  312. uses several algorithms that favor speed over accuracy,
  313. thus it can be misled about the contents of
  314. .SM ASCII
  315. files.
  316. .PP
  317. The support for
  318. .SM ASCII
  319. files (primarily for programming languages)
  320. is simplistic, inefficient and requires recompilation to update.
  321. .PP
  322. There should be an ``else'' clause to follow a series of continuation lines.
  323. .PP
  324. The magic file and keywords should have regular expression support.
  325. Their use of
  326. .SM "ASCII TAB"
  327. as a field delimiter is ugly and makes
  328. it hard to edit the files, but is entrenched.
  329. .PP
  330. It might be advisable to allow upper-case letters in keywords
  331. for e.g.,
  332. .BR troff (1)
  333. commands vs man page macros.
  334. Regular expression support would make this easy.
  335. .PP
  336. The program doesn't grok \s-2FORTRAN\s0.
  337. It should be able to figure \s-2FORTRAN\s0 by seeing some keywords which
  338. appear indented at the start of line.
  339. Regular expression support would make this easy.
  340. .PP
  341. The list of keywords in
  342. .I ascmagic
  343. probably belongs in the Magic file.
  344. This could be done by using some keyword like `*' for the offset value.
  345. .PP
  346. Another optimisation would be to sort
  347. the magic file so that we can just run down all the
  348. tests for the first byte, first word, first long, etc, once we
  349. have fetched it. Complain about conflicts in the magic file entries.
  350. Make a rule that the magic entries sort based on file offset rather
  351. than position within the magic file?
  352. .PP
  353. The program should provide a way to give an estimate
  354. of ``how good'' a guess is.
  355. We end up removing guesses (e.g. ``From '' as first 5 chars of file) because
  356. they are not as good as other guesses (e.g. ``Newsgroups:'' versus
  357. "Return-Path:"). Still, if the others don't pan out, it should be
  358. possible to use the first guess.
  359. .PP
  360. This program is slower than some vendors' file commands.
  361. .PP
  362. This manual page, and particularly this section, is too long.
  363. .SH AVAILABILITY
  364. You can obtain the original author's latest version by anonymous FTP
  365. on
  366. .B ftp.astron.com
  367. in the directory
  368. .I /pub/file/file-X.YY.tar.gz