# phpman > man > LIBMAGIC(3)

[LIBMAGIC(3)](https://www.chedong.com/phpMan.php/man/LIBMAGIC/3/markdown)              BSD Library Functions Manual              [LIBMAGIC(3)](https://www.chedong.com/phpMan.php/man/LIBMAGIC/3/markdown)

## NAME
     **magic**___**open**, **magic**___**close**, **magic**___**error**, **magic**___**errno**, **magic**___**descriptor**, **magic**___**buffer**,
     **magic**___**getflags**, **magic**___**setflags**, **magic**___**check**, **magic**___**compile**, **magic**___**list**, **magic**___**load**,
     **magic**___**load**___**buffers**, **magic**___**setparam**, **magic**___**getparam**, **magic**___**version** — Magic number recognition
     library

## LIBRARY
     Magic Number Recognition Library (libmagic, -lmagic)

## SYNOPSIS
### #include <magic.h>

     _magic_t_
     **magic**___**open**(_int_ _flags_);

     _void_
     **magic**___**close**(_magic_t_ _cookie_);

     _const_ _char_ _*_
     **magic**___**error**(_magic_t_ _cookie_);

     _int_
     **magic**___**errno**(_magic_t_ _cookie_);

     _const_ _char_ _*_
     **magic**___**descriptor**(_magic_t_ _cookie_, _int_ _fd_);

     _const_ _char_ _*_
     **magic**___**file**(_magic_t_ _cookie_, _const_ _char_ _*filename_);

     _const_ _char_ _*_
     **magic**___**buffer**(_magic_t_ _cookie_, _const_ _void_ _*buffer_, _size_t_ _length_);

     _int_
     **magic**___**getflags**(_magic_t_ _cookie_);

     _int_
     **magic**___**setflags**(_magic_t_ _cookie_, _int_ _flags_);

     _int_
     **magic**___**check**(_magic_t_ _cookie_, _const_ _char_ _*filename_);

     _int_
     **magic**___**compile**(_magic_t_ _cookie_, _const_ _char_ _*filename_);

     _int_
     **magic**___**list**(_magic_t_ _cookie_, _const_ _char_ _*filename_);

     _int_
     **magic**___**load**(_magic_t_ _cookie_, _const_ _char_ _*filename_);

     _int_
     **magic**___**load**___**buffers**(_magic_t_ _cookie_, _void_ _**buffers_, _size_t_ _*sizes_, _size_t_ _nbuffers_);

     _int_
     **magic**___**getparam**(_magic_t_ _cookie_, _int_ _param_, _void_ _*value_);

     _int_
     **magic**___**setparam**(_magic_t_ _cookie_, _int_ _param_, _const_ _void_ _*value_);

     _int_
     **magic**___**version**(_void_);

## DESCRIPTION
     These functions operate on the magic database file which is described in [magic(5)](https://www.chedong.com/phpMan.php/man/magic/5/markdown).

     The function **magic**___**open**() creates a magic cookie pointer and returns it.  It returns NULL if
     there was an error allocating the magic cookie.  The _flags_ argument specifies how the other
     magic functions should behave:

     MAGIC_NONE      No special handling.

     MAGIC_DEBUG     Print debugging messages to stderr.

     MAGIC_SYMLINK   If the file queried is a symlink, follow it.

     MAGIC_COMPRESS  If the file is compressed, unpack it and look at the contents.

     MAGIC_DEVICES   If the file is a block or character special device, then open the device and
                     try to look in its contents.

     MAGIC_MIME_TYPE
                     Return a MIME type string, instead of a textual description.

     MAGIC_MIME_ENCODING
                     Return a MIME encoding, instead of a textual description.

     MAGIC_MIME      A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.

     MAGIC_CONTINUE  Return all matches, not just the first.

     MAGIC_CHECK     Check the magic database for consistency and print warnings to stderr.

     MAGIC_PRESERVE_ATIME
                     On systems that support [utime(3)](https://www.chedong.com/phpMan.php/man/utime/3/markdown) or [utimes(2)](https://www.chedong.com/phpMan.php/man/utimes/2/markdown), attempt to preserve the access
                     time of files analysed.

     MAGIC_RAW       Don't translate unprintable characters to a \ooo octal representation.

     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.

     MAGIC_APPLE     Return the Apple creator and type.

     MAGIC_EXTENSION
                     Return a slash-separated list of extensions for this file type.

     MAGIC_COMPRESS_TRANSP
                     Don't report on compression, only report about the uncompressed data.

     MAGIC_NO_CHECK_APPTYPE
                     Don't check for EMX application type (only on EMX).

     MAGIC_NO_CHECK_CDF
                     Don't get extra information on MS Composite Document Files.

     MAGIC_NO_CHECK_COMPRESS
                     Don't look inside compressed files.

     MAGIC_NO_CHECK_ELF
                     Don't print ELF details.

     MAGIC_NO_CHECK_ENCODING
                     Don't check text encodings.

     MAGIC_NO_CHECK_SOFT
                     Don't consult magic files.

     MAGIC_NO_CHECK_TAR
                     Don't examine tar files.

     MAGIC_NO_CHECK_TEXT
                     Don't check for various types of text files.

     MAGIC_NO_CHECK_TOKENS
                     Don't look for known tokens inside ascii files.

     MAGIC_NO_CHECK_JSON
                     Don't examine JSON files.

     MAGIC_NO_CHECK_CSV
                     Don't examine CSV files.

     The **magic**___**close**() function closes the [magic(5)](https://www.chedong.com/phpMan.php/man/magic/5/markdown) database and deallocates any resources used.

     The **magic**___**error**() function returns a textual explanation of the last error, or NULL if there
     was no error.

     The **magic**___**errno**() function returns the last operating system error number ([errno(2)](https://www.chedong.com/phpMan.php/man/errno/2/markdown)) that was
     encountered by a system call.

     The **magic**___**file**() function returns a textual description of the contents of the _filename_ argu‐
     ment, or NULL if an error occurred.  If the _filename_ is NULL, then stdin is used.

     The **magic**___**descriptor**() function returns a textual description of the contents of the _fd_ argu‐
     ment, or NULL if an error occurred.

     The **magic**___**buffer**() function returns a textual description of the contents of the _buffer_ argu‐
     ment with _length_ bytes size.

     The **magic**___**getflags**() functions returns a value representing current _flags_ set.

     The **magic**___**setflags**() function sets the _flags_ described above.  Note that using both MIME flags
     together can also return extra information on the charset.

     The **magic**___**check**() function can be used to check the validity of entries in the colon separated
     database files passed in as _filename_, or NULL for the default database.  It returns 0 on suc‐
     cess and -1 on failure.

     The **magic**___**compile**() function can be used to compile the colon separated list of database files
     passed in as _filename_, or NULL for the default database.  It returns 0 on success and -1 on
     failure.  The compiled files created are named from the [basename(1)](https://www.chedong.com/phpMan.php/man/basename/1/markdown) of each file argument with
     “.mgc” appended to it.

     The **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 _filename_ argument which is a colon separated list of database files, or NULL
     for the default database.

     The **magic**___**load**() function must be used to load the colon separated list of database files
     passed in as _filename_, or NULL for the default database file before any magic queries can per‐
     formed.

     The default database file is named by the MAGIC environment variable.  If that variable is not
     set, the default database file name is /usr/share/misc/magic.  **magic**___**load**() adds “.mgc” to the
     database filename as appropriate.

     The **magic**___**load**___**buffers**() function takes an array of size _nbuffers_ of _buffers_ with a respective
     size for each in the array of _sizes_ loaded with the contents of the magic databases from the
     filesystem.  This function can be used in environment where the magic library does not have di‐
     rect access to the filesystem, but can access the magic database via shared memory or other IPC
     means.

     The **magic**___**getparam**() and **magic**___**setparam**() allow getting and setting various limits related to
     the magic library.

           **Parameter**                    **Type**      **Default**
           MAGIC_PARAM_INDIR_MAX        size_t    15
           MAGIC_PARAM_NAME_MAX         size_t    30
           MAGIC_PARAM_ELF_NOTES_MAX    size_t    256
           MAGIC_PARAM_ELF_PHNUM_MAX    size_t    128
           MAGIC_PARAM_ELF_SHNUM_MAX    size_t    32768
           MAGIC_PARAM_REGEX_MAX        size_t    8192
           MAGIC_PARAM_BYTES_MAX        size_t    1048576

     The MAGIC_PARAM_INDIR_RECURSION parameter controls how many levels of recursion will be fol‐
     lowed for indirect magic entries.

     The MAGIC_PARAM_NAME_RECURSION parameter controls how many levels of recursion will be followed
     for for name/use calls.

     The MAGIC_PARAM_NAME_MAX parameter controls the maximum number of calls for name/use.

     The MAGIC_PARAM_NOTES_MAX parameter controls how many ELF notes will be processed.

     The MAGIC_PARAM_PHNUM_MAX parameter controls how many ELF program sections will be processed.

     The MAGIC_PARAM_SHNUM_MAX parameter controls how many ELF sections will be processed.

     The **magic**___**version**() command returns the version number of this library which is compiled into
     the shared library using the constant MAGIC_VERSION from <_magic.h_>.  This can be used by client
     programs to verify that the version they compile against is the same as the version that they
     run against.

## RETURN VALUES
     The function **magic**___**open**() returns a magic cookie on success and NULL on failure setting errno
     to an appropriate value.  It will set errno to EINVAL if an unsupported value for flags was
     given.  The **magic**___**list**(), **magic**___**load**(), **magic**___**compile**(), and **magic**___**check**() functions return 0
     on success and -1 on failure.  The **magic**___**buffer**(), **magic**___**getpath**(), and **magic**___**file**(), functions
     return a string on success and NULL on failure.  The **magic**___**error**() function returns a textual
     description of the errors of the above functions, or NULL if there was no error.  The
     **magic**___**version**() always returns the version number of the library.  Finally, **magic**___**setflags**()
     returns -1 on systems that don't support [utime(3)](https://www.chedong.com/phpMan.php/man/utime/3/markdown), or [utimes(2)](https://www.chedong.com/phpMan.php/man/utimes/2/markdown) when MAGIC_PRESERVE_ATIME is
     set.

## FILES
     /usr/share/misc/magic      The non-compiled default magic database.
     /usr/share/misc/magic.mgc  The compiled default magic database.

## SEE ALSO
     [file(1)](https://www.chedong.com/phpMan.php/man/file/1/markdown), [magic(5)](https://www.chedong.com/phpMan.php/man/magic/5/markdown)

## BUGS
     The results from **magic**___**buffer**() and **magic**___**file**() where the buffer and the file contain the same
     data can produce different results, because in the **magic**___**file**() case, the program can [lseek(2)](https://www.chedong.com/phpMan.php/man/lseek/2/markdown)
     and [stat(2)](https://www.chedong.com/phpMan.php/man/stat/2/markdown) the file descriptor.

## AUTHORS
     Måns Rullgård Initial libmagic implementation, and configuration.
     Christos Zoulas API cleanup, error code and allocation handling.

BSD                              June 8, 2019                              BSD
