These functions operate on the magic database file which is described in magic(5). For more information, consult the libmagic(3) manual page.
(magic-open FLAGS) -> MAGIC-T (magic-load MAGIC-T FILENAME) (magic-error MAGIC-T) -> STRING (magic-file MAGIC-T FILENAME) -> STRING (magic-close MAGIC-T) (magic-setflags MAGIC-T FLAGS) (magic-errno MAGIC-T) -> INT
MAGIC_NONE MAGIC_DEBUG MAGIC_SYMLINK MAGIC_COMPRESS MAGIC_DEVICES MAGIC_MIME MAGIC_CONTINUE MAGIC_CHECK MAGIC_PRESERVE_ATIME MAGIC_RAW MAGIC_ERROR
An example which does what the UNIX file(1) command does:
(display "load extension") (newline) (require-extension magic) (display "create cookie") (newline) (define cookie (magic-open (+ MAGIC_MIME MAGIC_SYMLINK))) (display "open the file") (newline) (magic-load cookie #f) (display "test file") (newline) (display (magic-file cookie "/bin/ls")) (newline) (display "close") (newline) (magic-close cookie)
An example which prints the mime-type and the sanitized mime-type of a file:
#!/usr/bin/csi -script (require-extension posix easyffi magic) (define (main args) (if (not (null? args)) (begin (define cookie (magic-open (+ MAGIC_MIME MAGIC_SYMLINK))) (magic-load cookie #f) (for-each (lambda (path) (let ((mime-type (magic-file cookie path))) (print path ": raw mime-type: " mime-type) (print path ": sanitized mime-type: " (magic-sanitize-mime-type mime-type path)))) args) (magic-close cookie)))) (main (command-line-arguments)) (exit 0)
Copyright (c) 2005, Peter Busser All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.