xref: /dflybsd-src/contrib/file/doc/libmagic.man (revision a96e001b75d2a5877ad6fcec5487cf480f9d827a)
1*a96e001bSPeter Avalos.\" $File: libmagic.man,v 1.24 2011/05/13 22:11:44 christos Exp $
2327e51cbSPeter Avalos.\"
3327e51cbSPeter Avalos.\" Copyright (c) Christos Zoulas 2003.
4327e51cbSPeter Avalos.\" All Rights Reserved.
5327e51cbSPeter Avalos.\"
6327e51cbSPeter Avalos.\" Redistribution and use in source and binary forms, with or without
7327e51cbSPeter Avalos.\" modification, are permitted provided that the following conditions
8327e51cbSPeter Avalos.\" are met:
9327e51cbSPeter Avalos.\" 1. Redistributions of source code must retain the above copyright
10327e51cbSPeter Avalos.\"    notice immediately at the beginning of the file, without modification,
11327e51cbSPeter Avalos.\"    this list of conditions, and the following disclaimer.
12327e51cbSPeter Avalos.\" 2. Redistributions in binary form must reproduce the above copyright
13327e51cbSPeter Avalos.\"    notice, this list of conditions and the following disclaimer in the
14327e51cbSPeter Avalos.\"    documentation and/or other materials provided with the distribution.
15327e51cbSPeter Avalos.\"
16327e51cbSPeter Avalos.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17327e51cbSPeter Avalos.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18327e51cbSPeter Avalos.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19327e51cbSPeter Avalos.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20327e51cbSPeter Avalos.\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21327e51cbSPeter Avalos.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22327e51cbSPeter Avalos.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23327e51cbSPeter Avalos.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24327e51cbSPeter Avalos.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25327e51cbSPeter Avalos.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26327e51cbSPeter Avalos.\" SUCH DAMAGE.
27327e51cbSPeter Avalos.\"
28e4d4ce0cSPeter Avalos.Dd January 14, 2011
29f72f8299SJan Lentfer.Dt LIBMAGIC 3
30327e51cbSPeter Avalos.Os
31327e51cbSPeter Avalos.Sh NAME
32327e51cbSPeter Avalos.Nm magic_open ,
33327e51cbSPeter Avalos.Nm magic_close ,
34327e51cbSPeter Avalos.Nm magic_error ,
35e4d4ce0cSPeter Avalos.Nm magic_descriptor ,
36327e51cbSPeter Avalos.Nm magic_buffer ,
37327e51cbSPeter Avalos.Nm magic_setflags ,
38327e51cbSPeter Avalos.Nm magic_check ,
39327e51cbSPeter Avalos.Nm magic_compile ,
40327e51cbSPeter Avalos.Nm magic_load
41*a96e001bSPeter Avalos.Nd Magic number recognition library
42327e51cbSPeter Avalos.Sh LIBRARY
43327e51cbSPeter Avalos.Lb libmagic
44327e51cbSPeter Avalos.Sh SYNOPSIS
45327e51cbSPeter Avalos.In magic.h
46327e51cbSPeter Avalos.Ft magic_t
47327e51cbSPeter Avalos.Fn magic_open "int flags"
48327e51cbSPeter Avalos.Ft void
49327e51cbSPeter Avalos.Fn magic_close "magic_t cookie"
50327e51cbSPeter Avalos.Ft const char *
51327e51cbSPeter Avalos.Fn magic_error "magic_t cookie"
52327e51cbSPeter Avalos.Ft int
53327e51cbSPeter Avalos.Fn magic_errno "magic_t cookie"
54327e51cbSPeter Avalos.Ft const char *
55e4d4ce0cSPeter Avalos.Fn magic_descriptor "magic_t cookie, "int fd"
56e4d4ce0cSPeter Avalos.Ft const char *
57327e51cbSPeter Avalos.Fn magic_file "magic_t cookie, const char *filename"
58327e51cbSPeter Avalos.Ft const char *
59*a96e001bSPeter Avalos.Fn magic_buffer "magic_t cookie" "const void *buffer" "size_t length"
60327e51cbSPeter Avalos.Ft int
61*a96e001bSPeter Avalos.Fn magic_setflags "magic_t cookie" "int flags"
62327e51cbSPeter Avalos.Ft int
63*a96e001bSPeter Avalos.Fn magic_check "magic_t cookie" "const char *filename"
64327e51cbSPeter Avalos.Ft int
65*a96e001bSPeter Avalos.Fn magic_compile "magic_t cookie" "const char *filename"
66327e51cbSPeter Avalos.Ft int
67*a96e001bSPeter Avalos.Fn magic_load "magic_t cookie" "const char *filename"
68327e51cbSPeter Avalos.Sh DESCRIPTION
69327e51cbSPeter AvalosThese functions
70327e51cbSPeter Avalosoperate on the magic database file
71327e51cbSPeter Avaloswhich is described
72327e51cbSPeter Avalosin
73327e51cbSPeter Avalos.Xr magic __FSECTION__ .
74327e51cbSPeter Avalos.Pp
75327e51cbSPeter AvalosThe function
76327e51cbSPeter Avalos.Fn magic_open
77*a96e001bSPeter Avaloscreates a magic cookie pointer and returns it.
78*a96e001bSPeter AvalosIt returns
79*a96e001bSPeter Avalos.Dv NULL
80*a96e001bSPeter Avalosif there was an error allocating the magic cookie.
81*a96e001bSPeter AvalosThe
82327e51cbSPeter Avalos.Ar flags
83327e51cbSPeter Avalosargument specifies how the other magic functions should behave:
84327e51cbSPeter Avalos.Bl -tag -width MAGIC_COMPRESS
85327e51cbSPeter Avalos.It Dv MAGIC_NONE
86327e51cbSPeter AvalosNo special handling.
87327e51cbSPeter Avalos.It Dv MAGIC_DEBUG
88327e51cbSPeter AvalosPrint debugging messages to stderr.
89327e51cbSPeter Avalos.It Dv MAGIC_SYMLINK
90327e51cbSPeter AvalosIf the file queried is a symlink, follow it.
91327e51cbSPeter Avalos.It Dv MAGIC_COMPRESS
92327e51cbSPeter AvalosIf the file is compressed, unpack it and look at the contents.
93327e51cbSPeter Avalos.It Dv MAGIC_DEVICES
94327e51cbSPeter AvalosIf the file is a block or character special device, then open the device
95327e51cbSPeter Avalosand try to look in its contents.
96327e51cbSPeter Avalos.It Dv MAGIC_MIME_TYPE
97327e51cbSPeter AvalosReturn a MIME type string, instead of a textual description.
98327e51cbSPeter Avalos.It Dv MAGIC_MIME_ENCODING
99327e51cbSPeter AvalosReturn a MIME encoding, instead of a textual description.
100e4d4ce0cSPeter Avalos.It Dv MAGIC_MIME
101e4d4ce0cSPeter AvalosA shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
102327e51cbSPeter Avalos.It Dv MAGIC_CONTINUE
103327e51cbSPeter AvalosReturn all matches, not just the first.
104327e51cbSPeter Avalos.It Dv MAGIC_CHECK
105327e51cbSPeter AvalosCheck the magic database for consistency and print warnings to stderr.
106327e51cbSPeter Avalos.It Dv MAGIC_PRESERVE_ATIME
107327e51cbSPeter AvalosOn systems that support
108*a96e001bSPeter Avalos.Xr utime 3
109327e51cbSPeter Avalosor
110327e51cbSPeter Avalos.Xr utimes 2 ,
111e4d4ce0cSPeter Avalosattempt to preserve the access time of files analysed.
112327e51cbSPeter Avalos.It Dv MAGIC_RAW
113327e51cbSPeter AvalosDon't translate unprintable characters to a \eooo octal representation.
114327e51cbSPeter Avalos.It Dv MAGIC_ERROR
115327e51cbSPeter AvalosTreat operating system errors while trying to open files and follow symlinks
116327e51cbSPeter Avalosas real errors, instead of printing them in the magic buffer.
117e4d4ce0cSPeter Avalos.It Dv MAGIC_APPLE
118e4d4ce0cSPeter AvalosReturn the Apple creator and type.
119327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_APPTYPE
120e4d4ce0cSPeter AvalosDon't check for
121327e51cbSPeter Avalos.Dv EMX
122327e51cbSPeter Avalosapplication type (only on EMX).
123e4d4ce0cSPeter Avalos.It Dv MAGIC_NO_CHECK_CDF
124e4d4ce0cSPeter AvalosDon't get extra information on MS Composite Document Files.
125327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_COMPRESS
126e4d4ce0cSPeter AvalosDon't look inside compressed files.
127327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_ELF
128e4d4ce0cSPeter AvalosDon't print ELF details.
129e4d4ce0cSPeter Avalos.It Dv NO_CHECK_ENCODING
130e4d4ce0cSPeter AvalosDon't check text encodings.
131327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_SOFT
132327e51cbSPeter AvalosDon't consult magic files.
133327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_TAR
134327e51cbSPeter AvalosDon't examine tar files.
135e4d4ce0cSPeter Avalos.It Dv MAGIC_NO_CHECK_TEXT
136e4d4ce0cSPeter AvalosDon't check for various types of text files.
137327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_TOKENS
138327e51cbSPeter AvalosDon't look for known tokens inside ascii files.
139327e51cbSPeter Avalos.El
140327e51cbSPeter Avalos.Pp
141327e51cbSPeter AvalosThe
142327e51cbSPeter Avalos.Fn magic_close
143327e51cbSPeter Avalosfunction closes the
144327e51cbSPeter Avalos.Xr magic __FSECTION__
145327e51cbSPeter Avalosdatabase and deallocates any resources used.
146327e51cbSPeter Avalos.Pp
147327e51cbSPeter AvalosThe
148327e51cbSPeter Avalos.Fn magic_error
149*a96e001bSPeter Avalosfunction returns a textual explanation of the last error, or
150*a96e001bSPeter Avalos.Dv NULL
151*a96e001bSPeter Avalosif there was no error.
152327e51cbSPeter Avalos.Pp
153327e51cbSPeter AvalosThe
154327e51cbSPeter Avalos.Fn magic_errno
155327e51cbSPeter Avalosfunction returns the last operating system error number
156327e51cbSPeter Avalos.Pq Xr errno 2
157327e51cbSPeter Avalosthat was encountered by a system call.
158327e51cbSPeter Avalos.Pp
159327e51cbSPeter AvalosThe
160327e51cbSPeter Avalos.Fn magic_file
161327e51cbSPeter Avalosfunction returns a textual description of the contents of the
162327e51cbSPeter Avalos.Ar filename
163*a96e001bSPeter Avalosargument, or
164*a96e001bSPeter Avalos.Dv NULL
165*a96e001bSPeter Avalosif an error occurred.
166327e51cbSPeter AvalosIf the
167327e51cbSPeter Avalos.Ar filename
168*a96e001bSPeter Avalosis
169*a96e001bSPeter Avalos.Dv NULL ,
170*a96e001bSPeter Avalosthen stdin is used.
171327e51cbSPeter Avalos.Pp
172327e51cbSPeter AvalosThe
173e4d4ce0cSPeter Avalos.Fn magic_descriptor
174e4d4ce0cSPeter Avalosfunction returns a textual description of the contents of the
175e4d4ce0cSPeter Avalos.Ar fd
176*a96e001bSPeter Avalosargument, or
177*a96e001bSPeter Avalos.Dv NULL
178*a96e001bSPeter Avalosif an error occurred.
179e4d4ce0cSPeter Avalos.Pp
180e4d4ce0cSPeter AvalosThe
181327e51cbSPeter Avalos.Fn magic_buffer
182327e51cbSPeter Avalosfunction returns a textual description of the contents of the
183327e51cbSPeter Avalos.Ar buffer
184327e51cbSPeter Avalosargument with
185327e51cbSPeter Avalos.Ar length
186327e51cbSPeter Avalosbytes size.
187327e51cbSPeter Avalos.Pp
188327e51cbSPeter AvalosThe
189327e51cbSPeter Avalos.Fn magic_setflags
19079343712SPeter Avalosfunction sets the
191327e51cbSPeter Avalos.Ar flags
192*a96e001bSPeter Avalosdescribed above.
193*a96e001bSPeter AvalosNote that using both MIME flags together can also
194327e51cbSPeter Avalosreturn extra information on the charset.
195327e51cbSPeter Avalos.Pp
196327e51cbSPeter AvalosThe
197327e51cbSPeter Avalos.Fn magic_check
198327e51cbSPeter Avalosfunction can be used to check the validity of entries in the colon
199327e51cbSPeter Avalosseparated database files passed in as
200327e51cbSPeter Avalos.Ar filename ,
201*a96e001bSPeter Avalosor
202*a96e001bSPeter Avalos.Dv NULL
203*a96e001bSPeter Avalosfor the default database.
204*a96e001bSPeter AvalosIt returns 0 on success and \-1 on failure.
205327e51cbSPeter Avalos.Pp
206327e51cbSPeter AvalosThe
207327e51cbSPeter Avalos.Fn magic_compile
208327e51cbSPeter Avalosfunction can be used to compile the the colon
209327e51cbSPeter Avalosseparated list of database files passed in as
210327e51cbSPeter Avalos.Ar filename ,
211*a96e001bSPeter Avalosor
212*a96e001bSPeter Avalos.Dv NULL
213*a96e001bSPeter Avalosfor the default database.
214*a96e001bSPeter AvalosIt returns 0 on success and \-1 on failure.
215*a96e001bSPeter AvalosThe compiled files created are named from the
216327e51cbSPeter Avalos.Xr basename 1
21779343712SPeter Avalosof each file argument with
21879343712SPeter Avalos.Dq .mgc
21979343712SPeter Avalosappended to it.
220327e51cbSPeter Avalos.Pp
221327e51cbSPeter AvalosThe
222327e51cbSPeter Avalos.Fn magic_load
223327e51cbSPeter Avalosfunction must be used to load the the colon
224327e51cbSPeter Avalosseparated list of database files passed in as
225327e51cbSPeter Avalos.Ar filename ,
226*a96e001bSPeter Avalosor
227*a96e001bSPeter Avalos.Dv NULL
228*a96e001bSPeter Avalosfor the default database file before any magic queries can performed.
229327e51cbSPeter Avalos.Pp
230*a96e001bSPeter AvalosThe default database file is named by the MAGIC environment variable.
231*a96e001bSPeter AvalosIf that variable is not set, the default database file name is __MAGIC__.
232327e51cbSPeter Avalos.Fn magic_load
23379343712SPeter Avalosadds
23479343712SPeter Avalos.Dq .mgc
23579343712SPeter Avalosto the database filename as appropriate.
236327e51cbSPeter Avalos.Sh RETURN VALUES
237327e51cbSPeter AvalosThe function
238327e51cbSPeter Avalos.Fn magic_open
239*a96e001bSPeter Avalosreturns a magic cookie on success and
240*a96e001bSPeter Avalos.Dv NULL
241*a96e001bSPeter Avaloson failure setting errno to an appropriate value.
242*a96e001bSPeter AvalosIt will set errno to
243*a96e001bSPeter Avalos.Er EINVAL
244*a96e001bSPeter Avalosif an unsupported value for flags was given.
245327e51cbSPeter AvalosThe
246327e51cbSPeter Avalos.Fn magic_load ,
247327e51cbSPeter Avalos.Fn magic_compile ,
248327e51cbSPeter Avalosand
249327e51cbSPeter Avalos.Fn magic_check
250*a96e001bSPeter Avalosfunctions return 0 on success and \-1 on failure.
251327e51cbSPeter AvalosThe
252327e51cbSPeter Avalos.Fn magic_file ,
253327e51cbSPeter Avalosand
254327e51cbSPeter Avalos.Fn magic_buffer
255*a96e001bSPeter Avalosfunctions return a string on success and
256*a96e001bSPeter Avalos.Dv NULL
257*a96e001bSPeter Avaloson failure.
258*a96e001bSPeter AvalosThe
259327e51cbSPeter Avalos.Fn magic_error
260327e51cbSPeter Avalosfunction returns a textual description of the errors of the above
261*a96e001bSPeter Avalosfunctions, or
262*a96e001bSPeter Avalos.Dv NULL
263*a96e001bSPeter Avalosif there was no error.
264327e51cbSPeter AvalosFinally,
265327e51cbSPeter Avalos.Fn magic_setflags
266*a96e001bSPeter Avalosreturns \-1 on systems that don't support
267*a96e001bSPeter Avalos.Xr utime 3 ,
268327e51cbSPeter Avalosor
269327e51cbSPeter Avalos.Xr utimes 2
270327e51cbSPeter Avaloswhen
271327e51cbSPeter Avalos.Dv MAGIC_PRESERVE_ATIME
272327e51cbSPeter Avalosis set.
273327e51cbSPeter Avalos.Sh FILES
27479343712SPeter Avalos.Bl -tag -width __MAGIC__.mgc -compact
275327e51cbSPeter Avalos.It Pa __MAGIC__
276327e51cbSPeter AvalosThe non-compiled default magic database.
277327e51cbSPeter Avalos.It Pa __MAGIC__.mgc
278327e51cbSPeter AvalosThe compiled default magic database.
279327e51cbSPeter Avalos.El
280327e51cbSPeter Avalos.Sh SEE ALSO
281327e51cbSPeter Avalos.Xr file __CSECTION__ ,
282327e51cbSPeter Avalos.Xr magic __FSECTION__
283327e51cbSPeter Avalos.Sh AUTHORS
284*a96e001bSPeter Avalos.An M\(oans Rullg\(oard
285*a96e001bSPeter AvalosInitial libmagic implementation, and configuration.
286*a96e001bSPeter Avalos.An Christos Zoulas
287*a96e001bSPeter AvalosAPI cleanup, error code and allocation handling.
288