2012-04-13 23:30:38 +00:00
|
|
|
.\" $File: libmagic.man,v 1.26 2011/12/19 17:49:31 christos Exp $
|
2004-08-09 08:45:41 +00:00
|
|
|
.\"
|
|
|
|
.\" Copyright (c) Christos Zoulas 2003.
|
|
|
|
.\" All Rights Reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice immediately at the beginning of the file, without modification,
|
|
|
|
.\" this list of conditions, and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
2011-10-06 06:01:12 +00:00
|
|
|
.\"
|
2004-08-09 08:45:41 +00:00
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
.\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
2012-04-13 23:30:38 +00:00
|
|
|
.Dd December 19, 2011
|
2011-10-06 06:01:12 +00:00
|
|
|
.Dt LIBMAGIC 3
|
2004-08-09 08:45:41 +00:00
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm magic_open ,
|
|
|
|
.Nm magic_close ,
|
|
|
|
.Nm magic_error ,
|
2011-10-06 06:01:12 +00:00
|
|
|
.Nm magic_descriptor ,
|
2004-08-09 08:45:41 +00:00
|
|
|
.Nm magic_buffer ,
|
|
|
|
.Nm magic_setflags ,
|
|
|
|
.Nm magic_check ,
|
|
|
|
.Nm magic_compile ,
|
|
|
|
.Nm magic_load
|
2011-10-06 06:01:12 +00:00
|
|
|
.Nd Magic number recognition library
|
2004-08-09 08:45:41 +00:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libmagic
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.In magic.h
|
|
|
|
.Ft magic_t
|
|
|
|
.Fn magic_open "int flags"
|
|
|
|
.Ft void
|
|
|
|
.Fn magic_close "magic_t cookie"
|
|
|
|
.Ft const char *
|
|
|
|
.Fn magic_error "magic_t cookie"
|
|
|
|
.Ft int
|
|
|
|
.Fn magic_errno "magic_t cookie"
|
|
|
|
.Ft const char *
|
2012-04-13 23:30:38 +00:00
|
|
|
.Fn magic_descriptor "magic_t cookie" "int fd"
|
2011-10-06 06:01:12 +00:00
|
|
|
.Ft const char *
|
2012-04-13 23:30:38 +00:00
|
|
|
.Fn magic_file "magic_t cookie" "const char *filename"
|
2004-08-09 08:45:41 +00:00
|
|
|
.Ft const char *
|
2011-10-06 06:01:12 +00:00
|
|
|
.Fn magic_buffer "magic_t cookie" "const void *buffer" "size_t length"
|
2004-08-09 08:45:41 +00:00
|
|
|
.Ft int
|
2011-10-06 06:01:12 +00:00
|
|
|
.Fn magic_setflags "magic_t cookie" "int flags"
|
2004-08-09 08:45:41 +00:00
|
|
|
.Ft int
|
2011-10-06 06:01:12 +00:00
|
|
|
.Fn magic_check "magic_t cookie" "const char *filename"
|
2004-08-09 08:45:41 +00:00
|
|
|
.Ft int
|
2011-10-06 06:01:12 +00:00
|
|
|
.Fn magic_compile "magic_t cookie" "const char *filename"
|
2004-08-09 08:45:41 +00:00
|
|
|
.Ft int
|
2012-04-13 23:30:38 +00:00
|
|
|
.Fn magic_list "magic_t cookie" "const char *filename"
|
|
|
|
.Ft int
|
2011-10-06 06:01:12 +00:00
|
|
|
.Fn magic_load "magic_t cookie" "const char *filename"
|
2004-08-09 08:45:41 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
These functions
|
|
|
|
operate on the magic database file
|
|
|
|
which is described
|
|
|
|
in
|
|
|
|
.Xr magic __FSECTION__ .
|
|
|
|
.Pp
|
|
|
|
The function
|
|
|
|
.Fn magic_open
|
2011-10-06 06:01:12 +00:00
|
|
|
creates a magic cookie pointer and returns it.
|
|
|
|
It returns
|
|
|
|
.Dv NULL
|
|
|
|
if there was an error allocating the magic cookie.
|
|
|
|
The
|
2004-08-09 08:45:41 +00:00
|
|
|
.Ar flags
|
|
|
|
argument specifies how the other magic functions should behave:
|
|
|
|
.Bl -tag -width MAGIC_COMPRESS
|
|
|
|
.It Dv MAGIC_NONE
|
|
|
|
No special handling.
|
|
|
|
.It Dv MAGIC_DEBUG
|
|
|
|
Print debugging messages to stderr.
|
|
|
|
.It Dv MAGIC_SYMLINK
|
|
|
|
If the file queried is a symlink, follow it.
|
|
|
|
.It Dv MAGIC_COMPRESS
|
|
|
|
If the file is compressed, unpack it and look at the contents.
|
|
|
|
.It Dv MAGIC_DEVICES
|
|
|
|
If the file is a block or character special device, then open the device
|
|
|
|
and try to look in its contents.
|
2008-01-13 20:35:16 +00:00
|
|
|
.It Dv MAGIC_MIME_TYPE
|
|
|
|
Return a MIME type string, instead of a textual description.
|
|
|
|
.It Dv MAGIC_MIME_ENCODING
|
|
|
|
Return a MIME encoding, instead of a textual description.
|
2011-10-06 06:01:12 +00:00
|
|
|
.It Dv MAGIC_MIME
|
|
|
|
A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
|
2004-08-09 08:45:41 +00:00
|
|
|
.It Dv MAGIC_CONTINUE
|
|
|
|
Return all matches, not just the first.
|
|
|
|
.It Dv MAGIC_CHECK
|
|
|
|
Check the magic database for consistency and print warnings to stderr.
|
|
|
|
.It Dv MAGIC_PRESERVE_ATIME
|
|
|
|
On systems that support
|
2011-10-06 06:01:12 +00:00
|
|
|
.Xr utime 3
|
2004-08-09 08:45:41 +00:00
|
|
|
or
|
|
|
|
.Xr utimes 2 ,
|
2011-10-06 06:01:12 +00:00
|
|
|
attempt to preserve the access time of files analysed.
|
2004-08-09 08:45:41 +00:00
|
|
|
.It Dv MAGIC_RAW
|
|
|
|
Don't translate unprintable characters to a \eooo octal representation.
|
|
|
|
.It Dv MAGIC_ERROR
|
|
|
|
Treat operating system errors while trying to open files and follow symlinks
|
|
|
|
as real errors, instead of printing them in the magic buffer.
|
2011-10-06 06:01:12 +00:00
|
|
|
.It Dv MAGIC_APPLE
|
|
|
|
Return the Apple creator and type.
|
2008-01-13 20:35:16 +00:00
|
|
|
.It Dv MAGIC_NO_CHECK_APPTYPE
|
2011-10-06 06:01:12 +00:00
|
|
|
Don't check for
|
2008-01-13 20:35:16 +00:00
|
|
|
.Dv EMX
|
|
|
|
application type (only on EMX).
|
2011-10-06 06:01:12 +00:00
|
|
|
.It Dv MAGIC_NO_CHECK_CDF
|
|
|
|
Don't get extra information on MS Composite Document Files.
|
2008-01-13 20:35:16 +00:00
|
|
|
.It Dv MAGIC_NO_CHECK_COMPRESS
|
2011-10-06 06:01:12 +00:00
|
|
|
Don't look inside compressed files.
|
2008-01-13 20:35:16 +00:00
|
|
|
.It Dv MAGIC_NO_CHECK_ELF
|
2011-10-06 06:01:12 +00:00
|
|
|
Don't print ELF details.
|
2012-04-13 23:30:38 +00:00
|
|
|
.It Dv MAGIC_NO_CHECK_ENCODING
|
2011-10-06 06:01:12 +00:00
|
|
|
Don't check text encodings.
|
2008-01-13 20:35:16 +00:00
|
|
|
.It Dv MAGIC_NO_CHECK_SOFT
|
|
|
|
Don't consult magic files.
|
|
|
|
.It Dv MAGIC_NO_CHECK_TAR
|
|
|
|
Don't examine tar files.
|
2011-10-06 06:01:12 +00:00
|
|
|
.It Dv MAGIC_NO_CHECK_TEXT
|
|
|
|
Don't check for various types of text files.
|
2008-01-13 20:35:16 +00:00
|
|
|
.It Dv MAGIC_NO_CHECK_TOKENS
|
|
|
|
Don't look for known tokens inside ascii files.
|
2004-08-09 08:45:41 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn magic_close
|
|
|
|
function closes the
|
|
|
|
.Xr magic __FSECTION__
|
|
|
|
database and deallocates any resources used.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn magic_error
|
2011-10-06 06:01:12 +00:00
|
|
|
function returns a textual explanation of the last error, or
|
|
|
|
.Dv NULL
|
|
|
|
if there was no error.
|
2004-08-09 08:45:41 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn magic_errno
|
2008-01-13 20:35:16 +00:00
|
|
|
function returns the last operating system error number
|
|
|
|
.Pq Xr errno 2
|
2004-08-09 08:45:41 +00:00
|
|
|
that was encountered by a system call.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn magic_file
|
|
|
|
function returns a textual description of the contents of the
|
|
|
|
.Ar filename
|
2011-10-06 06:01:12 +00:00
|
|
|
argument, or
|
|
|
|
.Dv NULL
|
|
|
|
if an error occurred.
|
2004-08-09 08:45:41 +00:00
|
|
|
If the
|
|
|
|
.Ar filename
|
2011-10-06 06:01:12 +00:00
|
|
|
is
|
|
|
|
.Dv NULL ,
|
|
|
|
then stdin is used.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn magic_descriptor
|
|
|
|
function returns a textual description of the contents of the
|
|
|
|
.Ar fd
|
|
|
|
argument, or
|
|
|
|
.Dv NULL
|
|
|
|
if an error occurred.
|
2004-08-09 08:45:41 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn magic_buffer
|
|
|
|
function returns a textual description of the contents of the
|
|
|
|
.Ar buffer
|
|
|
|
argument with
|
|
|
|
.Ar length
|
|
|
|
bytes size.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn magic_setflags
|
2011-10-06 06:01:12 +00:00
|
|
|
function sets the
|
2004-08-09 08:45:41 +00:00
|
|
|
.Ar flags
|
2011-10-06 06:01:12 +00:00
|
|
|
described above.
|
|
|
|
Note that using both MIME flags together can also
|
2008-01-13 20:35:16 +00:00
|
|
|
return extra information on the charset.
|
2004-08-09 08:45:41 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn magic_check
|
|
|
|
function can be used to check the validity of entries in the colon
|
|
|
|
separated database files passed in as
|
|
|
|
.Ar filename ,
|
2011-10-06 06:01:12 +00:00
|
|
|
or
|
|
|
|
.Dv NULL
|
|
|
|
for the default database.
|
|
|
|
It returns 0 on success and \-1 on failure.
|
2004-08-09 08:45:41 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn magic_compile
|
|
|
|
function can be used to compile the the colon
|
|
|
|
separated list of database files passed in as
|
|
|
|
.Ar filename ,
|
2011-10-06 06:01:12 +00:00
|
|
|
or
|
|
|
|
.Dv NULL
|
|
|
|
for the default database.
|
|
|
|
It returns 0 on success and \-1 on failure.
|
|
|
|
The compiled files created are named from the
|
2004-08-09 08:45:41 +00:00
|
|
|
.Xr basename 1
|
2009-01-02 02:57:16 +00:00
|
|
|
of each file argument with
|
|
|
|
.Dq .mgc
|
|
|
|
appended to it.
|
2004-08-09 08:45:41 +00:00
|
|
|
.Pp
|
|
|
|
The
|
2012-04-13 23:30:38 +00:00
|
|
|
.Fn magic_list
|
|
|
|
function dumps all magic entries in a human readable format,
|
|
|
|
dumping first the entries that are matched against binary files and then the
|
|
|
|
ones that match text files.
|
|
|
|
It takes and optional
|
|
|
|
.Fa filename
|
|
|
|
argument which is a colon separated list of database files, or
|
|
|
|
.Dv NULL
|
|
|
|
for the default database.
|
|
|
|
.Pp
|
|
|
|
The
|
2004-08-09 08:45:41 +00:00
|
|
|
.Fn magic_load
|
|
|
|
function must be used to load the the colon
|
|
|
|
separated list of database files passed in as
|
|
|
|
.Ar filename ,
|
2011-10-06 06:01:12 +00:00
|
|
|
or
|
|
|
|
.Dv NULL
|
|
|
|
for the default database file before any magic queries can performed.
|
2004-08-09 08:45:41 +00:00
|
|
|
.Pp
|
2011-10-06 06:01:12 +00:00
|
|
|
The default database file is named by the MAGIC environment variable.
|
|
|
|
If that variable is not set, the default database file name is __MAGIC__.
|
2004-08-09 08:45:41 +00:00
|
|
|
.Fn magic_load
|
2009-01-02 02:57:16 +00:00
|
|
|
adds
|
|
|
|
.Dq .mgc
|
|
|
|
to the database filename as appropriate.
|
2004-08-09 08:45:41 +00:00
|
|
|
.Sh RETURN VALUES
|
|
|
|
The function
|
|
|
|
.Fn magic_open
|
2011-10-06 06:01:12 +00:00
|
|
|
returns a magic cookie on success and
|
|
|
|
.Dv NULL
|
|
|
|
on failure setting errno to an appropriate value.
|
|
|
|
It will set errno to
|
|
|
|
.Er EINVAL
|
|
|
|
if an unsupported value for flags was given.
|
2004-08-09 08:45:41 +00:00
|
|
|
The
|
2012-04-13 23:30:38 +00:00
|
|
|
.Fn magic_list ,
|
2004-08-09 08:45:41 +00:00
|
|
|
.Fn magic_load ,
|
|
|
|
.Fn magic_compile ,
|
|
|
|
and
|
|
|
|
.Fn magic_check
|
2011-10-06 06:01:12 +00:00
|
|
|
functions return 0 on success and \-1 on failure.
|
2004-08-09 08:45:41 +00:00
|
|
|
The
|
2012-04-13 23:30:38 +00:00
|
|
|
.Fn magic_buffer ,
|
|
|
|
.Fn magic_getpath ,
|
2004-08-09 08:45:41 +00:00
|
|
|
and
|
2012-04-13 23:30:38 +00:00
|
|
|
.Fn magic_file ,
|
2011-10-06 06:01:12 +00:00
|
|
|
functions return a string on success and
|
|
|
|
.Dv NULL
|
|
|
|
on failure.
|
|
|
|
The
|
2004-08-09 08:45:41 +00:00
|
|
|
.Fn magic_error
|
|
|
|
function returns a textual description of the errors of the above
|
2011-10-06 06:01:12 +00:00
|
|
|
functions, or
|
|
|
|
.Dv NULL
|
|
|
|
if there was no error.
|
2004-08-09 08:45:41 +00:00
|
|
|
Finally,
|
|
|
|
.Fn magic_setflags
|
2011-10-06 06:01:12 +00:00
|
|
|
returns \-1 on systems that don't support
|
|
|
|
.Xr utime 3 ,
|
2004-08-09 08:45:41 +00:00
|
|
|
or
|
|
|
|
.Xr utimes 2
|
|
|
|
when
|
|
|
|
.Dv MAGIC_PRESERVE_ATIME
|
|
|
|
is set.
|
|
|
|
.Sh FILES
|
2009-05-02 06:25:51 +00:00
|
|
|
.Bl -tag -width __MAGIC__.mgc -compact
|
2004-08-09 08:45:41 +00:00
|
|
|
.It Pa __MAGIC__
|
|
|
|
The non-compiled default magic database.
|
|
|
|
.It Pa __MAGIC__.mgc
|
|
|
|
The compiled default magic database.
|
|
|
|
.El
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr file __CSECTION__ ,
|
2011-10-06 06:01:12 +00:00
|
|
|
.Xr magic __FSECTION__
|
2004-08-09 08:45:41 +00:00
|
|
|
.Sh AUTHORS
|
2011-10-06 06:01:12 +00:00
|
|
|
.An M\(oans Rullg\(oard
|
|
|
|
Initial libmagic implementation, and configuration.
|
|
|
|
.An Christos Zoulas
|
|
|
|
API cleanup, error code and allocation handling.
|