2 * fontconfig/src/fccompat.c
4 * Copyright © 2012 Red Hat, Inc.
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.
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.
32 #include <sys/types.h>
46 #define FC_O_CLOEXEC O_CLOEXEC
48 #define FC_O_CLOEXEC 0
51 #define FC_O_LARGEFILE O_LARGEFILE
53 #define FC_O_LARGEFILE 0
56 #define FC_O_BINARY O_BINARY
61 #define FC_O_TEMPORARY O_TEMPORARY
63 #define FC_O_TEMPORARY 0
66 #define FC_O_NOINHERIT O_NOINHERIT
68 #define FC_O_NOINHERIT 0
71 #if !defined (HAVE_MKOSTEMP) && !defined(HAVE_MKSTEMP) && !defined(HAVE__MKTEMP_S)
73 mkstemp (char *template)
75 static const char s[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
84 l = strlen (template);
85 if (l < 6 || strcmp (&template[l - 6], "XXXXXX") != 0)
93 for (i = l - 6; i < l; i++)
96 template[i] = s[r % 62];
98 fd = FcOpen (template, FC_O_BINARY | O_CREAT | O_EXCL | FC_O_TEMPORARY | FC_O_NOINHERIT | O_RDWR, 0600);
99 } while (fd < 0 && errno == EEXIST);
105 #define HAVE_MKSTEMP 1
109 FcOpen(const char *pathname, int flags, ...)
119 mode = (mode_t) va_arg(ap, int);
122 fd = open(pathname, flags | FC_O_CLOEXEC | FC_O_LARGEFILE, mode);
126 fd = open(pathname, flags | FC_O_CLOEXEC | FC_O_LARGEFILE);
133 FcMakeTempfile (char *template)
138 fd = mkostemp (template, FC_O_CLOEXEC);
140 fd = mkstemp (template);
141 # ifdef F_DUPFD_CLOEXEC
144 int newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDIN_FILENO);
149 # elif defined(FD_CLOEXEC)
152 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
156 if (_mktemp_s(template, strlen(template) + 1) != 0)
158 fd = FcOpen(template, O_RDWR | O_EXCL | O_CREAT, 0600);
170 static struct random_data fcrandbuf;
171 static char statebuf[256];
172 static FcBool initialized = FcFalse;
178 if (initialized != FcTrue)
181 initstate_r (time (NULL), statebuf, 256, &retval, &fcrandbuf);
183 initstate_r (time (NULL), statebuf, 256, &fcrandbuf);
185 initialized = FcTrue;
189 random_r (&res, &fcrandbuf);
190 result = (int32_t)res;
192 random_r (&fcrandbuf, &result);
195 static char statebuf[256];
197 static FcBool initialized = FcFalse;
199 if (initialized != FcTrue)
201 state = initstate (time (NULL), statebuf, 256);
202 initialized = FcTrue;
205 state = setstate (statebuf);
213 static unsigned int seed = time (NULL);
215 result = rand_r (&seed);
217 static FcBool initialized = FcFalse;
219 if (initialized != FcTrue)
222 initialized = FcTrue;
226 # error no random number generator function available.
234 #define mkdir(path,mode) _mkdir(path)
238 FcMakeDirectory (const FcChar8 *dir)
243 if (strlen ((char *) dir) == 0)
246 parent = FcStrDirname (dir);
249 if (access ((char *) parent, F_OK) == 0)
250 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
251 else if (access ((char *) parent, F_OK) == -1)
252 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
260 #include "fcaliastail.h"