libmagic.man 7.3 KB

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