1 /* Copyright (C) 1999, 2001-2003 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. */
29 # define __set_errno(Val) errno = (Val)
38 # define TMP_MAX 238328
41 #if HAVE_STDINT_H || _LIBC
45 # include <inttypes.h>
48 #if HAVE_UNISTD_H || _LIBC
52 #if HAVE_GETTIMEOFDAY || _LIBC
53 # if HAVE_SYS_TIME_H || _LIBC
54 # include <sys/time.h>
57 # if HAVE_TIME_H || _LIBC
63 #if STAT_MACROS_BROKEN
66 #if !defined S_ISDIR && defined S_IFDIR
67 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
69 #if !S_IRUSR && S_IREAD
70 # define S_IRUSR S_IREAD
73 # define S_IRUSR 00400
75 #if !S_IWUSR && S_IWRITE
76 # define S_IWUSR S_IWRITE
79 # define S_IWUSR 00200
81 #if !S_IXUSR && S_IEXEC
82 # define S_IXUSR S_IEXEC
85 # define S_IXUSR 00100
89 /* mingw's mkdir() function has 1 argument, but we pass 2 arguments.
90 Therefore we have to disable the argument count checking. */
91 # define mkdir ((int (*)()) mkdir)
95 # define __getpid getpid
96 # define __gettimeofday gettimeofday
97 # define __mkdir mkdir
100 /* Use the widest available unsigned type if uint64_t is not
101 available. The algorithm below extracts a number less than 62**6
102 (approximately 2**35.725) from uint64_t, so ancient hosts where
103 uintmax_t is only 32 bits lose about 3.725 bits of randomness,
104 which is better than not having mkstemp at all. */
105 #if !defined UINT64_MAX && !defined uint64_t
106 # define uint64_t uintmax_t
109 /* These are the characters used in temporary filenames. */
110 static const char letters[] =
111 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
113 /* Generate a temporary file name based on TMPL. TMPL must match the
114 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
115 does not exist at the time of the call to __gen_tempname. TMPL is
116 overwritten with the result.
119 __GT_DIR: create a directory, which will be mode 0700.
121 We use a clever algorithm to get hard-to-predict names. */
123 gen_tempname (char *tmpl)
127 static uint64_t value;
128 uint64_t random_time_bits;
130 int save_errno = errno;
133 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
135 __set_errno (EINVAL);
139 /* This is where the Xs start. */
140 XXXXXX = &tmpl[len - 6];
142 /* Get some more or less random data. */
144 RANDOM_BITS (random_time_bits);
146 # if HAVE_GETTIMEOFDAY || _LIBC
149 __gettimeofday (&tv, NULL);
150 random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
153 random_time_bits = time (NULL);
156 value += random_time_bits ^ __getpid ();
158 for (count = 0; count < TMP_MAX; value += 7777, ++count)
162 /* Fill in the random bits. */
163 XXXXXX[0] = letters[v % 62];
165 XXXXXX[1] = letters[v % 62];
167 XXXXXX[2] = letters[v % 62];
169 XXXXXX[3] = letters[v % 62];
171 XXXXXX[4] = letters[v % 62];
173 XXXXXX[5] = letters[v % 62];
175 fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
179 __set_errno (save_errno);
182 else if (errno != EEXIST)
186 /* We got out of the loop because we ran out of combinations to try. */
187 __set_errno (EEXIST);
191 /* Generate a unique temporary directory from TEMPLATE.
192 The last six characters of TEMPLATE must be "XXXXXX";
193 they are replaced with a string that makes the filename unique.
194 The directory is created, mode 700, and its name is returned.
195 (This function comes from OpenBSD.) */
197 mkdtemp (char *template)
199 if (gen_tempname (template))