Merge pull request #2955 from ludovic-henry/mono_msec_ticks-overflow
[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 #ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 #include "mono-stdlib.h"
21
22 #ifndef HAVE_MKSTEMP
23 #ifndef O_BINARY
24 #define O_BINARY        0
25 #endif
26
27 int
28 mono_mkstemp (char *templ)
29 {
30         int ret;
31         int count = 27; /* Windows doc. */
32         char *t;
33         int len;
34
35         len = strlen (templ);
36         do {
37                 t = mktemp (templ);
38                 if (t == NULL) {
39                         errno = EINVAL;
40                         return -1;
41                 }
42
43                 if (*templ == '\0') {
44                         return -1;
45                 }
46
47                 ret = open (templ, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
48                 if (ret == -1) {
49                         if (errno != EEXIST)
50                                 return -1;
51                         memcpy (templ + len - 6, "XXXXXX", 6);
52                 } else {
53                         break;
54                 }
55
56         } while (count-- > 0);
57
58         return ret;
59 }
60 #endif
61