1.\" $File: libmagic.man,v 1.23 2011/01/14 21:59:17 rrt Exp $ 2.\" 3.\" Copyright (c) Christos Zoulas 2003. 4.\" All Rights Reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice immediately at the beginning of the file, without modification, 11.\" this list of conditions, and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20.\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd January 14, 2011 29.Dt LIBMAGIC 3 30.Os 31.Sh NAME 32.Nm magic_open , 33.Nm magic_close , 34.Nm magic_error , 35.Nm magic_descriptor , 36.Nm magic_buffer , 37.Nm magic_setflags , 38.Nm magic_check , 39.Nm magic_compile , 40.Nm magic_load 41.Nd Magic number recognition library. 42.Sh LIBRARY 43.Lb libmagic 44.Sh SYNOPSIS 45.In magic.h 46.Ft magic_t 47.Fn magic_open "int flags" 48.Ft void 49.Fn magic_close "magic_t cookie" 50.Ft const char * 51.Fn magic_error "magic_t cookie" 52.Ft int 53.Fn magic_errno "magic_t cookie" 54.Ft const char * 55.Fn magic_descriptor "magic_t cookie, "int fd" 56.Ft const char * 57.Fn magic_file "magic_t cookie, const char *filename" 58.Ft const char * 59.Fn magic_buffer "magic_t cookie, const void *buffer, size_t length" 60.Ft int 61.Fn magic_setflags "magic_t cookie, int flags" 62.Ft int 63.Fn magic_check "magic_t cookie, const char *filename" 64.Ft int 65.Fn magic_compile "magic_t cookie, const char *filename" 66.Ft int 67.Fn magic_load "magic_t cookie, const char *filename" 68.Sh DESCRIPTION 69These functions 70operate on the magic database file 71which is described 72in 73.Xr magic __FSECTION__ . 74.Pp 75The function 76.Fn magic_open 77creates a magic cookie pointer and returns it. It returns NULL if 78there was an error allocating the magic cookie. The 79.Ar flags 80argument specifies how the other magic functions should behave: 81.Bl -tag -width MAGIC_COMPRESS 82.It Dv MAGIC_NONE 83No special handling. 84.It Dv MAGIC_DEBUG 85Print debugging messages to stderr. 86.It Dv MAGIC_SYMLINK 87If the file queried is a symlink, follow it. 88.It Dv MAGIC_COMPRESS 89If the file is compressed, unpack it and look at the contents. 90.It Dv MAGIC_DEVICES 91If the file is a block or character special device, then open the device 92and try to look in its contents. 93.It Dv MAGIC_MIME_TYPE 94Return a MIME type string, instead of a textual description. 95.It Dv MAGIC_MIME_ENCODING 96Return a MIME encoding, instead of a textual description. 97.It Dv MAGIC_MIME 98A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING. 99.It Dv MAGIC_CONTINUE 100Return all matches, not just the first. 101.It Dv MAGIC_CHECK 102Check the magic database for consistency and print warnings to stderr. 103.It Dv MAGIC_PRESERVE_ATIME 104On systems that support 105.Xr utime 2 106or 107.Xr utimes 2 , 108attempt to preserve the access time of files analysed. 109.It Dv MAGIC_RAW 110Don't translate unprintable characters to a \eooo octal representation. 111.It Dv MAGIC_ERROR 112Treat operating system errors while trying to open files and follow symlinks 113as real errors, instead of printing them in the magic buffer. 114.It Dv MAGIC_APPLE 115Return the Apple creator and type. 116.It Dv MAGIC_NO_CHECK_APPTYPE 117Don't check for 118.Dv EMX 119application type (only on EMX). 120.It Dv MAGIC_NO_CHECK_CDF 121Don't get extra information on MS Composite Document Files. 122.It Dv MAGIC_NO_CHECK_COMPRESS 123Don't look inside compressed files. 124.It Dv MAGIC_NO_CHECK_ELF 125Don't print ELF details. 126.It Dv NO_CHECK_ENCODING 127Don't check text encodings. 128.It Dv MAGIC_NO_CHECK_SOFT 129Don't consult magic files. 130.It Dv MAGIC_NO_CHECK_TAR 131Don't examine tar files. 132.It Dv MAGIC_NO_CHECK_TEXT 133Don't check for various types of text files. 134.It Dv MAGIC_NO_CHECK_TOKENS 135Don't look for known tokens inside ascii files. 136.El 137.Pp 138The 139.Fn magic_close 140function closes the 141.Xr magic __FSECTION__ 142database and deallocates any resources used. 143.Pp 144The 145.Fn magic_error 146function returns a textual explanation of the last error, or NULL if there was 147no error. 148.Pp 149The 150.Fn magic_errno 151function returns the last operating system error number 152.Pq Xr errno 2 153that was encountered by a system call. 154.Pp 155The 156.Fn magic_file 157function returns a textual description of the contents of the 158.Ar filename 159argument, or NULL if an error occurred. 160If the 161.Ar filename 162is NULL, then stdin is used. 163.Pp 164The 165.Fn magic_descriptor 166function returns a textual description of the contents of the 167.Ar fd 168argument, or NULL if an error occurred. 169.Pp 170The 171.Fn magic_buffer 172function returns a textual description of the contents of the 173.Ar buffer 174argument with 175.Ar length 176bytes size. 177.Pp 178The 179.Fn magic_setflags 180function sets the 181.Ar flags 182described above. Note that using both MIME flags together can also 183return extra information on the charset. 184.Pp 185The 186.Fn magic_check 187function can be used to check the validity of entries in the colon 188separated database files passed in as 189.Ar filename , 190or NULL for the default database. It returns 0 on success and -1 on 191failure. 192.Pp 193The 194.Fn magic_compile 195function can be used to compile the the colon 196separated list of database files passed in as 197.Ar filename , 198or NULL for the default database. It returns 0 on success and -1 on 199failure. The compiled files created are named from the 200.Xr basename 1 201of each file argument with 202.Dq .mgc 203appended to it. 204.Pp 205The 206.Fn magic_load 207function must be used to load the the colon 208separated list of database files passed in as 209.Ar filename , 210or NULL for the default database file 211before any magic queries can performed. 212.Pp 213The default database file is named by the MAGIC environment variable. If 214that variable is not set, the default database file name is __MAGIC__. 215.Fn magic_load 216adds 217.Dq .mgc 218to the database filename as appropriate. 219.Sh RETURN VALUES 220The function 221.Fn magic_open 222returns a magic cookie on success and NULL on failure setting errno to 223an appropriate value. It will set errno to EINVAL if an unsupported 224value for flags was given. 225The 226.Fn magic_load , 227.Fn magic_compile , 228and 229.Fn magic_check 230functions return 0 on success and -1 on failure. 231The 232.Fn magic_file , 233and 234.Fn magic_buffer 235functions return a string on success and NULL on failure. The 236.Fn magic_error 237function returns a textual description of the errors of the above 238functions, or NULL if there was no error. 239Finally, 240.Fn magic_setflags 241returns -1 on systems that don't support 242.Xr utime 2 , 243or 244.Xr utimes 2 245when 246.Dv MAGIC_PRESERVE_ATIME 247is set. 248.Sh FILES 249.Bl -tag -width __MAGIC__.mgc -compact 250.It Pa __MAGIC__ 251The non-compiled default magic database. 252.It Pa __MAGIC__.mgc 253The compiled default magic database. 254.El 255.Sh SEE ALSO 256.Xr file __CSECTION__ , 257.Xr magic __FSECTION__ 258.Sh AUTHORS 259M�ns Rullg�rd Initial libmagic implementation, 260and configuration. 261.br 262Christos Zoulas API cleanup, error code and allocation handling. 263