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