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

RETURN VALUES

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

FILES

258     /usr/share/misc/magic      The non-compiled default magic database.
259     /usr/share/misc/magic.mgc  The compiled default magic database.
260

SEE ALSO

262     file(1), magic(5)
263

AUTHORS

265     Måns Rullgård Initial libmagic implementation, and configuration.
266     Christos Zoulas API cleanup, error code and allocation handling.
267
268BSD                              May 23, 2017                              BSD
Impressum