]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/posix/tempname.c
Update.
[thirdparty/glibc.git] / sysdeps / posix / tempname.c
1 /* Copyright (C) 1991,92,93,94,95,96,97,98 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 #include <stddef.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/time.h>
30
31 /* Return nonzero if DIR is an existent directory. */
32 static int
33 direxists (const char *dir)
34 {
35 struct stat buf;
36 return __xstat (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode);
37 }
38
39 /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
40 non-null and exists, uses it; otherwise uses the first of $TMPDIR,
41 P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
42 for use with mk[s]temp. Will fail (-1) if DIR is non-null and
43 doesn't exist, none of the searched dirs exists, or there's not
44 enough space in TMPL. */
45 int
46 __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
47 int try_tmpdir)
48 {
49 const char *d;
50 size_t dlen, plen;
51
52 if (!pfx || !pfx[0])
53 {
54 pfx = "file";
55 plen = 4;
56 }
57 else
58 {
59 plen = strlen (pfx);
60 if (plen > 5)
61 plen = 5;
62 }
63
64 if (try_tmpdir)
65 {
66 d = __secure_getenv ("TMPDIR");
67 if (d != NULL && direxists (d))
68 dir = d;
69 else if (dir != NULL && direxists (dir))
70 /* nothing */ ;
71 }
72 if (dir != NULL)
73 {
74 if (direxists (P_tmpdir))
75 dir = P_tmpdir;
76 else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
77 dir = "/tmp";
78 else
79 {
80 __set_errno (ENOENT);
81 return -1;
82 }
83 }
84
85 dlen = strlen (dir);
86 while (dlen > 1 && dir[dlen - 1] == '/')
87 dlen--; /* remove trailing slashes */
88
89 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
90 if (tmpl_len < dlen + 1 + plen + 6 + 1)
91 {
92 __set_errno (EINVAL);
93 return -1;
94 }
95
96 sprintf (tmpl, "%*s/%*sXXXXXX", dlen, dir, plen, pfx);
97 return 0;
98 }
99
100 /* These are the characters used in temporary filenames. */
101 static const char letters[] =
102 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
103
104 /* Generate a temporary file name based on TMPL. TMPL must match the
105 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
106 does not exist at the time of the call to __gen_tempname. TMPL is
107 overwritten with the result. If OPENIT is nonzero, creates the
108 file and returns a read-write fd; the file is mode 0600 modulo
109 umask. If LARGEFILE is nonzero, uses open64() instead of open().
110
111 We use a clever algorithm to get hard-to-predict names. */
112 int
113 __gen_tempname (char *tmpl, int openit, int largefile)
114 {
115 int len;
116 char *XXXXXX;
117 static uint64_t value;
118 struct timeval tv;
119 int count, fd;
120 int save_errno = errno;
121
122 len = strlen (tmpl);
123 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
124 {
125 __set_errno (EINVAL);
126 return -1;
127 }
128
129 /* This is where the Xs start. */
130 XXXXXX = &tmpl[len - 6];
131
132 /* Get some more or less random data. */
133 __gettimeofday (&tv, NULL);
134 value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ __getpid ();
135
136 for (count = 0; count < TMP_MAX; value += 7777, ++count)
137 {
138 uint64_t v = value;
139
140 /* Fill in the random bits. */
141 XXXXXX[0] = letters[v % 62];
142 v /= 62;
143 XXXXXX[1] = letters[v % 62];
144 v /= 62;
145 XXXXXX[2] = letters[v % 62];
146 v /= 62;
147 XXXXXX[3] = letters[v % 62];
148 v /= 62;
149 XXXXXX[4] = letters[v % 62];
150 v /= 62;
151 XXXXXX[5] = letters[v % 62];
152
153 if (openit)
154 {
155 fd = (largefile
156 ? __open (tmpl, O_RDWR | O_CREAT | O_EXCL, 0666)
157 : __open64 (tmpl, O_RDWR | O_CREAT | O_EXCL, 0666));
158 if (fd >= 0)
159 {
160 __set_errno (save_errno);
161 return fd;
162 }
163 else if (errno != EEXIST)
164 /* Any other error will apply also to other names we might
165 try, and there are 2^32 or so of them, so give up now. */
166 return -1;
167 }
168 else
169 {
170 struct stat st;
171 if (__xstat (_STAT_VER, tmpl, &st) < 0)
172 {
173 if (errno == ENOENT)
174 {
175 __set_errno (save_errno);
176 return 0;
177 }
178 else
179 /* Give up now. */
180 return -1;
181 }
182 }
183 }
184
185 /* We got out of the loop because we ran out of combinations to try. */
186 __set_errno (EEXIST);
187 return -1;
188 }