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.
36 #include <sys/types.h>
50 #define FC_O_CLOEXEC O_CLOEXEC
52 #define FC_O_CLOEXEC 0
55 #define FC_O_LARGEFILE O_LARGEFILE
57 #define FC_O_LARGEFILE 0
60 #define FC_O_BINARY O_BINARY
65 #define FC_O_TEMPORARY O_TEMPORARY
67 #define FC_O_TEMPORARY 0
70 #define FC_O_NOINHERIT O_NOINHERIT
72 #define FC_O_NOINHERIT 0
75 #if !defined (HAVE_MKOSTEMP) && !defined(HAVE_MKSTEMP) && !defined(HAVE__MKTEMP_S)
77 mkstemp (char *template)
79 static const char s[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
88 l = strlen (template);
89 if (l < 6 || strcmp (&template[l - 6], "XXXXXX") != 0)
97 for (i = l - 6; i < l; i++)
100 template[i] = s[r % 62];
102 fd = FcOpen (template, FC_O_BINARY | O_CREAT | O_EXCL | FC_O_TEMPORARY | FC_O_NOINHERIT | O_RDWR, 0600);
103 } while (fd < 0 && errno == EEXIST);
109 #define HAVE_MKSTEMP 1
113 FcOpen(const char *pathname, int flags, ...)
123 mode = (mode_t) va_arg(ap, int);
126 fd = open(pathname, flags | FC_O_CLOEXEC | FC_O_LARGEFILE, mode);
130 fd = open(pathname, flags | FC_O_CLOEXEC | FC_O_LARGEFILE);
137 FcMakeTempfile (char *template)
142 fd = mkostemp (template, FC_O_CLOEXEC);
144 fd = mkstemp (template);
145 # ifdef F_DUPFD_CLOEXEC
148 int newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDIN_FILENO);
153 # elif defined(FD_CLOEXEC)
156 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
160 if (_mktemp_s(template, strlen(template) + 1) != 0)
162 fd = FcOpen(template, O_RDWR | O_EXCL | O_CREAT, 0600);
174 static struct random_data fcrandbuf;
175 static char statebuf[256];
176 static FcBool initialized = FcFalse;
178 if (initialized != FcTrue)
180 initstate_r(time(NULL), statebuf, 256, &fcrandbuf);
181 initialized = FcTrue;
184 random_r(&fcrandbuf, &result);
186 static char statebuf[256];
188 static FcBool initialized = FcFalse;
190 if (initialized != FcTrue)
192 state = initstate(time(NULL), statebuf, 256);
193 initialized = FcTrue;
196 state = setstate(statebuf);
204 static unsigned int seed = time(NULL);
206 result = rand_r(&seed);
208 static FcBool initialized = FcFalse;
210 if (initialized != FcTrue)
213 initialized = FcTrue;
217 # error no random number generator function available.
225 #define mkdir(path,mode) _mkdir(path)
229 FcMakeDirectory (const FcChar8 *dir)
234 if (strlen ((char *) dir) == 0)
237 parent = FcStrDirname (dir);
240 if (access ((char *) parent, F_OK) == 0)
241 ret = mkdir ((char *) dir, 0755) == 0 && chmod ((char *) dir, 0755) == 0;
242 else if (access ((char *) parent, F_OK) == -1)
243 ret = FcMakeDirectory (parent) && (mkdir ((char *) dir, 0755) == 0) && chmod ((char *) dir, 0755) == 0;