1LIBMAGIC(3) BSD Library Functions Manual LIBMAGIC(3)
2
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
10 Magic Number Recognition Library (libmagic, -lmagic)
11
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
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_COMPRESS_FORK
126 Don't allow decompressors that use fork.
127
128 MAGIC_NO_CHECK_CDF
129 Don't get extra information on MS Composite Document
130 Files.
131
132 MAGIC_NO_CHECK_COMPRESS
133 Don't look inside compressed files.
134
135 MAGIC_NO_CHECK_ELF
136 Don't print ELF details.
137
138 MAGIC_NO_CHECK_ENCODING
139 Don't check text encodings.
140
141 MAGIC_NO_CHECK_SOFT
142 Don't consult magic files.
143
144 MAGIC_NO_CHECK_TAR
145 Don't examine tar files.
146
147 MAGIC_NO_CHECK_TEXT
148 Don't check for various types of text files.
149
150 MAGIC_NO_CHECK_TOKENS
151 Don't look for known tokens inside ascii files.
152
153 MAGIC_NO_CHECK_JSON
154 Don't examine JSON files.
155
156 MAGIC_NO_CHECK_CSV
157 Don't examine CSV files.
158
159 The magic_close() function closes the magic(5) database and deallocates
160 any resources used.
161
162 The magic_error() function returns a textual explanation of the last er‐
163 ror, or NULL if there was no error.
164
165 The magic_errno() function returns the last operating system error number
166 (errno(2)) that was encountered by a system call.
167
168 The magic_file() function returns a textual description of the contents
169 of the filename argument, or NULL if an error occurred. If the filename
170 is NULL, then stdin is used.
171
172 The magic_descriptor() function returns a textual description of the con‐
173 tents of the fd argument, or NULL if an error occurred.
174
175 The magic_buffer() function returns a textual description of the contents
176 of the buffer argument with length bytes size.
177
178 The magic_getflags() functions returns a value representing current flags
179 set.
180
181 The magic_setflags() function sets the flags described above. Note that
182 using both MIME flags together can also return extra information on the
183 charset.
184
185 The magic_check() function can be used to check the validity of entries
186 in the colon separated database files passed in as filename, or NULL for
187 the default database. It returns 0 on success and -1 on failure.
188
189 The magic_compile() function can be used to compile the colon separated
190 list of database files passed in as filename, or NULL for the default
191 database. It returns 0 on success and -1 on failure. The compiled files
192 created are named from the basename(1) of each file argument with “.mgc”
193 appended to it.
194
195 The magic_list() function dumps all magic entries in a human readable
196 format, dumping first the entries that are matched against binary files
197 and then the ones that match text files. It takes and optional filename
198 argument which is a colon separated list of database files, or NULL for
199 the default database.
200
201 The magic_load() function must be used to load the colon separated list
202 of database files passed in as filename, or NULL for the default database
203 file before any magic queries can performed.
204
205 The default database file is named by the MAGIC environment variable. If
206 that variable is not set, the default database file name is
207 /usr/share/misc/magic. magic_load() adds “.mgc” to the database filename
208 as appropriate.
209
210 The magic_load_buffers() function takes an array of size nbuffers of
211 buffers with a respective size for each in the array of sizes loaded with
212 the contents of the magic databases from the filesystem. This function
213 can be used in environment where the magic library does not have direct
214 access to the filesystem, but can access the magic database via shared
215 memory or other IPC means.
216
217 The magic_getparam() and magic_setparam() allow getting and setting vari‐
218 ous limits related to the magic library.
219
220 Parameter Type Default
221 MAGIC_PARAM_INDIR_MAX size_t 15
222 MAGIC_PARAM_NAME_MAX size_t 30
223 MAGIC_PARAM_ELF_NOTES_MAX size_t 256
224 MAGIC_PARAM_ELF_PHNUM_MAX size_t 128
225 MAGIC_PARAM_ELF_SHNUM_MAX size_t 32768
226 MAGIC_PARAM_REGEX_MAX size_t 8192
227 MAGIC_PARAM_BYTES_MAX size_t 1048576
228
229 The MAGIC_PARAM_INDIR_RECURSION parameter controls how many levels of re‐
230 cursion will be followed for indirect magic entries.
231
232 The MAGIC_PARAM_NAME_RECURSION parameter controls how many levels of re‐
233 cursion will be followed for for name/use calls.
234
235 The MAGIC_PARAM_NAME_MAX parameter controls the maximum number of calls
236 for name/use.
237
238 The MAGIC_PARAM_NOTES_MAX parameter controls how many ELF notes will be
239 processed.
240
241 The MAGIC_PARAM_PHNUM_MAX parameter controls how many ELF program sec‐
242 tions will be processed.
243
244 The MAGIC_PARAM_SHNUM_MAX parameter controls how many ELF sections will
245 be processed.
246
247 The magic_version() command returns the version number of this library
248 which is compiled into the shared library using the constant
249 MAGIC_VERSION from <magic.h>. This can be used by client programs to
250 verify that the version they compile against is the same as the version
251 that they run against.
252
254 The function magic_open() returns a magic cookie on success and NULL on
255 failure setting errno to an appropriate value. It will set errno to
256 EINVAL if an unsupported value for flags was given. The magic_list(),
257 magic_load(), magic_compile(), and magic_check() functions return 0 on
258 success and -1 on failure. The magic_buffer(), magic_getpath(), and
259 magic_file(), functions return a string on success and NULL on failure.
260 The magic_error() function returns a textual description of the errors of
261 the above functions, or NULL if there was no error. The magic_version()
262 always returns the version number of the library. Finally,
263 magic_setflags() returns -1 on systems that don't support utime(3), or
264 utimes(2) when MAGIC_PRESERVE_ATIME is set.
265
267 /usr/share/misc/magic The non-compiled default magic database.
268 /usr/share/misc/magic.mgc The compiled default magic database.
269
271 file(1), magic(5)
272
274 The results from magic_buffer() and magic_file() where the buffer and the
275 file contain the same data can produce different results, because in the
276 magic_file() case, the program can lseek(2) and stat(2) the file descrip‐
277 tor.
278
280 Måns Rullgård Initial libmagic implementation, and configuration.
281 Christos Zoulas API cleanup, error code and allocation handling.
282
283BSD September 15, 2022 BSD