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