1LIBMAGIC(3)              BSD Library Functions Manual              LIBMAGIC(3)
2

NAME

4     magic_open, magic_close, magic_error, magic_errno, magic_descriptor,
5     magic_buffer, magic_getflags, magic_setflags, magic_check, magic_compile,
6     magic_list, magic_load, magic_load_buffers, magic_setparam,
7     magic_getparam, magic_version — Magic number recognition library
8

LIBRARY

10     Magic Number Recognition Library (libmagic, -lmagic)
11

SYNOPSIS

13     #include <magic.h>
14
15     magic_t
16     magic_open(int flags);
17
18     void
19     magic_close(magic_t cookie);
20
21     const char *
22     magic_error(magic_t cookie);
23
24     int
25     magic_errno(magic_t cookie);
26
27     const char *
28     magic_descriptor(magic_t cookie, int fd);
29
30     const char *
31     magic_file(magic_t cookie, const char *filename);
32
33     const char *
34     magic_buffer(magic_t cookie, const void *buffer, size_t length);
35
36     int
37     magic_getflags(magic_t cookie);
38
39     int
40     magic_setflags(magic_t cookie, int flags);
41
42     int
43     magic_check(magic_t cookie, const char *filename);
44
45     int
46     magic_compile(magic_t cookie, const char *filename);
47
48     int
49     magic_list(magic_t cookie, const char *filename);
50
51     int
52     magic_load(magic_t cookie, const char *filename);
53
54     int
55     magic_load_buffers(magic_t cookie, void **buffers, size_t *sizes,
56         size_t nbuffers);
57
58     int
59     magic_getparam(magic_t cookie, int param, void *value);
60
61     int
62     magic_setparam(magic_t cookie, int param, const void *value);
63
64     int
65     magic_version(void);
66

DESCRIPTION

