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