msvcrt: Implement _clearfp for arm.
[wine.git] / libs / port / mkstemps.c
blobba92b2aeeb3eb37d78bae86a12eb994f0c4c9583
1 /* Copyright (C) 1991, 1992, 1996, 1998 Free Software Foundation, Inc.
2 This file is derived from mkstemp.c from the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 Boston, MA 02110-1301, USA */
19 #include "config.h"
20 #include "wine/port.h"
22 #include <sys/types.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
35 #ifndef TMP_MAX
36 #define TMP_MAX 16384
37 #endif
41 @deftypefn Replacement int mkstemps (char *@var{template}, int @var{suffix_len})
43 Generate a unique temporary file name from @var{template}.
44 @var{template} has the form:
46 @example
47 @var{path}/ccXXXXXX@var{suffix}
48 @end example
50 @var{suffix_len} tells us how long @var{suffix} is (it can be zero
51 length). The last six characters of @var{template} before @var{suffix}
52 must be @samp{XXXXXX}; they are replaced with a string that makes the
53 filename unique. Returns a file descriptor open on the file for
54 reading and writing.
56 @end deftypefn
60 int
61 mkstemps (
62 char *template,
63 int suffix_len)
65 static const char letters[]
66 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
67 static unsigned __int64 value;
68 char *XXXXXX;
69 size_t len;
70 int count;
72 len = strlen (template);
74 if ((int) len < 6 + suffix_len
75 || strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6))
77 return -1;
80 XXXXXX = &template[len - 6 - suffix_len];
82 #ifndef _WIN32
84 struct timeval tv;
85 gettimeofday( &tv, NULL );
86 value += ((unsigned __int64) tv.tv_usec << 16) ^ tv.tv_sec;
88 #endif
89 value += getpid();
91 for (count = 0; count < TMP_MAX; ++count)
93 unsigned __int64 v = value;
94 int fd;
96 /* Fill in the random bits. */
97 XXXXXX[0] = letters[v % 62];
98 v /= 62;
99 XXXXXX[1] = letters[v % 62];
100 v /= 62;
101 XXXXXX[2] = letters[v % 62];
102 v /= 62;
103 XXXXXX[3] = letters[v % 62];
104 v /= 62;
105 XXXXXX[4] = letters[v % 62];
106 v /= 62;
107 XXXXXX[5] = letters[v % 62];
109 fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
110 if (fd >= 0)
111 /* The file does not exist. */
112 return fd;
114 /* This is a random value. It is only necessary that the next
115 TMP_MAX values generated by adding 7777 to VALUE are different
116 with (module 2^32). */
117 value += 7777;
120 return -1;