summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkira TAGOH <akira@tagoh.org>2013-01-22 12:03:28 +0900
committerAkira TAGOH <akira@tagoh.org>2013-01-22 12:11:02 +0900
commit6363193a0575cf6f58baf7f0a772ad8f92b7b904 (patch)
treec730a83d17d8061ac7ac08dd7043f594f7345dc7
parent9dbc282796e9a4d5a2a8cc7d1c8e29b9154e91c0 (diff)
Fix mkstemp absence for some platform
Patch from LRN and modified to make more generic.
-rw-r--r--src/fccache.c57
-rw-r--r--src/fccompat.c116
-rw-r--r--src/fcint.h3
3 files changed, 119 insertions, 57 deletions
diff --git a/src/fccache.c b/src/fccache.c
index 610b8f0..2c63125 100644
--- a/src/fccache.c
+++ b/src/fccache.c
@@ -31,7 +31,6 @@
31#include <dirent.h> 31#include <dirent.h>
32#include <string.h> 32#include <string.h>
33#include <sys/types.h> 33#include <sys/types.h>
34#include <time.h>
35#include <assert.h> 34#include <assert.h>
36#if defined(HAVE_MMAP) || defined(__CYGWIN__) 35#if defined(HAVE_MMAP) || defined(__CYGWIN__)
37# include <unistd.h> 36# include <unistd.h>
@@ -253,62 +252,6 @@ static FcCacheSkip *fcCacheChains[FC_CACHE_MAX_LEVEL];
253static int fcCacheMaxLevel; 252static int fcCacheMaxLevel;
254 253
255 254
256static int32_t
257FcRandom(void)
258{
259 int32_t result;
260
261#if HAVE_RANDOM_R
262 static struct random_data fcrandbuf;
263 static char statebuf[256];
264 static FcBool initialized = FcFalse;
265
266 if (initialized != FcTrue)
267 {
268 initstate_r(time(NULL), statebuf, 256, &fcrandbuf);
269 initialized = FcTrue;
270 }
271
272 random_r(&fcrandbuf, &result);
273#elif HAVE_RANDOM
274 static char statebuf[256];
275 char *state;
276 static FcBool initialized = FcFalse;
277
278 if (initialized != FcTrue)
279 {
280 state = initstate(time(NULL), statebuf, 256);
281 initialized = FcTrue;
282 }
283 else
284 state = setstate(statebuf);
285
286 result = random();
287
288 setstate(state);
289#elif HAVE_LRAND48
290 result = lrand48();
291#elif HAVE_RAND_R
292 static unsigned int seed = time(NULL);
293
294 result = rand_r(&seed);
295#elif HAVE_RAND
296 static FcBool initialized = FcFalse;
297
298 if (initialized != FcTrue)
299 {
300 srand(time(NULL));
301 initialized = FcTrue;
302 }
303 result = rand();
304#else
305# error no random number generator function available.
306#endif
307
308 return result;
309}
310
311
312static FcMutex *cache_lock; 255static FcMutex *cache_lock;
313 256
314static void 257static void
diff --git a/src/fccompat.c b/src/fccompat.c
index 5ec2135..0a9c135 100644
--- a/src/fccompat.c
+++ b/src/fccompat.c
@@ -31,11 +31,20 @@
31 31
32#include "fcint.h" 32#include "fcint.h"
33 33
34#include <errno.h>
35#if HAVE_SYS_TYPES_H
36#include <sys/types.h>
37#endif
38#if HAVE_SYS_STAT_H
39#include <sys/stat.h>
40#endif
34#if HAVE_FCNTL_H 41#if HAVE_FCNTL_H
35#include <fcntl.h> 42#include <fcntl.h>
36#endif 43#endif
37#include <stdarg.h> 44#include <stdarg.h>
38#include <stdlib.h> 45#include <stdlib.h>
46#include <string.h>
47#include <time.h>
39 48
40#ifdef O_CLOEXEC 49#ifdef O_CLOEXEC
41#define FC_O_CLOEXEC O_CLOEXEC 50#define FC_O_CLOEXEC O_CLOEXEC
@@ -47,6 +56,58 @@
47#else 56#else
48#define FC_O_LARGEFILE 0 57#define FC_O_LARGEFILE 0
49#endif 58#endif
59#ifdef O_BINARY
60#define FC_O_BINARY O_BINARY
61#else
62#define FC_O_BINARY 0
63#endif
64#ifdef O_TEMPORARY
65#define FC_O_TEMPORARY O_TEMPORARY
66#else
67#define FC_O_TEMPORARY 0
68#endif
69#ifdef O_NOINHERIT
70#define FC_O_NOINHERIT O_NOINHERIT
71#else
72#define FC_O_NOINHERIT 0
73#endif
74
75#if !defined (HAVE_MKOSTEMP) && !defined(HAVE_MKSTEMP) && !defined(HAVE__MKTEMP_S)
76static int
77mkstemp (char *template)
78{
79 static const char s[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
80 int fd, i;
81 size_t l;
82
83 if (template == NULL)
84 {
85 errno = EINVAL;
86 return -1;
87 }
88 l = strlen (template);
89 if (l < 6 || strcmp (&template[l - 6], "XXXXXX") != 0)
90 {
91 errno = EINVAL;
92 return -1;
93 }
94 do
95 {
96 errno = 0;
97 for (i = l - 6; i < l; i++)
98 {
99 int r = FcRandom ();
100 template[i] = s[r % 62];
101 }
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);
104 if (fd >= 0)
105 errno = 0;
106
107 return fd;
108}
109#define HAVE_MKSTEMP 1
110#endif
50 111
51int 112int
52FcOpen(const char *pathname, int flags, ...) 113FcOpen(const char *pathname, int flags, ...)
@@ -109,3 +170,58 @@ FcMakeTempfile (char *template)
109 170
110 return fd; 171 return fd;
111} 172}
173
174int32_t
175FcRandom(void)
176{
177 int32_t result;
178
179#if HAVE_RANDOM_R
180 static struct random_data fcrandbuf;
181 static char statebuf[256];
182 static FcBool initialized = FcFalse;
183
184 if (initialized != FcTrue)
185 {
186 initstate_r(time(NULL), statebuf, 256, &fcrandbuf);
187 initialized = FcTrue;
188 }
189
190 random_r(&fcrandbuf, &result);
191#elif HAVE_RANDOM
192 static char statebuf[256];
193 char *state;
194 static FcBool initialized = FcFalse;
195
196 if (initialized != FcTrue)
197 {
198 state = initstate(time(NULL), statebuf, 256);
199 initialized = FcTrue;
200 }
201 else
202 state = setstate(statebuf);
203
204 result = random();
205
206 setstate(state);
207#elif HAVE_LRAND48
208 result = lrand48();
209#elif HAVE_RAND_R
210 static unsigned int seed = time(NULL);
211
212 result = rand_r(&seed);
213#elif HAVE_RAND
214 static FcBool initialized = FcFalse;
215
216 if (initialized != FcTrue)
217 {
218 srand(time(NULL));
219 initialized = FcTrue;
220 }
221 result = rand();
222#else
223# error no random number generator function available.
224#endif
225
226 return result;
227}
diff --git a/src/fcint.h b/src/fcint.h
index 3cf526f..71b7341 100644
--- a/src/fcint.h
+++ b/src/fcint.h
@@ -724,6 +724,9 @@ FcOpen(const char *pathname, int flags, ...);
724FcPrivate int 724FcPrivate int
725FcMakeTempfile (char *template); 725FcMakeTempfile (char *template);
726 726
727FcPrivate int32_t
728FcRandom (void);
729
727/* fcdbg.c */ 730/* fcdbg.c */
728 731
729FcPrivate void 732FcPrivate void