]> 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 else
72 dir = NULL;
73 }
74 if (dir != NULL)
75 {
76 if (direxists (P_tmpdir))
77 dir = P_tmpdir;
78 else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
79 dir = "/tmp";
80 else
81 {
82 __set_errno (ENOENT);
83 return -1;
84 }
85 }
86
87 dlen = strlen (dir);
88 while (dlen > 1 && dir[dlen - 1] == '/')
89 dlen--; /* remove trailing slashes */
90
91 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
92 if (tmpl_len < dlen + 1 + plen + 6 + 1)
93 {
94 __set_errno (EINVAL);
95 return -1;
96 }
97
98 sprintf (tmpl, "%*s/%*sXXXXXX", dlen, dir, plen, pfx);
99 return 0;
100 }
101
102 /* These are the characters used in temporary filenames. */
103 static const char letters[] =
104 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
105
106 /* Generate a temporary file name based on TMPL. TMPL must match the
107 rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
108 does not exist at the time of the call to __gen_tempname. TMPL is
109 overwritten with the result. If OPENIT is nonzero, creates the
110 file and returns a read-write fd; the file is mode 0600 modulo
111 umask. If LARGEFILE is nonzero, uses open64() instead of open().
112
113 We use a clever algorithm to get hard-to-predict names. */
114 int
115 __gen_tempname (char *tmpl, int openit, int largefile)
116 {
117 int len;
118 char *XXXXXX;
119 static uint64_t value;
120 struct timeval tv;
121 int count, fd;
122 int save_errno = errno;
123
124 len = strlen (tmpl);
125 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
126 {
127 __set_errno (EINVAL);
128 return -1;
129 }
130
131 /* This is where the Xs start. */
132 XXXXXX = &tmpl[len - 6];
133
134 /* Get some more or less random data. */
135 __gettimeofday (&tv, NULL);
136 value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ __getpid ();
137
138 for (count = 0; count < TMP_MAX; value += 7777, ++count)
139 {
140 uint64_t v = value;
141
142 /* Fill in the random bits. */
143 XXXXXX[0] = letters[v % 62];
144 v /= 62;
145 XXXXXX[1] = letters[v % 62];
146 v /= 62;
147 XXXXXX[2] = letters[v % 62];
148 v /= 62;
149 XXXXXX[3] = letters[v % 62];
150 v /= 62;
151 XXXXXX[4] = letters[v % 62];
152 v /= 62;
153 XXXXXX[5] = letters[v % 62];
154
155 if (openit)
156 {
157 fd = (largefile
158 ? __open (tmpl, O_RDWR | O_CREAT | O_EXCL, 0666)
159 : __open64 (tmpl, O_RDWR | O_CREAT | O_EXCL, 0666));
160 if (fd >= 0)
161 {
162 __set_errno (save_errno);
163 return fd;
164 }
165 else if (errno != EEXIST)
166 /* Any other error will apply also to other names we might
167 try, and there are 2^32 or so of them, so give up now. */
168 return -1;
169 }
170 else
171 {
172 struct stat st;
173 if (__xstat (_STAT_VER, tmpl, &st) < 0)
174 {
175 if (errno == ENOENT)
176 {
177 __set_errno (save_errno);
178 return 0;
179 }
180 else
181 /* Give up now. */
182 return -1;
183 }
184 }
185 }
186
187 /* We got out of the loop because we ran out of combinations to try. */
188 __set_errno (EEXIST);
189 return -1;
190 }