Fix a build fail on mingw
[platform/upstream/fontconfig.git] / src / fccompat.c
1 /*
2  * fontconfig/src/fccompat.c
3  *
4  * Copyright © 2012 Red Hat, Inc.
5  *
6  * Author(s):
7  *  Akira TAGOH
8  *
9  * Permission to use, copy, modify, distribute, and sell this software and its
10  * documentation for any purpose is hereby granted without fee, provided that
11  * the above copyright notice appear in all copies and that both that
12  * copyright notice and this permission notice appear in supporting
13  * documentation, and that the name of the author(s) not be used in
14  * advertising or publicity pertaining to distribution of the software without
15  * specific, written prior permission.  The authors make no
16  * representations about the suitability of this software for any purpose.  It
17  * is provided "as is" without express or implied warranty.
18  *
19  * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
20  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
21  * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
22  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
23  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
24  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
25  * PERFORMANCE OF THIS SOFTWARE.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "fcint.h"
33
34 #if HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37 #include <stdarg.h>
38 #include <stdlib.h>
39
40 #ifdef O_CLOEXEC
41 #define FC_O_CLOEXEC O_CLOEXEC
42 #else
43 #define FC_O_CLOEXEC 0
44 #endif
45 #ifdef O_LARGEFILE
46 #define FC_O_LARGEFILE O_LARGEFILE
47 #else
48 #define FC_O_LARGEFILE 0
49 #endif
50
51 int
52 FcOpen(const char *pathname, int flags, ...)
53 {
54     int fd = -1;
55
56     if (flags & O_CREAT)
57     {
58         va_list ap;
59         mode_t mode;
60
61         va_start(ap, flags);
62         mode = (mode_t) va_arg(ap, int);
63         va_end(ap);
64
65         fd = open(pathname, flags | FC_O_CLOEXEC | FC_O_LARGEFILE, mode);
66     }
67     else
68     {
69         fd = open(pathname, flags | FC_O_CLOEXEC | FC_O_LARGEFILE);
70     }
71
72     return fd;
73 }
74
75 int
76 FcMakeTempfile (char *template)
77 {
78     int fd = -1;
79
80 #if HAVE_MKOSTEMP
81     fd = mkostemp (template, FC_O_CLOEXEC);
82 #elif HAVE_MKSTEMP
83     fd = mkstemp (template);
84 #  ifdef F_DUPFD_CLOEXEC
85     if (fd != -1)
86     {
87         int newfd = fcntl(fd, F_DUPFD_CLOEXEC);
88
89         close(fd);
90         fd = newfd;
91     }
92 #  elif defined(FD_CLOEXEC)
93     if (fd != -1)
94     {
95         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
96     }
97 #  endif
98 #elif HAVE__MKTEMP_S
99    if (_mktemp_s(template, strlen(template) + 1) != 0)
100        return -1;
101    fd = FcOpen(template, O_RDWR | O_EXCL | O_CREAT, 0600);
102 #else
103 #error no secure functions to create a temporary file
104 #endif
105
106     return fd;
107 }