1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 /*
25  * mime/mailcap to magic support
26  */
27 
28 #include "mimelib.h"
29 
30 /*
31  * close magic handle
32  * done this way so that magic is only pulled in
33  * if mimetype() is used
34  */
35 
36 static void
drop(Mime_t * mp)37 drop(Mime_t* mp)
38 {
39 	if (mp->magic)
40 	{
41 		magicclose(mp->magic);
42 		mp->magic = 0;
43 	}
44 }
45 
46 /*
47  * return mime type for file
48  */
49 
50 char*
mimetype(Mime_t * mp,Sfio_t * fp,const char * file,struct stat * st)51 mimetype(Mime_t* mp, Sfio_t* fp, const char* file, struct stat* st)
52 {
53 	if (mp->disc->flags & MIME_NOMAGIC)
54 		return 0;
55 	if (!mp->magic)
56 	{
57 		mp->magicd.version = MAGIC_VERSION;
58 		mp->magicd.flags = MAGIC_MIME;
59 		mp->magicd.errorf = mp->disc->errorf;
60 		if (!(mp->magic = magicopen(&mp->magicd)))
61 		{
62 			mp->disc->flags |= MIME_NOMAGIC;
63 			return 0;
64 		}
65 		mp->freef = drop;
66 		magicload(mp->magic, NiL, 0);
67 	}
68 	return magictype(mp->magic, fp, file, st);
69 }
70