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*                  Common Public License, Version 1.0                  *
7*                    by AT&T Intellectual Property                     *
8*                                                                      *
9*                A copy of the License is available at                 *
10*            http://www.opensource.org/licenses/cpl1.0.txt             *
11*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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
36static void
37drop(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
50char*
51mimetype(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