libmagic.man 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. .\" $File: libmagic.man,v 1.26 2011/12/19 17:49:31 christos Exp $
  2. .\"
  3. .\" Copyright (c) Christos Zoulas 2003.
  4. .\" All Rights Reserved.
  5. .\"
  6. .\" Redistribution and use in source and binary forms, with or without
  7. .\" modification, are permitted provided that the following conditions
  8. .\" are met:
  9. .\" 1. Redistributions of source code must retain the above copyright
  10. .\" notice immediately at the beginning of the file, without modification,
  11. .\" this list of conditions, and the following disclaimer.
  12. .\" 2. Redistributions in binary form must reproduce the above copyright
  13. .\" notice, this list of conditions and the following disclaimer in the
  14. .\" documentation and/or other materials provided with the distribution.
  15. .\"
  16. .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. .\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. .\" SUCH DAMAGE.
  27. .\"
  28. .Dd December 19, 2011
  29. .Dt LIBMAGIC 3
  30. .Os
  31. .Sh NAME
  32. .Nm magic_open ,
  33. .Nm magic_close ,
  34. .Nm magic_error ,
  35. .Nm magic_descriptor ,
  36. .Nm magic_buffer ,
  37. .Nm magic_setflags ,
  38. .Nm magic_check ,
  39. .Nm magic_compile ,
  40. .Nm magic_load
  41. .Nd Magic number recognition library
  42. .Sh LIBRARY
  43. .Lb libmagic
  44. .Sh SYNOPSIS
  45. .In magic.h
  46. .Ft magic_t
  47. .Fn magic_open "int flags"
  48. .Ft void
  49. .Fn magic_close "magic_t cookie"
  50. .Ft const char *
  51. .Fn magic_error "magic_t cookie"
  52. .Ft int
  53. .Fn magic_errno "magic_t cookie"
  54. .Ft const char *
  55. .Fn magic_descriptor "magic_t cookie" "int fd"
  56. .Ft const char *
  57. .Fn magic_file "magic_t cookie" "const char *filename"
  58. .Ft const char *
  59. .Fn magic_buffer "magic_t cookie" "const void *buffer" "size_t length"
  60. .Ft int
  61. .Fn magic_setflags "magic_t cookie" "int flags"
  62. .Ft int
  63. .Fn magic_check "magic_t cookie" "const char *filename"
  64. .Ft int
  65. .Fn magic_compile "magic_t cookie" "const char *filename"
  66. .Ft int
  67. .Fn magic_list "magic_t cookie" "const char *filename"
  68. .Ft int
  69. .Fn magic_load "magic_t cookie" "const char *filename"
  70. .Sh DESCRIPTION
  71. These functions
  72. operate on the magic database file
  73. which is described
  74. in
  75. .Xr magic __FSECTION__ .
  76. .Pp
  77. The function
  78. .Fn magic_open
  79. creates a magic cookie pointer and returns it.
  80. It returns
  81. .Dv NULL
  82. if there was an error allocating the magic cookie.
  83. The
  84. .Ar flags
  85. argument specifies how the other magic functions should behave:
  86. .Bl -tag -width MAGIC_COMPRESS
  87. .It Dv MAGIC_NONE
  88. No special handling.
  89. .It Dv MAGIC_DEBUG
  90. Print debugging messages to stderr.
  91. .It Dv MAGIC_SYMLINK
  92. If the file queried is a symlink, follow it.
  93. .It Dv MAGIC_COMPRESS
  94. If the file is compressed, unpack it and look at the contents.
  95. .It Dv MAGIC_DEVICES
  96. If the file is a block or character special device, then open the device
  97. and try to look in its contents.
  98. .It Dv MAGIC_MIME_TYPE
  99. Return a MIME type string, instead of a textual description.
  100. .It Dv MAGIC_MIME_ENCODING
  101. Return a MIME encoding, instead of a textual description.
  102. .It Dv MAGIC_MIME
  103. A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
  104. .It Dv MAGIC_CONTINUE
  105. Return all matches, not just the first.
  106. .It Dv MAGIC_CHECK
  107. Check the magic database for consistency and print warnings to stderr.
  108. .It Dv MAGIC_PRESERVE_ATIME
  109. On systems that support
  110. .Xr utime 3
  111. or
  112. .Xr utimes 2 ,
  113. attempt to preserve the access time of files analysed.
  114. .It Dv MAGIC_RAW
  115. Don't translate unprintable characters to a \eooo octal representation.
  116. .It Dv MAGIC_ERROR
  117. Treat operating system errors while trying to open files and follow symlinks
  118. as real errors, instead of printing them in the magic buffer.
  119. .It Dv MAGIC_APPLE
  120. Return the Apple creator and type.
  121. .It Dv MAGIC_NO_CHECK_APPTYPE
  122. Don't check for
  123. .Dv EMX
  124. application type (only on EMX).
  125. .It Dv MAGIC_NO_CHECK_CDF
  126. Don't get extra information on MS Composite Document Files.
  127. .It Dv MAGIC_NO_CHECK_COMPRESS
  128. Don't look inside compressed files.
  129. .It Dv MAGIC_NO_CHECK_ELF
  130. Don't print ELF details.
  131. .It Dv MAGIC_NO_CHECK_ENCODING
  132. Don't check text encodings.
  133. .It Dv MAGIC_NO_CHECK_SOFT
  134. Don't consult magic files.
  135. .It Dv MAGIC_NO_CHECK_TAR
  136. Don't examine tar files.
  137. .It Dv MAGIC_NO_CHECK_TEXT
  138. Don't check for various types of text files.
  139. .It Dv MAGIC_NO_CHECK_TOKENS
  140. Don't look for known tokens inside ascii files.
  141. .El
  142. .Pp
  143. The
  144. .Fn magic_close
  145. function closes the
  146. .Xr magic __FSECTION__
  147. database and deallocates any resources used.
  148. .Pp
  149. The
  150. .Fn magic_error
  151. function returns a textual explanation of the last error, or
  152. .Dv NULL
  153. if there was no error.
  154. .Pp
  155. The
  156. .Fn magic_errno
  157. function returns the last operating system error number
  158. .Pq Xr errno 2
  159. that was encountered by a system call.
  160. .Pp
  161. The
  162. .Fn magic_file
  163. function returns a textual description of the contents of the
  164. .Ar filename
  165. argument, or
  166. .Dv NULL
  167. if an error occurred.
  168. If the
  169. .Ar filename
  170. is
  171. .Dv NULL ,
  172. then stdin is used.
  173. .Pp
  174. The
  175. .Fn magic_descriptor
  176. function returns a textual description of the contents of the
  177. .Ar fd
  178. argument, or
  179. .Dv NULL
  180. if an error occurred.
  181. .Pp
  182. The
  183. .Fn magic_buffer
  184. function returns a textual description of the contents of the
  185. .Ar buffer
  186. argument with
  187. .Ar length
  188. bytes size.
  189. .Pp
  190. The
  191. .Fn magic_setflags
  192. function sets the
  193. .Ar flags
  194. described above.
  195. Note that using both MIME flags together can also
  196. return extra information on the charset.
  197. .Pp
  198. The
  199. .Fn magic_check
  200. function can be used to check the validity of entries in the colon
  201. separated database files passed in as
  202. .Ar filename ,
  203. or
  204. .Dv NULL
  205. for the default database.
  206. It returns 0 on success and \-1 on failure.
  207. .Pp
  208. The
  209. .Fn magic_compile
  210. function can be used to compile the the colon
  211. separated list of database files passed in as
  212. .Ar filename ,
  213. or
  214. .Dv NULL
  215. for the default database.
  216. It returns 0 on success and \-1 on failure.
  217. The compiled files created are named from the
  218. .Xr basename 1
  219. of each file argument with
  220. .Dq .mgc
  221. appended to it.
  222. .Pp
  223. The
  224. .Fn magic_list
  225. function dumps all magic entries in a human readable format,
  226. dumping first the entries that are matched against binary files and then the
  227. ones that match text files.
  228. It takes and optional
  229. .Fa filename
  230. argument which is a colon separated list of database files, or
  231. .Dv NULL
  232. for the default database.
  233. .Pp
  234. The
  235. .Fn magic_load
  236. function must be used to load the the colon
  237. separated list of database files passed in as
  238. .Ar filename ,
  239. or
  240. .Dv NULL
  241. for the default database file before any magic queries can performed.
  242. .Pp
  243. The default database file is named by the MAGIC environment variable.
  244. If that variable is not set, the default database file name is __MAGIC__.
  245. .Fn magic_load
  246. adds
  247. .Dq .mgc
  248. to the database filename as appropriate.
  249. .Sh RETURN VALUES
  250. The function
  251. .Fn magic_open
  252. returns a magic cookie on success and
  253. .Dv NULL
  254. on failure setting errno to an appropriate value.
  255. It will set errno to
  256. .Er EINVAL
  257. if an unsupported value for flags was given.
  258. The
  259. .Fn magic_list ,
  260. .Fn magic_load ,
  261. .Fn magic_compile ,
  262. and
  263. .Fn magic_check
  264. functions return 0 on success and \-1 on failure.
  265. The
  266. .Fn magic_buffer ,
  267. .Fn magic_getpath ,
  268. and
  269. .Fn magic_file ,
  270. functions return a string on success and
  271. .Dv NULL
  272. on failure.
  273. The
  274. .Fn magic_error
  275. function returns a textual description of the errors of the above
  276. functions, or
  277. .Dv NULL
  278. if there was no error.
  279. Finally,
  280. .Fn magic_setflags
  281. returns \-1 on systems that don't support
  282. .Xr utime 3 ,
  283. or
  284. .Xr utimes 2
  285. when
  286. .Dv MAGIC_PRESERVE_ATIME
  287. is set.
  288. .Sh FILES
  289. .Bl -tag -width __MAGIC__.mgc -compact
  290. .It Pa __MAGIC__
  291. The non-compiled default magic database.
  292. .It Pa __MAGIC__.mgc
  293. The compiled default magic database.
  294. .El
  295. .Sh SEE ALSO
  296. .Xr file __CSECTION__ ,
  297. .Xr magic __FSECTION__
  298. .Sh AUTHORS
  299. .An M\(oans Rullg\(oard
  300. Initial libmagic implementation, and configuration.
  301. .An Christos Zoulas
  302. API cleanup, error code and allocation handling.