1 /* Copyright (C) 1991, 1992, 1996, 1998, 2004 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 Street - Fifth Floor,
17 Boston, MA 02110-1301, USA. */
23 #include <sys/types.h>
36 #ifdef HAVE_SYS_TIME_H
41 /* We need to provide a type for gcc_uint64_t. */
43 __extension__
typedef unsigned long long gcc_uint64_t
;
45 typedef unsigned long gcc_uint64_t
;
58 @deftypefn Replacement int mkstemps (char *@var{pattern}, int @var{suffix_len})
60 Generate a unique temporary file name from @var{pattern}.
61 @var{pattern} has the form:
64 @var{path}/ccXXXXXX@var{suffix}
67 @var{suffix_len} tells us how long @var{suffix} is (it can be zero
68 length). The last six characters of @var{pattern} before @var{suffix}
69 must be @samp{XXXXXX}; they are replaced with a string that makes the
70 filename unique. Returns a file descriptor open on the file for
78 mkstemps (char *pattern
, int suffix_len
)
80 static const char letters
[]
81 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
82 static gcc_uint64_t value
;
83 #ifdef HAVE_GETTIMEOFDAY
90 len
= strlen (pattern
);
92 if ((int) len
< 6 + suffix_len
93 || strncmp (&pattern
[len
- 6 - suffix_len
], "XXXXXX", 6))
98 XXXXXX
= &pattern
[len
- 6 - suffix_len
];
100 #ifdef HAVE_GETTIMEOFDAY
101 /* Get some more or less random data. */
102 gettimeofday (&tv
, NULL
);
103 value
+= ((gcc_uint64_t
) tv
.tv_usec
<< 16) ^ tv
.tv_sec
^ getpid ();
108 for (count
= 0; count
< TMP_MAX
; ++count
)
110 gcc_uint64_t v
= value
;
113 /* Fill in the random bits. */
114 XXXXXX
[0] = letters
[v
% 62];
116 XXXXXX
[1] = letters
[v
% 62];
118 XXXXXX
[2] = letters
[v
% 62];
120 XXXXXX
[3] = letters
[v
% 62];
122 XXXXXX
[4] = letters
[v
% 62];
124 XXXXXX
[5] = letters
[v
% 62];
126 fd
= open (pattern
, O_BINARY
|O_RDWR
|O_CREAT
|O_EXCL
, 0600);
128 /* The file does not exist. */
131 /* This is a random value. It is only necessary that the next
132 TMP_MAX values generated by adding 7777 to VALUE are different
133 with (module 2^32). */
137 /* We return the null string if we can't find a unique file name. */
This page took 0.036751 seconds and 4 git commands to generate.