NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUES |FILES |SEE ALSO |BUGS |AUTHORS |COLOPHON | |
LIBMAGIC(3) Library Functions ManualLIBMAGIC(3)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
Magic Number Recognition Library (libmagic, -lmagic)
<magic.h>magic_tmagic_open(int flags)voidmagic_close(magic_tcookie)const char *magic_error(magic_t cookie)intmagic_errno(magic_t cookie)const char *magic_descriptor(magic_tcookie,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)intmagic_getflags(magic_t cookie)intmagic_setflags(magic_t cookie,int flags)intmagic_check(magic_tcookie,const char *filename)intmagic_compile(magic_t cookie,const char *filename)intmagic_list(magic_t cookie,const char*filename)intmagic_load(magic_t cookie,const char *filename)intmagic_load_buffers(magic_t cookie,void **buffers,size_t*sizes,size_t nbuffers)intmagic_getparam(magic_t cookie,intparam,void *value)intmagic_setparam(magic_t cookie,int param,const void *value)intmagic_version(void)const char *magic_getpath(const char *magicfile,int action)
These functions operate on the magic database file which is described inmagic(4). The functionmagic_open() creates a magic cookie pointer and returns it. It returns NULL if there was an error allocating the magic cookie. Theflags 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 supportutime(3) orutimes(2), 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_COMPRESS_FORK Don't allow decompressors that use fork. 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. MAGIC_NO_CHECK_SIMH Don't examine SIMH tape files. Themagic_close() function closes themagic(4) database and deallocates any resources used. Themagic_error() function returns a textual explanation of the last error, or NULL if there was no error. Themagic_errno() function returns the last operating system error number (errno(2)) that was encountered by a system call. Themagic_file() function returns a textual description of the contents of thefilename argument, or NULL if an error occurred. If thefilename is NULL, then stdin is used. Themagic_descriptor() function returns a textual description of the contents of thefd argument, or NULL if an error occurred. Themagic_buffer() function returns a textual description of the contents of thebuffer argument withlength bytes size. Themagic_getflags() functions returns a value representing currentflags set. Themagic_setflags() function sets theflags described above. Note that using both MIME flags together can also return extra information on the charset. Themagic_check() function can be used to check the validity of entries in the colon separated database files passed in asfilename, or NULL for the default database. It returns 0 on success and -1 on failure. Themagic_compile() function can be used to compile the colon separated list of database files passed in asfilename, or NULL for the default database. It returns 0 on success and -1 on failure. The compiled files created are named from thebasename(1) of each file argument with “.mgc” appended to it. Themagic_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 optionalfilename argument which is a colon separated list of database files, or NULL for the default database. Themagic_load() function must be used to load the colon separated list of database files passed in asfilename, or NULL for the default database file before any magic queries can performed. The default database file is named by the MAGIC environment variable. If that variable is not set, the default database file name is /usr/local/share/misc/magic.magic_load() adds “.mgc” to the database filename as appropriate. Themagic_load_buffers() function takes an array of sizenbuffers ofbuffers with a respective size for each in the array ofsizes 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 direct access to the filesystem, but can access the magic database via shared memory or other IPC means. Themagic_getparam() andmagic_setparam() allow getting and setting various limits related to the magic library.Parameter Type DefaultMAGIC_PARAM_INDIR_MAXsize_t 15MAGIC_PARAM_NAME_MAXsize_t 30MAGIC_PARAM_ELF_NOTES_MAXsize_t 256MAGIC_PARAM_ELF_PHNUM_MAXsize_t 128MAGIC_PARAM_ELF_SHNUM_MAXsize_t 32768MAGIC_PARAM_REGEX_MAXsize_t 8192MAGIC_PARAM_BYTES_MAXsize_t 7340032MAGIC_PARAM_ENCODING_MAXsize_t 1048576MAGIC_PARAM_ELF_SHSIZE_MAXsize_t 134217728MAGIC_PARAM_MAGWARN_MAXsize_t 64 The MAGIC_PARAM_INDIR_RECURSION parameter controls how many levels of recursion will be followed 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_PARAM_REGEX_MAX parameter controls the maximum length for regex searches. The MAGIC_PARAM_BYTES_MAX parameter controls the maximum number of bytes to look inside a file. The MAGIC_PARAM_ENCODING_MAX parameter controls the maximum number of bytes to scan for encoding detection. The MAGIC_PARAM_ELF_SHSIZE_MAX parameter controls the maximum number of bytes in an elf section. The MAGIC_PARAM_MAGWARN_MAX parameter controls the maximum number of warnings to tolerate in a magic file. Themagic_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. Themagic_getpath() command returns the colon separated list of magic database locations. If thefilename is non-NULL, then it is returned. Otherwise, if the MAGIC environment variable is defined, then it is returned. Otherwise, ifaction is 0 (meaning "file load"), then any user-specific magic database file is included. Otherwise, only the system default magic database path is included.
The functionmagic_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. Themagic_list(),magic_load(),magic_compile(), andmagic_check() functions return 0 on success and -1 on failure. Themagic_buffer(),magic_getpath(), andmagic_file(), functions return a string on success and NULL on failure. Themagic_error() function returns a textual description of the errors of the above functions, or NULL if there was no error. Themagic_version() always returns the version number of the library. Finally,magic_setflags() returns -1 on systems that don't supportutime(3), orutimes(2) when MAGIC_PRESERVE_ATIME is set.
/usr/local/share/misc/magic The non-compiled default magic database./usr/local/share/misc/magic.mgc The compiled default magic database.
file(1),magic(4)
The results frommagic_buffer() andmagic_file() where the buffer and the file contain the same data can produce different results, because in themagic_file() case, the program canlseek(2) andstat(2) the file descriptor.
Måns Rullgård Initial libmagic implementation, and configuration. Christos Zoulas API cleanup, error code and allocation handling.
This page is part of thefile (a file type guesser) project. Information about the project can be found athttp://www.darwinsys.com/file/. If you have a bug report for this manual page, see ⟨http://bugs.gw.com/my_view_page.php⟩. This page was obtained from the project's upstream Git read-only mirror of the CVS repository ⟨https://github.com/glensc/file⟩ on 2025-08-11. (At that time, the date of the most recent commit that was found in the repository was 2025-07-23.) If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up-to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgGNU December 29, 2023LIBMAGIC(3)HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |