2006-09-01 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / utils / mono-stdlib.c
1 /*
2  * mono-stdlib.c: stdlib replacement functions.
3  * 
4  * Authors:
5  *      Gonzalo Paniagua Javier (gonzalo@novell.com)
6  *
7  * (C) 2006 Novell, Inc.  http://www.novell.com
8  *
9  */
10 #include <config.h>
11 #include <glib.h>
12 #include <errno.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include "mono-stdlib.h"
19
20 #ifndef HAVE_MKSTEMP
21 #ifndef O_BINARY
22 #define O_BINARY        0
23 #endif
24
25 int
26 mono_mkstemp (char *templ)
27 {
28         int ret;
29         int count = 27; /* Windows doc. */
30         char *t;
31         int len;
32
33         len = strlen (templ);
34         do {
35                 t = mktemp (templ);
36                 if (t == NULL) {
37                         errno = EINVAL;
38                         return -1;
39                 }
40
41                 if (*templ == '\0') {
42                         return -1;
43                 }
44
45                         
46                 ret = open (templ, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
47                 if (ret == -1 && errno != EEXIST) {
48                         return -1;
49                 }
50
51                 if (ret == -1) {
52                         memcpy (templ + len - 6, "XXXXXX", 6);
53                 }
54
55         } while (count-- > 0);
56
57         return ret;
58 }
59 #endif
60