1 /* Copyright (C) 1991-1999, 2000, 2001 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. */
23 #include <sys/types.h>
28 # define __set_errno(Val) errno = (Val)
33 # define P_tmpdir "/tmp"
37 # define __GT_BIGFILE 1
39 # define __GT_NOCREATE 3
42 #if STDC_HEADERS || _LIBC
48 #if HAVE_FCNTL_H || _LIBC
52 #if HAVE_SYS_TIME_H || _LIBC
53 # include <sys/time.h>
56 #if HAVE_STDINT_H || _LIBC
60 #if HAVE_UNISTD_H || _LIBC
65 #if STAT_MACROS_BROKEN
68 #if !defined S_ISDIR && defined S_IFDIR
69 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
71 #if !S_IRUSR && S_IREAD
72 # define S_IRUSR S_IREAD
75 # define S_IRUSR 00400
77 #if !S_IWUSR && S_IWRITE
78 # define S_IWUSR S_IWRITE
81 # define S_IWUSR 00200
83 #if !S_IXUSR && S_IEXEC
84 # define S_IXUSR S_IEXEC
87 # define S_IXUSR 00100
91 # define struct_stat64 struct stat64
93 # define struct_stat64 struct stat
94 # define __getpid getpid
95 # define __gettimeofday gettimeofday
96 # define __mkdir mkdir
98 # define __open64 open
99 # define __lxstat64(version, path, buf) lstat (path, buf)
100 # define __xstat64(version, path, buf) stat (path, buf)
103 #if ! (HAVE___SECURE_GETENV || _LIBC)
104 # define __secure_getenv getenv
106 # ifndef HAVE_DECL_GETENV
107 "this configure-time declaration test was not run"
109 # if !HAVE_DECL_GETENV
114 /* Use the widest available unsigned type if uint64_t is not
115 available. The algorithm below extracts a number less than 62**6
116 (approximately 2**35.725) from uint64_t, so ancient hosts where
117 uintmax_t is only 32 bits lose about 3.725 bits of randomness,
118 which is better than not having mkstemp at all. */
119 #if !defined UINT64_MAX && !defined uint64_t
120 # define uint64_t uintmax_t
123 /* The total number of temporary file names that can exist for a given
124 template is 62**6. On ancient hosts where uint64_t is really 32
125 bits, TEMPORARIES evaluates to 965660736, which is good enough. */
126 #define TEMPORARIES ((uint64_t) 62 * 62 * 62 * 62 * 62 * 62)
128 /* Return nonzero if DIR is an existent directory. */
130 direxists (const char *dir)
133 return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode);
136 /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
137 non-null and exists, uses it; otherwise uses the first of $TMPDIR,
138 P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
139 for use with mk[s]temp. Will fail (-1) if DIR is non-null and
140 doesn't exist, none of the searched dirs exists, or there's not
141 enough space in TMPL. */
143 __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
163 d = __secure_getenv ("TMPDIR");
164 if (d != NULL && direxists (d))
166 else if (dir != NULL && direxists (dir))
173 if (direxists (P_tmpdir))
175 else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
179 __set_errno (ENOENT);
185 while (dlen > 1 && dir[dlen - 1] == '/')
186 dlen--; /* remove trailing slashes */
188 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
189 if (tmpl_len < dlen + 1 + plen + 6 + 1)
191 __set_errno (EINVAL);
195 sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
199 /* These are the characters used in temporary filenames. */
200 static const char letters[] =
201 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
203 /* Generate a temporary file name based on TMPL. TMPL must match the
204 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
205 does not exist at the time of the call to __gen_tempname. TMPL is
206 overwritten with the result.
209 __GT_NOCREATE: simply verify that the name does not exist
210 at the time of the call.
211 __GT_FILE: create the file using open(O_CREAT|O_EXCL)
212 and return a read-write fd. The file is mode 0600.
213 __GT_BIGFILE: same as __GT_FILE but use open64().
214 __GT_DIR: create a directory, which will be mode 0700.
216 We use a clever algorithm to get hard-to-predict names. */
218 __gen_tempname (char *tmpl, int kind)
222 static uint64_t value;
223 uint64_t random_time_bits;
226 int save_errno = errno;
230 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
232 __set_errno (EINVAL);
236 /* This is where the Xs start. */
237 XXXXXX = &tmpl[len - 6];
239 /* Get some more or less random data. */
240 #if HAVE_GETTIMEOFDAY || _LIBC
243 __gettimeofday (&tv, NULL);
244 random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
247 random_time_bits = time (NULL);
249 value += random_time_bits ^ __getpid ();
251 for (count = 0; count < TEMPORARIES; value += 7777, ++count)
255 /* Fill in the random bits. */
256 XXXXXX[0] = letters[v % 62];
258 XXXXXX[1] = letters[v % 62];
260 XXXXXX[2] = letters[v % 62];
262 XXXXXX[3] = letters[v % 62];
264 XXXXXX[4] = letters[v % 62];
266 XXXXXX[5] = letters[v % 62];
271 fd = __open (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
275 fd = __open64 (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
279 fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
283 /* This case is backward from the other three. __gen_tempname
284 succeeds if __xstat fails because the name does not exist.
285 Note the continue to bypass the common logic at the bottom
287 if (__lxstat64 (_STAT_VER, tmpl, &st) < 0)
291 __set_errno (save_errno);
301 assert (! "invalid KIND in __gen_tempname");
306 __set_errno (save_errno);
309 else if (errno != EEXIST)
313 /* We got out of the loop because we ran out of combinations to try. */
314 __set_errno (EEXIST);