1MAGIC(3)                 BSD Library Functions Manual                 MAGIC(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      Return a mime string, instead of a textual description.
65
66     MAGIC_CONTINUE  Return all matches, not just the first.
67
68     MAGIC_CHECK     Check the magic database for consistency and print warn‐
69                     ings to stderr.
70
71     MAGIC_PRESERVE_ATIME
72                     On systems that support utime(2) or utimes(2), attempt to
73                     preserve the access time of files analyzed.
74
75     MAGIC_RAW       Don't translate unprintable characters to a \ooo octal
76                     representation.
77
78     MAGIC_ERROR     Treat operating system errors while trying to open files
79                     and follow symlinks as real errors, instead of printing
80                     them in the magic buffer.
81
82     MAGIC_NO_CHECK_APPTYPE
83                     Check for EMX application type (only on EMX).
84
85     MAGIC_NO_CHECK_ASCII
86                     Check for various types of ascii files.
87
88     MAGIC_NO_CHECK_COMPRESS
89                     Don't look for, or inside compressed files.
90
91     MAGIC_NO_CHECK_ELF
92                     Don't print elf details.
93
94     MAGIC_NO_CHECK_FORTRAN
95                     Don't look for fortran sequences inside ascii files.
96
97     MAGIC_NO_CHECK_SOFT
98                     Don't consult magic files.
99
100     MAGIC_NO_CHECK_TAR
101                     Don't examine tar files.
102
103     MAGIC_NO_CHECK_TOKENS
104                     Don't look for known tokens inside ascii files.
105
106     MAGIC_NO_CHECK_TROFF
107                     Don't look for troff sequences inside ascii files.
108
109     The magic_close() function closes the magic(5) database and deallocates
110     any resources used.
111
112     The magic_error() function returns a textual explanation of the last
113     error, or NULL if there was no error.
114
115     The magic_errno() function returns the last operating system error number
116     (errno(2)) that was encountered by a system call.
117
118     The magic_file() function returns a textual description of the contents
119     of the filename argument, or NULL if an error occurred.  If the filename
120     is NULL, then stdin is used.
121
122     The magic_buffer() function returns a textual description of the contents
123     of the buffer argument with length bytes size.
124
125     The magic_setflags() function, sets the flags described above.
126
127     The magic_check() function can be used to check the validity of entries
128     in the colon separated database files passed in as filename, or NULL for
129     the default database. It returns 0 on success and -1 on failure.
130
131     The magic_compile() function can be used to compile the the colon sepa‐
132     rated list of database files passed in as filename, or NULL for the
133     default database. It returns 0 on success and -1 on failure. The compiled
134     files created are named from the basename(1) of each file argument with
135     ".mgc" appended to it.
136
137     The magic_load() function must be used to load the the colon separated
138     list of database files passed in as filename, or NULL for the default
139     database file before any magic queries can performed.
140
141     The default database file is named by the MAGIC environment variable.  If
142     that variable is not set, the default database file name is
143     /usr/share/file/magic.
144
145     magic_load() adds ".mime" and/or ".mgc" to the database filename as
146     appropriate.
147

RETURN VALUES

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

FILES

160     /usr/share/file/magic.mime      The non-compiled default magic mime data‐
161                                     base.
162     /usr/share/file/magic.mime.mgc  The compiled default magic mime database.
163     /usr/share/file/magic           The non-compiled default magic database.
164     /usr/share/file/magic.mgc       The compiled default magic database.
165

SEE ALSO

167     file(1), magic(5)
168

AUTHORS

170     Måns Rullgård Initial libmagic implementation, and configuration.  Chris‐
171     tos Zoulas API cleanup, error code and allocation handling.
172
173BSD                            November 15, 2006                           BSD
Impressum