1*79343712SPeter Avalos.\" $File: libmagic.man,v 1.19 2008/10/06 20:16:04 christos Exp $ 2327e51cbSPeter Avalos.\" 3327e51cbSPeter Avalos.\" Copyright (c) Christos Zoulas 2003. 4327e51cbSPeter Avalos.\" All Rights Reserved. 5327e51cbSPeter Avalos.\" 6327e51cbSPeter Avalos.\" Redistribution and use in source and binary forms, with or without 7327e51cbSPeter Avalos.\" modification, are permitted provided that the following conditions 8327e51cbSPeter Avalos.\" are met: 9327e51cbSPeter Avalos.\" 1. Redistributions of source code must retain the above copyright 10327e51cbSPeter Avalos.\" notice immediately at the beginning of the file, without modification, 11327e51cbSPeter Avalos.\" this list of conditions, and the following disclaimer. 12327e51cbSPeter Avalos.\" 2. Redistributions in binary form must reproduce the above copyright 13327e51cbSPeter Avalos.\" notice, this list of conditions and the following disclaimer in the 14327e51cbSPeter Avalos.\" documentation and/or other materials provided with the distribution. 15327e51cbSPeter Avalos.\" 16327e51cbSPeter Avalos.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17327e51cbSPeter Avalos.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18327e51cbSPeter Avalos.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19327e51cbSPeter Avalos.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20327e51cbSPeter Avalos.\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21327e51cbSPeter Avalos.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22327e51cbSPeter Avalos.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23327e51cbSPeter Avalos.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24327e51cbSPeter Avalos.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25327e51cbSPeter Avalos.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26327e51cbSPeter Avalos.\" SUCH DAMAGE. 27327e51cbSPeter Avalos.\" 28*79343712SPeter Avalos.Dd October 6, 2008 29327e51cbSPeter Avalos.Dt MAGIC 3 30327e51cbSPeter Avalos.Os 31327e51cbSPeter Avalos.Sh NAME 32327e51cbSPeter Avalos.Nm magic_open , 33327e51cbSPeter Avalos.Nm magic_close , 34327e51cbSPeter Avalos.Nm magic_error , 35327e51cbSPeter Avalos.Nm magic_file , 36327e51cbSPeter Avalos.Nm magic_buffer , 37327e51cbSPeter Avalos.Nm magic_setflags , 38327e51cbSPeter Avalos.Nm magic_check , 39327e51cbSPeter Avalos.Nm magic_compile , 40327e51cbSPeter Avalos.Nm magic_load 41327e51cbSPeter Avalos.Nd Magic number recognition library. 42327e51cbSPeter Avalos.Sh LIBRARY 43327e51cbSPeter Avalos.Lb libmagic 44327e51cbSPeter Avalos.Sh SYNOPSIS 45327e51cbSPeter Avalos.In magic.h 46327e51cbSPeter Avalos.Ft magic_t 47327e51cbSPeter Avalos.Fn magic_open "int flags" 48327e51cbSPeter Avalos.Ft void 49327e51cbSPeter Avalos.Fn magic_close "magic_t cookie" 50327e51cbSPeter Avalos.Ft const char * 51327e51cbSPeter Avalos.Fn magic_error "magic_t cookie" 52327e51cbSPeter Avalos.Ft int 53327e51cbSPeter Avalos.Fn magic_errno "magic_t cookie" 54327e51cbSPeter Avalos.Ft const char * 55327e51cbSPeter Avalos.Fn magic_file "magic_t cookie, const char *filename" 56327e51cbSPeter Avalos.Ft const char * 57327e51cbSPeter Avalos.Fn magic_buffer "magic_t cookie, const void *buffer, size_t length" 58327e51cbSPeter Avalos.Ft int 59327e51cbSPeter Avalos.Fn magic_setflags "magic_t cookie, int flags" 60327e51cbSPeter Avalos.Ft int 61327e51cbSPeter Avalos.Fn magic_check "magic_t cookie, const char *filename" 62327e51cbSPeter Avalos.Ft int 63327e51cbSPeter Avalos.Fn magic_compile "magic_t cookie, const char *filename" 64327e51cbSPeter Avalos.Ft int 65327e51cbSPeter Avalos.Fn magic_load "magic_t cookie, const char *filename" 66327e51cbSPeter Avalos.Sh DESCRIPTION 67327e51cbSPeter AvalosThese functions 68327e51cbSPeter Avalosoperate on the magic database file 69327e51cbSPeter Avaloswhich is described 70327e51cbSPeter Avalosin 71327e51cbSPeter Avalos.Xr magic __FSECTION__ . 72327e51cbSPeter Avalos.Pp 73327e51cbSPeter AvalosThe function 74327e51cbSPeter Avalos.Fn magic_open 75327e51cbSPeter Avaloscreates a magic cookie pointer and returns it. It returns NULL if 76327e51cbSPeter Avalosthere was an error allocating the magic cookie. The 77327e51cbSPeter Avalos.Ar flags 78327e51cbSPeter Avalosargument specifies how the other magic functions should behave: 79327e51cbSPeter Avalos.Bl -tag -width MAGIC_COMPRESS 80327e51cbSPeter Avalos.It Dv MAGIC_NONE 81327e51cbSPeter AvalosNo special handling. 82327e51cbSPeter Avalos.It Dv MAGIC_DEBUG 83327e51cbSPeter AvalosPrint debugging messages to stderr. 84327e51cbSPeter Avalos.It Dv MAGIC_SYMLINK 85327e51cbSPeter AvalosIf the file queried is a symlink, follow it. 86327e51cbSPeter Avalos.It Dv MAGIC_COMPRESS 87327e51cbSPeter AvalosIf the file is compressed, unpack it and look at the contents. 88327e51cbSPeter Avalos.It Dv MAGIC_DEVICES 89327e51cbSPeter AvalosIf the file is a block or character special device, then open the device 90327e51cbSPeter Avalosand try to look in its contents. 91327e51cbSPeter Avalos.It Dv MAGIC_MIME_TYPE 92327e51cbSPeter AvalosReturn a MIME type string, instead of a textual description. 93327e51cbSPeter Avalos.It Dv MAGIC_MIME_ENCODING 94327e51cbSPeter AvalosReturn a MIME encoding, instead of a textual description. 95327e51cbSPeter Avalos.It Dv MAGIC_CONTINUE 96327e51cbSPeter AvalosReturn all matches, not just the first. 97327e51cbSPeter Avalos.It Dv MAGIC_CHECK 98327e51cbSPeter AvalosCheck the magic database for consistency and print warnings to stderr. 99327e51cbSPeter Avalos.It Dv MAGIC_PRESERVE_ATIME 100327e51cbSPeter AvalosOn systems that support 101327e51cbSPeter Avalos.Xr utime 2 102327e51cbSPeter Avalosor 103327e51cbSPeter Avalos.Xr utimes 2 , 104327e51cbSPeter Avalosattempt to preserve the access time of files analyzed. 105327e51cbSPeter Avalos.It Dv MAGIC_RAW 106327e51cbSPeter AvalosDon't translate unprintable characters to a \eooo octal representation. 107327e51cbSPeter Avalos.It Dv MAGIC_ERROR 108327e51cbSPeter AvalosTreat operating system errors while trying to open files and follow symlinks 109327e51cbSPeter Avalosas real errors, instead of printing them in the magic buffer. 110327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_APPTYPE 111327e51cbSPeter AvalosCheck for 112327e51cbSPeter Avalos.Dv EMX 113327e51cbSPeter Avalosapplication type (only on EMX). 114327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_ASCII 115327e51cbSPeter AvalosCheck for various types of ascii files. 116327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_COMPRESS 117327e51cbSPeter AvalosDon't look for, or inside compressed files. 118327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_ELF 119327e51cbSPeter AvalosDon't print elf details. 120327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_FORTRAN 121327e51cbSPeter AvalosDon't look for fortran sequences inside ascii files. 122327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_SOFT 123327e51cbSPeter AvalosDon't consult magic files. 124327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_TAR 125327e51cbSPeter AvalosDon't examine tar files. 126327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_TOKENS 127327e51cbSPeter AvalosDon't look for known tokens inside ascii files. 128327e51cbSPeter Avalos.It Dv MAGIC_NO_CHECK_TROFF 129327e51cbSPeter AvalosDon't look for troff sequences inside ascii files. 130327e51cbSPeter Avalos.El 131327e51cbSPeter Avalos.Pp 132327e51cbSPeter AvalosThe 133327e51cbSPeter Avalos.Fn magic_close 134327e51cbSPeter Avalosfunction closes the 135327e51cbSPeter Avalos.Xr magic __FSECTION__ 136327e51cbSPeter Avalosdatabase and deallocates any resources used. 137327e51cbSPeter Avalos.Pp 138327e51cbSPeter AvalosThe 139327e51cbSPeter Avalos.Fn magic_error 140327e51cbSPeter Avalosfunction returns a textual explanation of the last error, or NULL if there was 141327e51cbSPeter Avalosno error. 142327e51cbSPeter Avalos.Pp 143327e51cbSPeter AvalosThe 144327e51cbSPeter Avalos.Fn magic_errno 145327e51cbSPeter Avalosfunction returns the last operating system error number 146327e51cbSPeter Avalos.Pq Xr errno 2 147327e51cbSPeter Avalosthat was encountered by a system call. 148327e51cbSPeter Avalos.Pp 149327e51cbSPeter AvalosThe 150327e51cbSPeter Avalos.Fn magic_file 151327e51cbSPeter Avalosfunction returns a textual description of the contents of the 152327e51cbSPeter Avalos.Ar filename 153327e51cbSPeter Avalosargument, or NULL if an error occurred. 154327e51cbSPeter AvalosIf the 155327e51cbSPeter Avalos.Ar filename 156327e51cbSPeter Avalosis NULL, then stdin is used. 157327e51cbSPeter Avalos.Pp 158327e51cbSPeter AvalosThe 159327e51cbSPeter Avalos.Fn magic_buffer 160327e51cbSPeter Avalosfunction returns a textual description of the contents of the 161327e51cbSPeter Avalos.Ar buffer 162327e51cbSPeter Avalosargument with 163327e51cbSPeter Avalos.Ar length 164327e51cbSPeter Avalosbytes size. 165327e51cbSPeter Avalos.Pp 166327e51cbSPeter AvalosThe 167327e51cbSPeter Avalos.Fn magic_setflags 168*79343712SPeter Avalosfunction sets the 169327e51cbSPeter Avalos.Ar flags 170327e51cbSPeter Avalosdescribed above. Note that using both MIME flags together can also 171327e51cbSPeter Avalosreturn extra information on the charset. 172327e51cbSPeter Avalos.Pp 173327e51cbSPeter AvalosThe 174327e51cbSPeter Avalos.Fn magic_check 175327e51cbSPeter Avalosfunction can be used to check the validity of entries in the colon 176327e51cbSPeter Avalosseparated database files passed in as 177327e51cbSPeter Avalos.Ar filename , 178327e51cbSPeter Avalosor NULL for the default database. It returns 0 on success and -1 on 179327e51cbSPeter Avalosfailure. 180327e51cbSPeter Avalos.Pp 181327e51cbSPeter AvalosThe 182327e51cbSPeter Avalos.Fn magic_compile 183327e51cbSPeter Avalosfunction can be used to compile the the colon 184327e51cbSPeter Avalosseparated list of database files passed in as 185327e51cbSPeter Avalos.Ar filename , 186327e51cbSPeter Avalosor NULL for the default database. It returns 0 on success and -1 on 187327e51cbSPeter Avalosfailure. The compiled files created are named from the 188327e51cbSPeter Avalos.Xr basename 1 189*79343712SPeter Avalosof each file argument with 190*79343712SPeter Avalos.Dq .mgc 191*79343712SPeter Avalosappended to it. 192327e51cbSPeter Avalos.Pp 193327e51cbSPeter AvalosThe 194327e51cbSPeter Avalos.Fn magic_load 195327e51cbSPeter Avalosfunction must be used to load the the colon 196327e51cbSPeter Avalosseparated list of database files passed in as 197327e51cbSPeter Avalos.Ar filename , 198327e51cbSPeter Avalosor NULL for the default database file 199327e51cbSPeter Avalosbefore any magic queries can performed. 200327e51cbSPeter Avalos.Pp 201327e51cbSPeter AvalosThe default database file is named by the MAGIC environment variable. If 202327e51cbSPeter Avalosthat variable is not set, the default database file name is __MAGIC__. 203327e51cbSPeter Avalos.Fn magic_load 204*79343712SPeter Avalosadds 205*79343712SPeter Avalos.Dq .mgc 206*79343712SPeter Avalosto the database filename as appropriate. 207327e51cbSPeter Avalos.Sh RETURN VALUES 208327e51cbSPeter AvalosThe function 209327e51cbSPeter Avalos.Fn magic_open 210327e51cbSPeter Avalosreturns a magic cookie on success and NULL on failure setting errno to 211327e51cbSPeter Avalosan appropriate value. It will set errno to EINVAL if an unsupported 212327e51cbSPeter Avalosvalue for flags was given. 213327e51cbSPeter AvalosThe 214327e51cbSPeter Avalos.Fn magic_load , 215327e51cbSPeter Avalos.Fn magic_compile , 216327e51cbSPeter Avalosand 217327e51cbSPeter Avalos.Fn magic_check 218327e51cbSPeter Avalosfunctions return 0 on success and -1 on failure. 219327e51cbSPeter AvalosThe 220327e51cbSPeter Avalos.Fn magic_file , 221327e51cbSPeter Avalosand 222327e51cbSPeter Avalos.Fn magic_buffer 223327e51cbSPeter Avalosfunctions return a string on success and NULL on failure. The 224327e51cbSPeter Avalos.Fn magic_error 225327e51cbSPeter Avalosfunction returns a textual description of the errors of the above 226327e51cbSPeter Avalosfunctions, or NULL if there was no error. 227327e51cbSPeter AvalosFinally, 228327e51cbSPeter Avalos.Fn magic_setflags 229327e51cbSPeter Avalosreturns -1 on systems that don't support 230327e51cbSPeter Avalos.Xr utime 2 , 231327e51cbSPeter Avalosor 232327e51cbSPeter Avalos.Xr utimes 2 233327e51cbSPeter Avaloswhen 234327e51cbSPeter Avalos.Dv MAGIC_PRESERVE_ATIME 235327e51cbSPeter Avalosis set. 236327e51cbSPeter Avalos.Sh FILES 237*79343712SPeter Avalos.Bl -tag -width __MAGIC__.mgc -compact 238327e51cbSPeter Avalos.It Pa __MAGIC__ 239327e51cbSPeter AvalosThe non-compiled default magic database. 240327e51cbSPeter Avalos.It Pa __MAGIC__.mgc 241327e51cbSPeter AvalosThe compiled default magic database. 242327e51cbSPeter Avalos.El 243327e51cbSPeter Avalos.Sh SEE ALSO 244327e51cbSPeter Avalos.Xr file __CSECTION__ , 245327e51cbSPeter Avalos.Xr magic __FSECTION__ 246327e51cbSPeter Avalos.Sh AUTHORS 247327e51cbSPeter AvalosM�ns Rullg�rd Initial libmagic implementation, 248327e51cbSPeter Avalosand configuration. 249327e51cbSPeter AvalosChristos Zoulas API cleanup, error code and allocation handling. 250