---
name: "libmagic"
section: "3"
platform: "linux"
package: "file"
version: "5.48-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/libmagic"
---

# libmagic(3)

Magic number recognition library

## 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

library “libmagic”

## 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*);

*const* *char* *\**\

**magic\_getpath**(*const char \*magicfile*, *int action*);

## DESCRIPTION

These functions operate on the magic database file which is described in [magic(5)](/magic/5.md).

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)](/utime/3.md) or [utimes(2)](/utimes/2.md), 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.

The **magic\_close**() function closes the [magic(5)](/magic/5.md) 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)](/errno/2.md)) that was encountered by a system call.

The **magic\_file**() function returns a textual description of the contents of the *filename* argument, 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* argument, or NULL if an error occurred.

The **magic\_buffer**() function returns a textual description of the contents of the *buffer* argument 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 success 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)](/basename/1.md) 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 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/share/file/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 direct 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 | 7340032 |
| MAGIC\_PARAM\_ENCODING\_MAX | size\_t | 1048576 |
| MAGIC\_PARAM\_ELF\_SHSIZE\_MAX | size\_t | 134217728 |
| MAGIC\_PARAM\_MAGWARN\_MAX | size\_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.

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.

The **magic\_getpath**() command returns the colon separated list of magic database locations. If the *filename* is non-NULL, then it is returned. Otherwise, if the MAGIC environment variable is defined, then it is returned. Otherwise, if *action* is 0 (meaning "file load"), then any user-specific magic database file is included. Otherwise, only the system default magic database path is included.

## 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)](/utime/3.md), or [utimes(2)](/utimes/2.md) when MAGIC\_PRESERVE\_ATIME is set.

## FILES

- */usr/share/file/misc/magic*

  The non-compiled default magic database.

- */usr/share/file/misc/magic.mgc*

  The compiled default magic database.

## SEE ALSO

[file(1)](/file/1.md), [magic(5)](/magic/5.md)

## 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)](/lseek/2.md) and [stat(2)](/stat/2.md) the file descriptor.

## AUTHORS

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