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 */
20 #include "wine/port.h"
22 #include <sys/types.h>
31 #ifdef HAVE_SYS_TIME_H
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:
47 @var{path}/ccXXXXXX@var{suffix}
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
65 static const char letters
[]
66 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
67 static unsigned __int64 value
;
72 len
= strlen (template);
74 if ((int) len
< 6 + suffix_len
75 || strncmp (&template[len
- 6 - suffix_len
], "XXXXXX", 6))
80 XXXXXX
= &template[len
- 6 - suffix_len
];
85 gettimeofday( &tv
, NULL
);
86 value
+= ((unsigned __int64
) tv
.tv_usec
<< 16) ^ tv
.tv_sec
;
91 for (count
= 0; count
< TMP_MAX
; ++count
)
93 unsigned __int64 v
= value
;
96 /* Fill in the random bits. */
97 XXXXXX
[0] = letters
[v
% 62];
99 XXXXXX
[1] = letters
[v
% 62];
101 XXXXXX
[2] = letters
[v
% 62];
103 XXXXXX
[3] = letters
[v
% 62];
105 XXXXXX
[4] = letters
[v
% 62];
107 XXXXXX
[5] = letters
[v
% 62];
109 fd
= open (template, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
111 /* The file does not exist. */
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). */