2008-10-21 Marcus Brinkmann <marcus@g10code.com>
[gnupg.git] / gl / mkdtemp.c
blob4cf86a0cca9787660f567de4f1f50c02dd8802d9
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 3, or (at your option)
7 any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Extracted from misc/mkdtemp.c and sysdeps/posix/tempname.c. */
19 #include <config.h>
21 /* Specification. */
22 #include "mkdtemp.h"
24 #include <errno.h>
25 #ifndef __set_errno
26 # define __set_errno(Val) errno = (Val)
27 #endif
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include <stdio.h>
35 #ifndef TMP_MAX
36 # define TMP_MAX 238328
37 #endif
39 #include <unistd.h>
41 #if HAVE_GETTIMEOFDAY || _LIBC
42 # if HAVE_SYS_TIME_H || _LIBC
43 # include <sys/time.h>
44 # endif
45 #else
46 # if HAVE_TIME_H || _LIBC
47 # include <time.h>
48 # endif
49 #endif
51 #include <sys/stat.h>
52 #if !defined S_ISDIR && defined S_IFDIR
53 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
54 #endif
55 #if !S_IRUSR && S_IREAD
56 # define S_IRUSR S_IREAD
57 #endif
58 #if !S_IRUSR
59 # define S_IRUSR 00400
60 #endif
61 #if !S_IWUSR && S_IWRITE
62 # define S_IWUSR S_IWRITE
63 #endif
64 #if !S_IWUSR
65 # define S_IWUSR 00200
66 #endif
67 #if !S_IXUSR && S_IEXEC
68 # define S_IXUSR S_IEXEC
69 #endif
70 #if !S_IXUSR
71 # define S_IXUSR 00100
72 #endif
74 #ifdef __MINGW32__
75 # include <io.h>
76 #endif
78 #if !_LIBC
79 # define __getpid getpid
80 # define __gettimeofday gettimeofday
81 # define __mkdir mkdir
82 #endif
84 /* Use the widest available unsigned type if uint64_t is not
85 available. The algorithm below extracts a number less than 62**6
86 (approximately 2**35.725) from uint64_t, so ancient hosts where
87 uintmax_t is only 32 bits lose about 3.725 bits of randomness,
88 which is better than not having mkstemp at all. */
89 #if !defined UINT64_MAX && !defined uint64_t
90 # define uint64_t uintmax_t
91 #endif
93 /* These are the characters used in temporary filenames. */
94 static const char letters[] =
95 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
97 /* Generate a temporary file name based on TMPL. TMPL must match the
98 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
99 does not exist at the time of the call to __gen_tempname. TMPL is
100 overwritten with the result.
102 KIND is:
103 __GT_DIR: create a directory, which will be mode 0700.
105 We use a clever algorithm to get hard-to-predict names. */
106 static int
107 gen_tempname (char *tmpl)
109 int len;
110 char *XXXXXX;
111 static uint64_t value;
112 uint64_t random_time_bits;
113 unsigned int count;
114 int fd = -1;
115 int save_errno = errno;
117 /* A lower bound on the number of temporary files to attempt to
118 generate. The maximum total number of temporary file names that
119 can exist for a given template is 62**6. It should never be
120 necessary to try all these combinations. Instead if a reasonable
121 number of names is tried (we define reasonable as 62**3) fail to
122 give the system administrator the chance to remove the problems. */
123 #define ATTEMPTS_MIN (62 * 62 * 62)
125 /* The number of times to attempt to generate a temporary file. To
126 conform to POSIX, this must be no smaller than TMP_MAX. */
127 #if ATTEMPTS_MIN < TMP_MAX
128 unsigned int attempts = TMP_MAX;
129 #else
130 unsigned int attempts = ATTEMPTS_MIN;
131 #endif
133 len = strlen (tmpl);
134 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
136 __set_errno (EINVAL);
137 return -1;
140 /* This is where the Xs start. */
141 XXXXXX = &tmpl[len - 6];
143 /* Get some more or less random data. */
144 #ifdef RANDOM_BITS
145 RANDOM_BITS (random_time_bits);
146 #else
147 # if HAVE_GETTIMEOFDAY || _LIBC
149 struct timeval tv;
150 __gettimeofday (&tv, NULL);
151 random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
153 # else
154 random_time_bits = time (NULL);
155 # endif
156 #endif
157 value += random_time_bits ^ __getpid ();
159 for (count = 0; count < attempts; value += 7777, ++count)
161 uint64_t v = value;
163 /* Fill in the random bits. */
164 XXXXXX[0] = letters[v % 62];
165 v /= 62;
166 XXXXXX[1] = letters[v % 62];
167 v /= 62;
168 XXXXXX[2] = letters[v % 62];
169 v /= 62;
170 XXXXXX[3] = letters[v % 62];
171 v /= 62;
172 XXXXXX[4] = letters[v % 62];
173 v /= 62;
174 XXXXXX[5] = letters[v % 62];
176 #ifdef MKDIR_TAKES_ONE_ARG
177 fd = mkdir (tmpl);
178 #else
179 fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
180 #endif
182 if (fd >= 0)
184 __set_errno (save_errno);
185 return fd;
187 else if (errno != EEXIST)
188 return -1;
191 /* We got out of the loop because we ran out of combinations to try. */
192 __set_errno (EEXIST);
193 return -1;
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.) */
201 char *
202 mkdtemp (char *template)
204 if (gen_tempname (template))
205 return NULL;
206 else
207 return template;