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