1 /* Copyright (C) 1999, 2001-2003 Free Software Foundation, Inc.
2 This file is part of 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., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* Extracted from misc/mkdtemp.c and sysdeps/posix/tempname.c. */
30 # define __set_errno(Val) errno = (Val)
39 # define TMP_MAX 238328
42 #if HAVE_STDINT_H || _LIBC
46 # include <inttypes.h>
49 #if HAVE_UNISTD_H || _LIBC
53 #if HAVE_GETTIMEOFDAY || _LIBC
54 # if HAVE_SYS_TIME_H || _LIBC
55 # include <sys/time.h>
58 # if HAVE_TIME_H || _LIBC
64 #if STAT_MACROS_BROKEN
67 #if !defined S_ISDIR && defined S_IFDIR
68 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
70 #if !S_IRUSR && S_IREAD
71 # define S_IRUSR S_IREAD
74 # define S_IRUSR 00400
76 #if !S_IWUSR && S_IWRITE
77 # define S_IWUSR S_IWRITE
80 # define S_IWUSR 00200
82 #if !S_IXUSR && S_IEXEC
83 # define S_IXUSR S_IEXEC
86 # define S_IXUSR 00100
90 /* mingw's mkdir() function has 1 argument, but we pass 2 arguments.
91 Therefore we have to disable the argument count checking. */
92 # define mkdir ((int (*)()) mkdir)
96 # define __getpid getpid
97 # define __gettimeofday gettimeofday
98 # define __mkdir mkdir
101 /* Use the widest available unsigned type if uint64_t is not
102 available. The algorithm below extracts a number less than 62**6
103 (approximately 2**35.725) from uint64_t, so ancient hosts where
104 uintmax_t is only 32 bits lose about 3.725 bits of randomness,
105 which is better than not having mkstemp at all. */
106 #if !defined UINT64_MAX && !defined uint64_t
107 # define uint64_t uintmax_t
110 /* These are the characters used in temporary filenames. */
111 static const char letters
[] =
112 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
114 /* Generate a temporary file name based on TMPL. TMPL must match the
115 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
116 does not exist at the time of the call to __gen_tempname. TMPL is
117 overwritten with the result.
120 __GT_DIR: create a directory, which will be mode 0700.
122 We use a clever algorithm to get hard-to-predict names. */
124 gen_tempname (char *tmpl
)
128 static uint64_t value
;
129 uint64_t random_time_bits
;
131 int save_errno
= errno
;
134 if (len
< 6 || strcmp (&tmpl
[len
- 6], "XXXXXX"))
136 __set_errno (EINVAL
);
140 /* This is where the Xs start. */
141 XXXXXX
= &tmpl
[len
- 6];
143 /* Get some more or less random data. */
145 RANDOM_BITS (random_time_bits
);
147 # if HAVE_GETTIMEOFDAY || _LIBC
150 __gettimeofday (&tv
, NULL
);
151 random_time_bits
= ((uint64_t) tv
.tv_usec
<< 16) ^ tv
.tv_sec
;
154 random_time_bits
= time (NULL
);
157 value
+= random_time_bits
^ __getpid ();
159 for (count
= 0; count
< TMP_MAX
; value
+= 7777, ++count
)
163 /* Fill in the random bits. */
164 XXXXXX
[0] = letters
[v
% 62];
166 XXXXXX
[1] = letters
[v
% 62];
168 XXXXXX
[2] = letters
[v
% 62];
170 XXXXXX
[3] = letters
[v
% 62];
172 XXXXXX
[4] = letters
[v
% 62];
174 XXXXXX
[5] = letters
[v
% 62];
176 fd
= __mkdir (tmpl
, S_IRUSR
| S_IWUSR
| S_IXUSR
);
180 __set_errno (save_errno
);
183 else if (errno
!= EEXIST
)
187 /* We got out of the loop because we ran out of combinations to try. */
188 __set_errno (EEXIST
);
192 /* Generate a unique temporary directory from TEMPLATE.
193 The last six characters of TEMPLATE must be "XXXXXX";
194 they are replaced with a string that makes the filename unique.
195 The directory is created, mode 700, and its name is returned.
196 (This function comes from OpenBSD.) */
198 mkdtemp (char *template)
200 if (gen_tempname (template))