libmagic.man 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. .\" $File: libmagic.man,v 1.29 2014/08/04 06:19:44 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 August 4, 2014
  29. .Dt LIBMAGIC 3
  30. .Os
  31. .Sh NAME
  32. .Nm magic_open ,
  33. .Nm magic_close ,
  34. .Nm magic_error ,
  35. .Nm magic_errno ,
  36. .Nm magic_descriptor ,
  37. .Nm magic_buffer ,
  38. .Nm magic_setflags ,
  39. .Nm magic_check ,
  40. .Nm magic_compile ,
  41. .Nm magic_list ,
  42. .Nm magic_load ,
  43. .Nm magic_version
  44. .Nd Magic number recognition library
  45. .Sh LIBRARY
  46. .Lb libmagic
  47. .Sh SYNOPSIS
  48. .In magic.h
  49. .Ft magic_t
  50. .Fn magic_open "int flags"
  51. .Ft void
  52. .Fn magic_close "magic_t cookie"
  53. .Ft const char *
  54. .Fn magic_error "magic_t cookie"
  55. .Ft int
  56. .Fn magic_errno "magic_t cookie"
  57. .Ft const char *
  58. .Fn magic_descriptor "magic_t cookie" "int fd"
  59. .Ft const char *
  60. .Fn magic_file "magic_t cookie" "const char *filename"
  61. .Ft const char *
  62. .Fn magic_buffer "magic_t cookie" "const void *buffer" "size_t length"
  63. .Ft int
  64. .Fn magic_setflags "magic_t cookie" "int flags"
  65. .Ft int
  66. .Fn magic_check "magic_t cookie" "const char *filename"
  67. .Ft int
  68. .Fn magic_compile "magic_t cookie" "const char *filename"
  69. .Ft int
  70. .Fn magic_list "magic_t cookie" "const char *filename"
  71. .Ft int
  72. .Fn magic_load "magic_t cookie" "const char *filename"
  73. .Ft int
  74. .Fn magic_load_buffers "magic_t cookie" "void **buffers" "size_t *sizes" "size_t nbuffers"
  75. .Ft int
  76. .Fn magic_version "void"
  77. .Sh DESCRIPTION
  78. These functions
  79. operate on the magic database file
  80. which is described
  81. in
  82. .Xr magic __FSECTION__ .
  83. .Pp
  84. The function
  85. .Fn magic_open
  86. creates a magic cookie pointer and returns it.
  87. It returns
  88. .Dv NULL
  89. if there was an error allocating the magic cookie.
  90. The
  91. .Ar flags
  92. argument specifies how the other magic functions should behave:
  93. .Bl -tag -width MAGIC_COMPRESS
  94. .It Dv MAGIC_NONE
  95. No special handling.
  96. .It Dv MAGIC_DEBUG
  97. Print debugging messages to stderr.
  98. .It Dv MAGIC_SYMLINK
  99. If the file queried is a symlink, follow it.
  100. .It Dv MAGIC_COMPRESS
  101. If the file is compressed, unpack it and look at the contents.
  102. .It Dv MAGIC_DEVICES
  103. If the file is a block or character special device, then open the device
  104. and try to look in its contents.
  105. .It Dv MAGIC_MIME_TYPE
  106. Return a MIME type string, instead of a textual description.
  107. .It Dv MAGIC_MIME_ENCODING
  108. Return a MIME encoding, instead of a textual description.
  109. .It Dv MAGIC_MIME
  110. A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
  111. .It Dv MAGIC_CONTINUE
  112. Return all matches, not just the first.
  113. .It Dv MAGIC_CHECK
  114. Check the magic database for consistency and print warnings to stderr.
  115. .It Dv MAGIC_PRESERVE_ATIME
  116. On systems that support
  117. .Xr utime 3
  118. or
  119. .Xr utimes 2 ,
  120. attempt to preserve the access time of files analysed.
  121. .It Dv MAGIC_RAW
  122. Don't translate unprintable characters to a \eooo octal representation.
  123. .It Dv MAGIC_ERROR
  124. Treat operating system errors while trying to open files and follow symlinks
  125. as real errors, instead of printing them in the magic buffer.
  126. .It Dv MAGIC_APPLE
  127. Return the Apple creator and type.
  128. .It Dv MAGIC_NO_CHECK_APPTYPE
  129. Don't check for
  130. .Dv EMX
  131. application type (only on EMX).
  132. .It Dv MAGIC_NO_CHECK_CDF
  133. Don't get extra information on MS Composite Document Files.
  134. .It Dv MAGIC_NO_CHECK_COMPRESS
  135. Don't look inside compressed files.
  136. .It Dv MAGIC_NO_CHECK_ELF
  137. Don't print ELF details.
  138. .It Dv MAGIC_NO_CHECK_ENCODING
  139. Don't check text encodings.
  140. .It Dv MAGIC_NO_CHECK_SOFT
  141. Don't consult magic files.
  142. .It Dv MAGIC_NO_CHECK_TAR
  143. Don't examine tar files.
  144. .It Dv MAGIC_NO_CHECK_TEXT
  145. Don't check for various types of text files.
  146. .It Dv MAGIC_NO_CHECK_TOKENS
  147. Don't look for known tokens inside ascii files.
  148. .El
  149. .Pp
  150. The
  151. .Fn magic_close
  152. function closes the
  153. .Xr magic __FSECTION__
  154. database and deallocates any resources used.
  155. .Pp
  156. The
  157. .Fn magic_error
  158. function returns a textual explanation of the last error, or
  159. .Dv NULL
  160. if there was no error.
  161. .Pp
  162. The
  163. .Fn magic_errno
  164. function returns the last operating system error number
  165. .Pq Xr errno 2
  166. that was encountered by a system call.
  167. .Pp
  168. The
  169. .Fn magic_file
  170. function returns a textual description of the contents of the
  171. .Ar filename
  172. argument, or
  173. .Dv NULL
  174. if an error occurred.
  175. If the
  176. .Ar filename
  177. is
  178. .Dv NULL ,
  179. then stdin is used.
  180. .Pp
  181. The
  182. .Fn magic_descriptor
  183. function returns a textual description of the contents of the
  184. .Ar fd
  185. argument, or
  186. .Dv NULL
  187. if an error occurred.
  188. .Pp
  189. The
  190. .Fn magic_buffer
  191. function returns a textual description of the contents of the
  192. .Ar buffer
  193. argument with
  194. .Ar length
  195. bytes size.
  196. .Pp
  197. The
  198. .Fn magic_setflags
  199. function sets the
  200. .Ar flags
  201. described above.
  202. Note that using both MIME flags together can also
  203. return extra information on the charset.
  204. .Pp
  205. The
  206. .Fn magic_check
  207. function can be used to check the validity of entries in the colon
  208. separated database files passed in as
  209. .Ar filename ,
  210. or
  211. .Dv NULL
  212. for the default database.
  213. It returns 0 on success and \-1 on failure.
  214. .Pp
  215. The
  216. .Fn magic_compile
  217. function can be used to compile the the colon
  218. separated list of database files passed in as
  219. .Ar filename ,
  220. or
  221. .Dv NULL
  222. for the default database.
  223. It returns 0 on success and \-1 on failure.
  224. The compiled files created are named from the
  225. .Xr basename 1
  226. of each file argument with
  227. .Dq .mgc
  228. appended to it.
  229. .Pp
  230. The
  231. .Fn magic_list
  232. function dumps all magic entries in a human readable format,
  233. dumping first the entries that are matched against binary files and then the
  234. ones that match text files.
  235. It takes and optional
  236. .Fa filename
  237. argument which is a colon separated list of database files, or
  238. .Dv NULL
  239. for the default database.
  240. .Pp
  241. The
  242. .Fn magic_load
  243. function must be used to load the the colon
  244. separated list of database files passed in as
  245. .Ar filename ,
  246. or
  247. .Dv NULL
  248. for the default database file before any magic queries can performed.
  249. .Pp
  250. The default database file is named by the MAGIC environment variable.
  251. If that variable is not set, the default database file name is __MAGIC__.
  252. .Fn magic_load
  253. adds
  254. .Dq .mgc
  255. to the database filename as appropriate.
  256. .Pp
  257. The
  258. .Fn magic_load_buffers
  259. function takes an array of size
  260. .Fa nbuffers
  261. of
  262. .Fa buffers
  263. with a respective size for each in the array of
  264. .Fa sizes
  265. loaded with the contents of the magic databases from the filesystem.
  266. This function can be used in environment where the magic library does
  267. not have direct access to the filesystem, but can access the magic
  268. database via shared memory or other IPC means.
  269. .Pp
  270. The
  271. .Fn magic_version
  272. command returns the version number of this library which is compiled into
  273. the shared library using the constant
  274. .Dv MAGIC_VERSION
  275. from
  276. .In magic.h .
  277. This can be used by client programs to verify that the version they compile
  278. against is the same as the version that they run against.
  279. .Sh RETURN VALUES
  280. The function
  281. .Fn magic_open
  282. returns a magic cookie on success and
  283. .Dv NULL
  284. on failure setting errno to an appropriate value.
  285. It will set errno to
  286. .Er EINVAL
  287. if an unsupported value for flags was given.
  288. The
  289. .Fn magic_list ,
  290. .Fn magic_load ,
  291. .Fn magic_compile ,
  292. and
  293. .Fn magic_check
  294. functions return 0 on success and \-1 on failure.
  295. The
  296. .Fn magic_buffer ,
  297. .Fn magic_getpath ,
  298. and
  299. .Fn magic_file ,
  300. functions return a string on success and
  301. .Dv NULL
  302. on failure.
  303. The
  304. .Fn magic_error
  305. function returns a textual description of the errors of the above
  306. functions, or
  307. .Dv NULL
  308. if there was no error.
  309. The
  310. .Fn magic_version
  311. always returns the version number of the library.
  312. Finally,
  313. .Fn magic_setflags
  314. returns \-1 on systems that don't support
  315. .Xr utime 3 ,
  316. or
  317. .Xr utimes 2
  318. when
  319. .Dv MAGIC_PRESERVE_ATIME
  320. is set.
  321. .Sh FILES
  322. .Bl -tag -width __MAGIC__.mgc -compact
  323. .It Pa __MAGIC__
  324. The non-compiled default magic database.
  325. .It Pa __MAGIC__.mgc
  326. The compiled default magic database.
  327. .El
  328. .Sh SEE ALSO
  329. .Xr file __CSECTION__ ,
  330. .Xr magic __FSECTION__
  331. .Sh AUTHORS
  332. .An M\(oans Rullg\(oard
  333. Initial libmagic implementation, and configuration.
  334. .An Christos Zoulas
  335. API cleanup, error code and allocation handling.