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 example JSON files.
152
153     The magic_close() function closes the magic(5) database and deallocates
154     any resources used.
155
156     The magic_error() function returns a textual explanation of the last
157     error, or NULL if there was no error.
158
159     The magic_errno() function returns the last operating system error number
160     (errno(2)) that was encountered by a system call.
161
162     The magic_file() function returns a textual description of the contents
163     of the filename argument, or NULL if an error occurred.  If the filename
164     is NULL, then stdin is used.
165
166     The magic_descriptor() function returns a textual description of the con‐
167     tents of the fd argument, or NULL if an error occurred.
168
169     The magic_buffer() function returns a textual description of the contents
170     of the buffer argument with length bytes size.
171
172     The magic_getflags() functions returns a value representing current flags
173     set.
174
175     The magic_setflags() function sets the flags described above.  Note that
176     using both MIME flags together can also return extra information on the
177     charset.
178
179     The magic_check() function can be used to check the validity of entries
180     in the colon separated database files passed in as filename, or NULL for
181     the default database.  It returns 0 on success and -1 on failure.
182
183     The magic_compile() function can be used to compile the colon separated
184     list of database files passed in as filename, or NULL for the default
185     database.  It returns 0 on success and -1 on failure.  The compiled files
186     created are named from the basename(1) of each file argument with “.mgc”
187     appended to it.
188
189     The magic_list() function dumps all magic entries in a human readable
190     format, dumping first the entries that are matched against binary files
191     and then the ones that match text files.  It takes and optional filename
192     argument which is a colon separated list of database files, or NULL for
193     the default database.
194
195     The magic_load() function must be used to load the colon separated list
196     of database files passed in as filename, or NULL for the default database
197     file before any magic queries can performed.
198
199     The default database file is named by the MAGIC environment variable.  If
200     that variable is not set, the default database file name is
201     /usr/share/misc/magic.  magic_load() adds “.mgc” to the database filename
202     as appropriate.
203
204     The magic_load_buffers() function takes an array of size nbuffers of
205     buffers with a respective size for each in the array of sizes loaded with
206     the contents of the magic databases from the filesystem.  This function
207     can be used in environment where the magic library does not have direct
208     access to the filesystem, but can access the magic database via shared
209     memory or other IPC means.
210
211     The magic_getparam() and magic_setparam() allow getting and setting vari‐
212     ous limits related to the magic library.
213
214           Parameter                    Type      Default
215           MAGIC_PARAM_INDIR_MAX        size_t    15
216           MAGIC_PARAM_NAME_MAX         size_t    30
217           MAGIC_PARAM_ELF_NOTES_MAX    size_t    256
218           MAGIC_PARAM_ELF_PHNUM_MAX    size_t    128
219           MAGIC_PARAM_ELF_SHNUM_MAX    size_t    32768
220           MAGIC_PARAM_REGEX_MAX        size_t    8192
221           MAGIC_PARAM_BYTES_MAX        size_t    1048576
222
223     The MAGIC_PARAM_INDIR_RECURSION parameter controls how many levels of
224     recursion will be followed for indirect magic entries.
225
226     The MAGIC_PARAM_NAME_RECURSION parameter controls how many levels of
227     recursion will be followed for for name/use calls.
228
229     The MAGIC_PARAM_NAME_MAX parameter controls the maximum number of calls
230     for name/use.
231
232     The MAGIC_PARAM_NOTES_MAX parameter controls how many ELF notes will be
233     processed.
234
235     The MAGIC_PARAM_PHNUM_MAX parameter controls how many ELF program sec‐
236     tions will be processed.
237
238     The MAGIC_PARAM_SHNUM_MAX parameter controls how many ELF sections will
239     be processed.
240
241     The magic_version() command returns the version number of this library
242     which is compiled into the shared library using the constant
243     MAGIC_VERSION from <magic.h>.  This can be used by client programs to
244     verify that the version they compile against is the same as the version
245     that they run against.
246

RETURN VALUES

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

FILES

261     /usr/share/misc/magic      The non-compiled default magic database.
262     /usr/share/misc/magic.mgc  The compiled default magic database.
263

SEE ALSO

265     file(1), magic(5)
266

BUGS

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

AUTHORS

274     Måns Rullgård Initial libmagic implementation, and configuration.
275     Christos Zoulas API cleanup, error code and allocation handling.
276
277BSD                             August 18, 2018                            BSD
Impressum