libmagic.man 8.4 KB

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