1 /* Copyright (C) 1999, 2001-2003, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Extracted from misc/mkdtemp.c and sysdeps/posix/tempname.c. */
27 # define __set_errno(Val) errno = (Val)
37 # define TMP_MAX 238328
42 #if HAVE_GETTIMEOFDAY || _LIBC
43 # if HAVE_SYS_TIME_H || _LIBC
44 # include <sys/time.h>
47 # if HAVE_TIME_H || _LIBC
53 #if !defined S_ISDIR && defined S_IFDIR
54 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
56 #if !S_IRUSR && S_IREAD
57 # define S_IRUSR S_IREAD
60 # define S_IRUSR 00400
62 #if !S_IWUSR && S_IWRITE
63 # define S_IWUSR S_IWRITE
66 # define S_IWUSR 00200
68 #if !S_IXUSR && S_IEXEC
69 # define S_IXUSR S_IEXEC
72 # define S_IXUSR 00100
77 /* mingw's _mkdir() function has 1 argument, but we pass 2 arguments.
78 Therefore we have to disable the argument count checking. */
79 # define mkdir ((int (*)()) _mkdir)
83 # define __getpid getpid
84 # define __gettimeofday gettimeofday
85 # define __mkdir mkdir
88 /* Use the widest available unsigned type if uint64_t is not
89 available. The algorithm below extracts a number less than 62**6
90 (approximately 2**35.725) from uint64_t, so ancient hosts where
91 uintmax_t is only 32 bits lose about 3.725 bits of randomness,
92 which is better than not having mkstemp at all. */
93 #if !defined UINT64_MAX && !defined uint64_t
94 # define uint64_t uintmax_t
97 /* These are the characters used in temporary filenames. */
98 static const char letters[] =
99 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
101 /* Generate a temporary file name based on TMPL. TMPL must match the
102 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
103 does not exist at the time of the call to __gen_tempname. TMPL is
104 overwritten with the result.
107 __GT_DIR: create a directory, which will be mode 0700.
109 We use a clever algorithm to get hard-to-predict names. */
111 gen_tempname (char *tmpl)
115 static uint64_t value;
116 uint64_t random_time_bits;
119 int save_errno = errno;
121 /* A lower bound on the number of temporary files to attempt to
122 generate. The maximum total number of temporary file names that
123 can exist for a given template is 62**6. It should never be
124 necessary to try all these combinations. Instead if a reasonable
125 number of names is tried (we define reasonable as 62**3) fail to
126 give the system administrator the chance to remove the problems. */
127 #define ATTEMPTS_MIN (62 * 62 * 62)
129 /* The number of times to attempt to generate a temporary file. To
130 conform to POSIX, this must be no smaller than TMP_MAX. */
131 #if ATTEMPTS_MIN < TMP_MAX
132 unsigned int attempts = TMP_MAX;
134 unsigned int attempts = ATTEMPTS_MIN;
138 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
140 __set_errno (EINVAL);
144 /* This is where the Xs start. */
145 XXXXXX = &tmpl[len - 6];
147 /* Get some more or less random data. */
149 RANDOM_BITS (random_time_bits);
151 # if HAVE_GETTIMEOFDAY || _LIBC
154 __gettimeofday (&tv, NULL);
155 random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
158 random_time_bits = time (NULL);
161 value += random_time_bits ^ __getpid ();
163 for (count = 0; count < attempts; value += 7777, ++count)
167 /* Fill in the random bits. */
168 XXXXXX[0] = letters[v % 62];
170 XXXXXX[1] = letters[v % 62];
172 XXXXXX[2] = letters[v % 62];
174 XXXXXX[3] = letters[v % 62];
176 XXXXXX[4] = letters[v % 62];
178 XXXXXX[5] = letters[v % 62];
180 fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
184 __set_errno (save_errno);
187 else if (errno != EEXIST)
191 /* We got out of the loop because we ran out of combinations to try. */
192 __set_errno (EEXIST);
196 /* Generate a unique temporary directory from TEMPLATE.
197 The last six characters of TEMPLATE must be "XXXXXX";
198 they are replaced with a string that makes the filename unique.
199 The directory is created, mode 700, and its name is returned.
200 (This function comes from OpenBSD.) */
202 mkdtemp (char *template)
204 if (gen_tempname (template))