1/*
2
3Copyright 1991, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27/*
28 * Author:  Keith Packard, MIT X Consortium
29 */
30
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34#include <X11/fonts/fntfilio.h>
35#include <X11/Xos.h>
36#ifndef O_BINARY
37#define O_BINARY O_RDONLY
38#endif
39#ifndef O_CLOEXEC
40#define O_CLOEXEC 0
41#endif
42#ifndef O_NOFOLLOW
43#define O_NOFOLLOW 0
44#endif
45
46FontFilePtr
47FontFileOpen (const char *name)
48{
49    int		fd;
50    int		len;
51    BufFilePtr	raw, cooked;
52
53    fd = open (name, O_BINARY|O_CLOEXEC|O_NOFOLLOW);
54    if (fd < 0)
55	return 0;
56    raw = BufFileOpenRead (fd);
57    if (!raw)
58    {
59	close (fd);
60	return 0;
61    }
62    len = strlen (name);
63    if (len > 2 && !strcmp (name + len - 2, ".Z")) {
64	cooked = BufFilePushCompressed (raw);
65	if (!cooked) {
66	    BufFileClose (raw, TRUE);
67	    return 0;
68	}
69	raw = cooked;
70#ifdef X_GZIP_FONT_COMPRESSION
71    } else if (len > 3 && !strcmp (name + len - 3, ".gz")) {
72	cooked = BufFilePushZIP (raw);
73	if (!cooked) {
74	    BufFileClose (raw, TRUE);
75	    return 0;
76	}
77	raw = cooked;
78#endif
79#ifdef X_BZIP2_FONT_COMPRESSION
80    } else if (len > 4 && !strcmp (name + len - 4, ".bz2")) {
81	cooked = BufFilePushBZIP2 (raw);
82	if (!cooked) {
83	    BufFileClose (raw, TRUE);
84	    return 0;
85	}
86	raw = cooked;
87#endif
88    }
89    return (FontFilePtr) raw;
90}
91
92int
93FontFileClose (FontFilePtr f)
94{
95    return BufFileClose ((BufFilePtr) f, TRUE);
96}
97
98