Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / mkdtemp.c
blob0866e543c298a5f981d43f5b1aa3e97bb155efa8
1 /* Copyright (C) 1999, 2001-2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
19 /* Extracted from misc/mkdtemp.c and sysdeps/posix/tempname.c. */
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 /* Specification. */
26 #include "mkdtemp.h"
28 #include <errno.h>
29 #ifndef __set_errno
30 # define __set_errno(Val) errno = (Val)
31 #endif
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include <stdio.h>
38 #ifndef TMP_MAX
39 # define TMP_MAX 238328
40 #endif
42 #if HAVE_STDINT_H || _LIBC
43 # include <stdint.h>
44 #endif
45 #if HAVE_INTTYPES_H
46 # include <inttypes.h>
47 #endif
49 #if HAVE_UNISTD_H || _LIBC
50 # include <unistd.h>
51 #endif
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
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
89 #ifdef __MINGW32__
90 /* mingw's mkdir() function has 1 argument, but we pass 2 arguments.
91 Therefore we have to disable the argument count checking. */
92 # define mkdir ((int (*)()) mkdir)
93 #endif
95 #if !_LIBC
96 # define __getpid getpid
97 # define __gettimeofday gettimeofday
98 # define __mkdir mkdir
99 #endif
101 /* Use the widest available unsigned type if uint64_t is not
102 available. The algorithm below extracts a number less than 62**6
103 (approximately 2**35.725) from uint64_t, so ancient hosts where
104 uintmax_t is only 32 bits lose about 3.725 bits of randomness,
105 which is better than not having mkstemp at all. */
106 #if !defined UINT64_MAX && !defined uint64_t
107 # define uint64_t uintmax_t
108 #endif
110 /* These are the characters used in temporary filenames. */
111 static const char letters[] =
112 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
114 /* Generate a temporary file name based on TMPL. TMPL must match the
115 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
116 does not exist at the time of the call to __gen_tempname. TMPL is
117 overwritten with the result.
119 KIND is:
120 __GT_DIR: create a directory, which will be mode 0700.
122 We use a clever algorithm to get hard-to-predict names. */
123 static int
124 gen_tempname (char *tmpl)
126 int len;
127 char *XXXXXX;
128 static uint64_t value;
129 uint64_t random_time_bits;
130 int count, fd = -1;
131 int save_errno = errno;
133 len = strlen (tmpl);
134 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
136 __set_errno (EINVAL);
137 return -1;
140 /* This is where the Xs start. */
141 XXXXXX = &tmpl[len - 6];
143 /* Get some more or less random data. */
144 #ifdef RANDOM_BITS
145 RANDOM_BITS (random_time_bits);
146 #else
147 # if HAVE_GETTIMEOFDAY || _LIBC
149 struct timeval tv;
150 __gettimeofday (&tv, NULL);
151 random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
153 # else
154 random_time_bits = time (NULL);
155 # endif
156 #endif
157 value += random_time_bits ^ __getpid ();
159 for (count = 0; count < TMP_MAX; value += 7777, ++count)
161 uint64_t v = value;
163 /* Fill in the random bits. */
164 XXXXXX[0] = letters[v % 62];
165 v /= 62;
166 XXXXXX[1] = letters[v % 62];
167 v /= 62;
168 XXXXXX[2] = letters[v % 62];
169 v /= 62;
170 XXXXXX[3] = letters[v % 62];
171 v /= 62;
172 XXXXXX[4] = letters[v % 62];
173 v /= 62;
174 XXXXXX[5] = letters[v % 62];
176 fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
178 if (fd >= 0)
180 __set_errno (save_errno);
181 return fd;
183 else if (errno != EEXIST)
184 return -1;
187 /* We got out of the loop because we ran out of combinations to try. */
188 __set_errno (EEXIST);
189 return -1;
192 /* Generate a unique temporary directory from TEMPLATE.
193 The last six characters of TEMPLATE must be "XXXXXX";
194 they are replaced with a string that makes the filename unique.
195 The directory is created, mode 700, and its name is returned.
196 (This function comes from OpenBSD.) */
197 char *
198 mkdtemp (char *template)
200 if (gen_tempname (template))
201 return NULL;
202 else
203 return template;