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