68     These functions operate on the magic database file which is described in
69     magic(5).
70
71     The function magic_open() creates a magic cookie pointer and returns it.
72     It returns NULL if there was an error allocating the magic cookie.  The
73     flags argument specifies how the other magic functions should behave:
74
75     MAGIC_NONE      No special handling.
76
77     MAGIC_DEBUG     Print debugging messages to stderr.
78
79     MAGIC_SYMLINK   If the file queried is a symlink, follow it.
80
81     MAGIC_COMPRESS  If the file is compressed, unpack it and look at the con‐
82                     tents.
83
84     MAGIC_DEVICES   If the file is a block or character special device, then
85                     open the device and try to look in its contents.
86
87     MAGIC_MIME_TYPE
88                     Return a MIME type string, instead of a textual descrip‐
89                     tion.
90
91     MAGIC_MIME_ENCODING
92                     Return a MIME encoding, instead of a textual description.
93
94     MAGIC_MIME      A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
95
96     MAGIC_CONTINUE  Return all matches, not just the first.
97
98     MAGIC_CHECK     Check the magic database for consistency and print warn‐
99                     ings to stderr.
100
101     MAGIC_PRESERVE_ATIME
102                     On systems that support utime(3) or utimes(2), attempt to
103                     preserve the access time of files analysed.
104
105     MAGIC_RAW       Don't translate unprintable characters to a \ooo octal
106                     representation.
107
108     MAGIC_ERROR     Treat operating system errors while trying to open files
109                     and follow symlinks as real errors, instead of printing
110                     them in the magic buffer.
111
112     MAGIC_APPLE     Return the Apple creator and type.
113
114     MAGIC_EXTENSION
115                     Return a slash-separated list of extensions for this file
116                     type.
117
118     MAGIC_COMPRESS_TRANSP
119                     Don't report on compression, only report about the uncom‐
120                     pressed data.
121
122     MAGIC_NO_CHECK_APPTYPE
123                     Don't check for EMX application type (only on EMX).
124
125     MAGIC_NO_CHECK_CDF
126                     Don't get extra information on MS Composite Document
127                     Files.
128
129     MAGIC_NO_CHECK_COMPRESS
130                     Don't look inside compressed files.
131
132     MAGIC_NO_CHECK_ELF
133                     Don't print ELF details.
134
135     MAGIC_NO_CHECK_ENCODING
136                     Don't check text encodings.
137
138     MAGIC_NO_CHECK_SOFT
139                     Don't consult magic files.
140
141     MAGIC_NO_CHECK_TAR
142                     Don't examine tar files.
143
144     MAGIC_NO_CHECK_TEXT
145                     Don't check for various types of text files.
146
147     MAGIC_NO_CHECK_TOKENS
148                     Don't look for known tokens inside ascii files.
149
150     MAGIC_NO_CHECK_JSON
151                     Don't examine JSON files.
152
153     MAGIC_NO_CHECK_CSV
154                     Don't examine CSV files.
155
156     The magic_close() function closes the magic(5) database and deallocates
157     any resources used.
158
159     The magic_error() function returns a textual explanation of the last
160     error, or NULL if there was no error.
161
162     The magic_errno() function returns the last operating system error number
163     (errno(2)) that was encountered by a system call.
164
165     The magic_file() function returns a textual description of the contents
166     of the filename argument, or NULL if an error occurred.  If the filename
167     is NULL, then stdin is used.
168
169     The magic_descriptor() function returns a textual description of the con‐
170     tents of the fd argument, or NULL if an error occurred.
171
172     The magic_buffer() function returns a textual description of the contents
173     of the buffer argument with length bytes size.
174
175     The magic_getflags() functions returns a value representing current flags
176     set.
177
178     The magic_setflags() function sets the flags described above.  Note that
179     using both MIME flags together can also return extra information on the
180     charset.
181
182     The magic_check() function can be used to check the validity of entries
183     in the colon separated database files passed in as filename, or NULL for
184     the default database.  It returns 0 on success and -1 on failure.
185
186     The magic_compile() function can be used to compile the colon separated
187     list of database files passed in as filename, or NULL for the default
188     database.  It returns 0 on success and -1 on failure.  The compiled files
189     created are named from the basename(1) of each file argument with “.mgc”
190     appended to it.
191
192     The magic_list() function dumps all magic entries in a human readable
193     format, dumping first the entries that are matched against binary files
194     and then the ones that match text files.  It takes and optional filename
195     argument which is a colon separated list of database files, or NULL for
196     the default database.
197
198     The magic_load() function must be used to load the colon separated list
199     of database files passed in as filename, or NULL for the default database
200     file before any magic queries can performed.
201
202     The default database file is named by the MAGIC environment variable.  If
203     that variable is not set, the default database file name is
204     /usr/share/misc/magic.  magic_load() adds “.mgc” to the database filename
205     as appropriate.
206
207     The magic_load_buffers() function takes an array of size nbuffers of
208     buffers with a respective size for each in the array of sizes loaded with
209     the contents of the magic databases from the filesystem.  This function
210     can be used in environment where the magic library does not have direct
211     access to the filesystem, but can access the magic database via shared
212     memory or other IPC means.
213
214     The magic_getparam() and magic_setparam() allow getting and setting vari‐
215     ous limits related to the magic library.
216
217           Parameter                    Type      Default
218           MAGIC_PARAM_INDIR_MAX        size_t    15
219           MAGIC_PARAM_NAME_MAX         size_t    30
220           MAGIC_PARAM_ELF_NOTES_MAX    size_t    256
221           MAGIC_PARAM_ELF_PHNUM_MAX    size_t    128
222           MAGIC_PARAM_ELF_SHNUM_MAX    size_t    32768
223           MAGIC_PARAM_REGEX_MAX        size_t    8192
224           MAGIC_PARAM_BYTES_MAX        size_t    1048576
225
226     The MAGIC_PARAM_INDIR_RECURSION parameter controls how many levels of
227     recursion will be followed for indirect magic entries.
228
229     The MAGIC_PARAM_NAME_RECURSION parameter controls how many levels of
230     recursion will be followed for for name/use calls.
231
232     The MAGIC_PARAM_NAME_MAX parameter controls the maximum number of calls
233     for name/use.
234
235     The MAGIC_PARAM_NOTES_MAX parameter controls how many ELF notes will be
236     processed.
237
238     The MAGIC_PARAM_PHNUM_MAX parameter controls how many ELF program sec‐
239     tions will be processed.
240
241     The MAGIC_PARAM_SHNUM_MAX parameter controls how many ELF sections will
242     be processed.
243
244     The magic_version() command returns the version number of this library
245     which is compiled into the shared library using the constant
246     MAGIC_VERSION from <magic.h>.  This can be used by client programs to
247     verify that the version they compile against is the same as the version
248     that they run against.
249

RETURN VALUES

251     The function magic_open() returns a magic cookie on success and NULL on
252     failure setting errno to an appropriate value.  It will set errno to
253     EINVAL if an unsupported value for flags was given.  The magic_list(),
254     magic_load(), magic_compile(), and magic_check() functions return 0 on
255     success and -1 on failure.  The magic_buffer(), magic_getpath(), and
256     magic_file(), functions return a string on success and NULL on failure.
257     The magic_error() function returns a textual description of the errors of
258     the above functions, or NULL if there was no error.  The magic_version()
259     always returns the version number of the library.  Finally,
260     magic_setflags() returns -1 on systems that don't support utime(3), or
261     utimes(2) when MAGIC_PRESERVE_ATIME is set.
262

FILES

264     /usr/share/misc/magic      The non-compiled default magic database.
265     /usr/share/misc/magic.mgc  The compiled default magic database.
266

SEE ALSO

268     file(1), magic(5)
269

BUGS

271     The results from magic_buffer() and magic_file() where the buffer and the
272     file contain the same data can produce different results, because in the
273     magic_file() case, the program can lseek(2) and stat(2) the file descrip‐
274     tor.
275

AUTHORS

277     Måns Rullgård Initial libmagic implementation, and configuration.
278     Christos Zoulas API cleanup, error code and allocation handling.
279
280BSD                              June 8, 2019                              BSD
Impressum