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>
49 #define FC_O_CLOEXEC O_CLOEXEC
51 #define FC_O_CLOEXEC 0
54 #define FC_O_LARGEFILE O_LARGEFILE
56 #define FC_O_LARGEFILE 0
59 #define FC_O_BINARY O_BINARY
64 #define FC_O_TEMPORARY O_TEMPORARY
66 #define FC_O_TEMPORARY 0
69 #define FC_O_NOINHERIT O_NOINHERIT
71 #define FC_O_NOINHERIT 0
74 #if !defined (HAVE_MKOSTEMP) && !defined(HAVE_MKSTEMP) && !defined(HAVE__MKTEMP_S)
76 mkstemp (char *template)
78 static const char s[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
87 l = strlen (template);
88 if (l < 6 || strcmp (&template[l - 6], "XXXXXX") != 0)
96 for (i = l - 6; i < l; i++)
99 template[i] = s[r % 62];
101 fd = FcOpen (template, FC_O_BINARY | O_CREAT | O_EXCL | FC_O_TEMPORARY | FC_O_NOINHERIT | O_RDWR, 0600);
102 } while (fd < 0 && errno == EEXIST);
108 #define HAVE_MKSTEMP 1
112 FcOpen(const char *pathname, int flags, ...)
122 mode = (mode_t) va_arg(ap, int);
125 fd = open(pathname, flags | FC_O_CLOEXEC | FC_O_LARGEFILE, mode);
129 fd = open(pathname, flags | FC_O_CLOEXEC | FC_O_LARGEFILE);
136 FcMakeTempfile (char *template)
141 fd = mkostemp (template, FC_O_CLOEXEC);
143 fd = mkstemp (template);
144 # ifdef F_DUPFD_CLOEXEC
147 int newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDIN_FILENO);
152 # elif defined(FD_CLOEXEC)
155 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
159 if (_mktemp_s(template, strlen(template) + 1) != 0)
161 fd = FcOpen(template, O_RDWR | O_EXCL | O_CREAT, 0600);
173 static struct random_data fcrandbuf;
174 static char statebuf[256];
175 static FcBool initialized = FcFalse;
181 if (initialized != FcTrue)
184 initstate_r (time (NULL), statebuf, 256, &retval, &fcrandbuf);
186 initstate_r (time (NULL), statebuf, 256, &fcrandbuf);
188 initialized = FcTrue;
192 random_r (&res, &fcrandbuf);
193 result = (int32_t)res;
195 random_r (&fcrandbuf, &result);
198 static char statebuf[256];
200 static FcBool initialized = FcFalse;
202 if (initialized != FcTrue)
204 state = initstate (time (NULL), statebuf, 256);
205 initialized = FcTrue;
208 state = setstate (statebuf);
216 static unsigned int seed = time (NULL);
218 result = rand_r (&seed);
220 static FcBool initialized = FcFalse;
222 if (initialized != FcTrue)
225 initialized = FcTrue;
229 # error no random number generator function available.
237 #define mkdir(path,mode) _mkdir(path)
241 FcMakeDirectory (const FcChar8 *dir)
246 if (strlen ((char *) dir) == 0)
249 parent = FcStrDirname (dir);
252 if (access ((char *) parent, F_OK) == 0)
253 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
254 else if (access ((char *) parent, F_OK) == -1)
255 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;
263 FcReadLink (const FcChar8 *pathname,
268 return readlink ((const char *) pathname, (char *)buf, bufsiz);
270 /* XXX: this function is only used for FcConfigRealFilename() so far
271 * and returning -1 as an error still just works.
279 #include "fcaliastail.h"