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