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

NAME

4     magic_open, magic_close, magic_error, magic_file, magic_buffer,
5     magic_setflags, magic_check, magic_compile, magic_load — Magic number
6     recognition library.
7

LIBRARY

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

SYNOPSIS

12     #include <magic.h>
13
14     magic_t
15     magic_open(int flags);
16
17     void
18     magic_close(magic_t cookie);
19
20     const char *
21     magic_error(magic_t cookie);
22
23     int
24     magic_errno(magic_t cookie);
25
26     const char *
27     magic_file(magic_t cookie, const char *filename);
28
29     const char *
30     magic_buffer(magic_t cookie, const void *buffer, size_t length);
31
32     int
33     magic_setflags(magic_t cookie, int flags);
34
35     int
36     magic_check(magic_t cookie, const char *filename);
37
38     int
39     magic_compile(magic_t cookie, const char *filename);
40
41     int
42     magic_load(magic_t cookie, const char *filename);
43

DESCRIPTION

45     These functions operate on the magic database file which is described in
46     magic(5).
47
48     The function magic_open() creates a magic cookie pointer and returns it.
49     It returns NULL if there was an error allocating the magic cookie. The
50     flags argument specifies how the other magic functions should behave:
51
52     MAGIC_NONE      No special handling.
53
54     MAGIC_DEBUG     Print debugging messages to stderr.
55
56     MAGIC_SYMLINK   If the file queried is a symlink, follow it.
57
58     MAGIC_COMPRESS  If the file is compressed, unpack it and look at the con‐
59                     tents.
60
61     MAGIC_DEVICES   If the file is a block or character special device, then
62                     open the device and try to look in its contents.
63
64     MAGIC_MIME_TYPE
65                     Return a MIME type string, instead of a textual descrip‐
66                     tion.
67
68     MAGIC_MIME_ENCODING
69                     Return a MIME encoding, instead of a textual description.
70
71     MAGIC_CONTINUE  Return all matches, not just the first.
72
73     MAGIC_CHECK     Check the magic database for consistency and print warn‐
74                     ings to stderr.
75
76     MAGIC_PRESERVE_ATIME
77                     On systems that support utime(2) or utimes(2), attempt to
78                     preserve the access time of files analyzed.
79
80     MAGIC_RAW       Don't translate unprintable characters to a \ooo octal
81                     representation.
82
83     MAGIC_ERROR     Treat operating system errors while trying to open files
84                     and follow symlinks as real errors, instead of printing
85                     them in the magic buffer.
86
87     MAGIC_NO_CHECK_APPTYPE
88                     Check for EMX application type (only on EMX).
89
90     MAGIC_NO_CHECK_ASCII
91                     Check for various types of ascii files.
92
93     MAGIC_NO_CHECK_COMPRESS
94                     Don't look for, or inside compressed files.
95
96     MAGIC_NO_CHECK_ELF
97                     Don't print elf details.
98
99     MAGIC_NO_CHECK_FORTRAN
100                     Don't look for fortran sequences inside ascii files.
101
102     MAGIC_NO_CHECK_SOFT
103                     Don't consult magic files.
104
105     MAGIC_NO_CHECK_TAR
106                     Don't examine tar files.
107
108     MAGIC_NO_CHECK_TOKENS
109                     Don't look for known tokens inside ascii files.
110
111     MAGIC_NO_CHECK_TROFF
112                     Don't look for troff sequences inside ascii files.
113
114     The magic_close() function closes the magic(5) database and deallocates
115     any resources used.
116
117     The magic_error() function returns a textual explanation of the last
118     error, or NULL if there was no error.
119
120     The magic_errno() function returns the last operating system error number
121     (errno(2)) that was encountered by a system call.
122
123     The magic_file() function returns a textual description of the contents
124     of the filename argument, or NULL if an error occurred.  If the filename
125     is NULL, then stdin is used.
126
127     The magic_buffer() function returns a textual description of the contents
128     of the buffer argument with length bytes size.
129
130     The magic_setflags() function sets the flags described above. Note that
131     using both MIME flags together can also return extra information on the
132     charset.
133
134     The magic_check() function can be used to check the validity of entries
135     in the colon separated database files passed in as filename, or NULL for
136     the default database. It returns 0 on success and -1 on failure.
137
138     The magic_compile() function can be used to compile the the colon sepa‐
139     rated list of database files passed in as filename, or NULL for the
140     default database. It returns 0 on success and -1 on failure. The compiled
141     files created are named from the basename(1) of each file argument with
142     “.mgc” appended to it.
143
144     The magic_load() function must be used to load the the colon separated
145     list of database files passed in as filename, or NULL for the default
146     database file before any magic queries can performed.
147
148     The default database file is named by the MAGIC environment variable.  If
149     that variable is not set, the default database file name is
150     /usr/share/misc/magic.  magic_load() adds “.mgc” to the database filename
151     as appropriate.
152

RETURN VALUES

154     The function magic_open() returns a magic cookie on success and NULL on
155     failure setting errno to an appropriate value. It will set errno to EIN‐
156     VAL if an unsupported value for flags was given.  The magic_load(),
157     magic_compile(), and magic_check() functions return 0 on success and -1
158     on failure.  The magic_file(), and magic_buffer() functions return a
159     string on success and NULL on failure. The magic_error() function returns
160     a textual description of the errors of the above functions, or NULL if
161     there was no error.  Finally, magic_setflags() returns -1 on systems that
162     don't support utime(2), or utimes(2) when MAGIC_PRESERVE_ATIME is set.
163

FILES

165     /usr/share/misc/magic      The non-compiled default magic database.
166     /usr/share/misc/magic.mgc  The compiled default magic database.
167

SEE ALSO

169     file(1), magic(5)
170

AUTHORS

172     Måns Rullgård Initial libmagic implementation, and configuration.  Chris‐
173     tos Zoulas API cleanup, error code and allocation handling.
174
175BSD                            November 24, 2009                           BSD
Impressum