1.\" $NetBSD: libmagic.3,v 1.6 2011/05/13 01:52:13 christos Exp $ 2.\" 3.\" $File: libmagic.man,v 1.23 2011/01/14 21:59:17 rrt 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 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 NULL if an error occurred. 179.Pp 180The 181.Fn magic_buffer 182function returns a textual description of the contents of the 183.Ar buffer 184argument with 185.Ar length 186bytes size. 187.Pp 188The 189.Fn magic_setflags 190function sets the 191.Ar flags 192described above. 193Note that using both MIME flags together can also 194return extra information on the charset. 195.Pp 196The 197.Fn magic_check 198function can be used to check the validity of entries in the colon 199separated database files passed in as 200.Ar filename , 201or 202.Dv NULL 203for the default database. 204It returns 0 on success and \-1 on failure. 205.Pp 206The 207.Fn magic_compile 208function can be used to compile the the colon 209separated list of database files passed in as 210.Ar filename , 211or 212.Dv NULL 213for the default database. 214It returns 0 on success and \-1 on failure. 215The compiled files created are named from the 216.Xr basename 1 217of each file argument with 218.Dq .mgc 219appended to it. 220.Pp 221The 222.Fn magic_load 223function must be used to load the the colon 224separated list of database files passed in as 225.Ar filename , 226or 227.Dv NULL 228for the default database file before any magic queries can performed. 229.Pp 230The default database file is named by the MAGIC environment variable. 231If that variable is not set, the default database file name is 232.Pa /usr/share/misc/magic . 233.Fn magic_load 234adds 235.Dq .mgc 236to the database filename as appropriate. 237.Sh RETURN VALUES 238The function 239.Fn magic_open 240returns a magic cookie on success and 241.Dv NULL 242on failure setting errno to an appropriate value. 243It will set errno to 244.Er EINVAL 245if an unsupported value for flags was given. 246The 247.Fn magic_load , 248.Fn magic_compile , 249and 250.Fn magic_check 251functions return 0 on success and \-1 on failure. 252The 253.Fn magic_file , 254and 255.Fn magic_buffer 256functions return a string on success and 257.Dv NULL 258on failure. 259The 260.Fn magic_error 261function returns a textual description of the errors of the above 262functions, or 263.Dv NULL 264if there was no error. 265Finally, 266.Fn magic_setflags 267returns \-1 on systems that don't support 268.Xr utime 3 , 269or 270.Xr utimes 2 271when 272.Dv MAGIC_PRESERVE_ATIME 273is set. 274.Sh FILES 275.Bl -tag -width /usr/share/misc/magic.mgc -compact 276.It Pa /usr/share/misc/magic 277The non-compiled default magic database. 278.It Pa /usr/share/misc/magic.mgc 279The compiled default magic database. 280.El 281.Sh SEE ALSO 282.Xr file 1 , 283.Xr magic 5 284.Sh AUTHORS 285.An M\(oans Rullg\(oard 286Initial libmagic implementation, and configuration. 287.An Christos Zoulas 288API cleanup, error code and allocation handling. 289