mkdtemp can now use the full <inttypes.h>.
[gnulib.git] / lib / mkdtemp.c
1 /* Copyright (C) 1999, 2001-2003, 2006 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 /* Extracted from misc/mkdtemp.c and sysdeps/posix/tempname.c.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 /* Specification.  */
25 #include "mkdtemp.h"
26
27 #include <errno.h>
28 #ifndef __set_errno
29 # define __set_errno(Val) errno = (Val)
30 #endif
31
32 #include <stddef.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <stdio.h>
38 #ifndef TMP_MAX
39 # define TMP_MAX 238328
40 #endif
41
42 #include <unistd.h>
43
44 #if HAVE_GETTIMEOFDAY || _LIBC
45 # if HAVE_SYS_TIME_H || _LIBC
46 #  include <sys/time.h>
47 # endif
48 #else
49 # if HAVE_TIME_H || _LIBC
50 #  include <time.h>
51 # endif
52 #endif
53
54 #include <sys/stat.h>
55 #if !defined S_ISDIR && defined S_IFDIR
56 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
57 #endif
58 #if !S_IRUSR && S_IREAD
59 # define S_IRUSR S_IREAD
60 #endif
61 #if !S_IRUSR
62 # define S_IRUSR 00400
63 #endif
64 #if !S_IWUSR && S_IWRITE
65 # define S_IWUSR S_IWRITE
66 #endif
67 #if !S_IWUSR
68 # define S_IWUSR 00200
69 #endif
70 #if !S_IXUSR && S_IEXEC
71 # define S_IXUSR S_IEXEC
72 #endif
73 #if !S_IXUSR
74 # define S_IXUSR 00100
75 #endif
76
77 #ifdef __MINGW32__
78 # include <io.h>
79 /* mingw's _mkdir() function has 1 argument, but we pass 2 arguments.
80    Therefore we have to disable the argument count checking.  */
81 # define mkdir ((int (*)()) _mkdir)
82 #endif
83
84 #if !_LIBC
85 # define __getpid getpid
86 # define __gettimeofday gettimeofday
87 # define __mkdir mkdir
88 #endif
89
90 /* Use the widest available unsigned type if uint64_t is not
91    available.  The algorithm below extracts a number less than 62**6
92    (approximately 2**35.725) from uint64_t, so ancient hosts where
93    uintmax_t is only 32 bits lose about 3.725 bits of randomness,
94    which is better than not having mkstemp at all.  */
95 #if !defined UINT64_MAX && !defined uint64_t
96 # define uint64_t uintmax_t
97 #endif
98
99 /* These are the characters used in temporary filenames.  */
100 static const char letters[] =
101 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
102
103 /* Generate a temporary file name based on TMPL.  TMPL must match the
104    rules for mk[s]temp (i.e. end in "XXXXXX").  The name constructed
105    does not exist at the time of the call to __gen_tempname.  TMPL is
106    overwritten with the result.
107
108    KIND is:
109    __GT_DIR:            create a directory, which will be mode 0700.
110
111    We use a clever algorithm to get hard-to-predict names. */
112 static int
113 gen_tempname (char *tmpl)
114 {
115   int len;
116   char *XXXXXX;
117   static uint64_t value;
118   uint64_t random_time_bits;
119   int count, fd = -1;
120   int save_errno = errno;
121
122   len = strlen (tmpl);
123   if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
124     {
125       __set_errno (EINVAL);
126       return -1;
127     }
128
129   /* This is where the Xs start.  */
130   XXXXXX = &tmpl[len - 6];
131
132   /* Get some more or less random data.  */
133 #ifdef RANDOM_BITS
134   RANDOM_BITS (random_time_bits);
135 #else
136 # if HAVE_GETTIMEOFDAY || _LIBC
137   {
138     struct timeval tv;
139     __gettimeofday (&tv, NULL);
140     random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
141   }
142 # else
143   random_time_bits = time (NULL);
144 # endif
145 #endif
146   value += random_time_bits ^ __getpid ();
147
148   for (count = 0; count < TMP_MAX; value += 7777, ++count)
149     {
150       uint64_t v = value;
151
152       /* Fill in the random bits.  */
153       XXXXXX[0] = letters[v % 62];
154       v /= 62;
155       XXXXXX[1] = letters[v % 62];
156       v /= 62;
157       XXXXXX[2] = letters[v % 62];
158       v /= 62;
159       XXXXXX[3] = letters[v % 62];
160       v /= 62;
161       XXXXXX[4] = letters[v % 62];
162       v /= 62;
163       XXXXXX[5] = letters[v % 62];
164
165       fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
166
167       if (fd >= 0)
168         {
169           __set_errno (save_errno);
170           return fd;
171         }
172       else if (errno != EEXIST)
173         return -1;
174     }
175
176   /* We got out of the loop because we ran out of combinations to try.  */
177   __set_errno (EEXIST);
178   return -1;
179 }
180
181 /* Generate a unique temporary directory from TEMPLATE.
182    The last six characters of TEMPLATE must be "XXXXXX";
183    they are replaced with a string that makes the filename unique.
184    The directory is created, mode 700, and its name is returned.
185    (This function comes from OpenBSD.) */
186 char *
187 mkdtemp (char *template)
188 {
189   if (gen_tempname (template))
190     return NULL;
191   else
192     return template;
193 }