diff -uNr orig/f new/f --- orig/f 1970-01-01 01:00:00.000000000 +0100 +++ new/f 2006-10-25 11:09:34.000000000 +0200 @@ -0,0 +1 @@ +csc -o magic.so -X easyffi -shared magic.scm -d0 -O2 -L -lmagic Binary files orig/magic.egg and new/magic.egg differ diff -uNr orig/magic.html new/magic.html --- orig/magic.html 2006-08-19 23:29:50.000000000 +0200 +++ new/magic.html 2006-10-25 11:32:09.000000000 +0200 @@ -187,6 +187,10 @@

Examples

+

+An example which does what the UNIX file(1) command does: +

+
(display "load extension") (newline)
 (require-extension magic)
 
@@ -202,6 +206,37 @@
 (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)
+
+
+
+

License

 
Copyright (c) 2005, Peter Busser
@@ -228,4 +263,4 @@
 POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file +
$Revision$ $Date$
  diff -uNr orig/magic.scm new/magic.scm --- orig/magic.scm 2006-08-19 23:28:49.000000000 +0200 +++ new/magic.scm 2006-10-25 11:10:58.000000000 +0200 @@ -1,9 +1,10 @@ ; csc -s magic.scm -L -lmagic +; Copyright (c) 2005,2006 by Peter Busser +; This file has been released under the BSD licence. See magic.html for the +; licence text. (declare - (usual-integrations) - (export magic-open @@ -13,6 +14,7 @@ magic-close magic-setflags magic-errno + magic-sanitize-mime-type MAGIC_NONE MAGIC_DEBUG @@ -25,18 +27,14 @@ MAGIC_PRESERVE_ATIME MAGIC_RAW MAGIC_ERROR - ) - -(use easyffi) + )) - (foreign-declare #< #include +<# -EOF -)) - -(foreign-parse #<! ___declare(export_constants, yes) #define MAGIC_NONE 0x000 /* No flags */ #define MAGIC_DEBUG 0x001 /* Turn on debugging */ @@ -49,8 +47,7 @@ #define MAGIC_PRESERVE_ATIME 0x080 /* Restore access time on exit */ #define MAGIC_RAW 0x100 /* Don't translate unprintable chars */ #define MAGIC_ERROR 0x200 /* Handle ENOENT etc as real errors */ -EOF -) +<# (define-foreign-type magic-t (pointer (struct "magic_set"))) @@ -75,3 +72,21 @@ (define magic-errno (foreign-lambda int "magic_errno" magic-t)) +; +; Sanitizes the mime-type provided by magic-file (with the MAGIC_MIME and +; MAGIC_SYMLINK flags set in magic-open). The mime-types stored in the +; file database sometimes do not contain useful information in which case +; this function provides an alternative. Or it contains additional garbage, +; in which case it strips the garbage. +; +(define (magic-sanitize-mime-type raw-mime-type path) + (if raw-mime-type + (if (string=? raw-mime-type "") + "unknown" + (if (directory? path) + "directory" + (car (string-split raw-mime-type ";,")))) + (if (symbolic-link? path) + "symlink" + "unknown"))) + diff -uNr orig/magic.setup new/magic.setup --- orig/magic.setup 2005-10-24 17:39:08.000000000 +0200 +++ new/magic.setup 2006-10-25 11:01:40.000000000 +0200 @@ -1,2 +1,2 @@ -(run (csc -o "magic.so" -shared "magic.scm" -d0 -O2 -L -lmagic)) +(run (csc -o "magic.so" -X easyffi -shared "magic.scm" -d0 -O2 -L -lmagic)) (install-extension 'magic '("magic.so" "magic.html") '((documentation "magic.html")))