828
|
1 /* Copyright (C) 1991, 1992, 1993 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 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 General Public License for more details. |
|
13 |
|
14 You should have received a copy of the GNU General Public |
|
15 License along with the GNU C Library; see the file COPYING. If |
1315
|
16 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
17 Boston, MA 02111-1307, USA. */ |
828
|
18 |
|
19 #ifdef HAVE_CONFIG_H |
1243
|
20 #include <config.h> |
828
|
21 #endif |
|
22 |
|
23 #ifndef HAVE_TEMPNAM |
|
24 |
|
25 #include <errno.h> |
|
26 #include <stddef.h> |
|
27 #include <stdio.h> |
|
28 #include <stdlib.h> |
|
29 #include <string.h> |
1350
|
30 |
|
31 #ifdef HAVE_UNISTD_H |
|
32 #include <sys/types.h> |
|
33 #include <unistd.h> |
|
34 #endif |
|
35 |
828
|
36 #include <fcntl.h> |
|
37 |
1056
|
38 #include "statdefs.h" |
|
39 |
828
|
40 #ifndef FILENAME_MAX |
|
41 #ifdef MAXPATHLEN |
|
42 #define FILENAME_MAX MAXPATHLEN |
|
43 #else |
|
44 #define FILENAME_MAX 1024 |
|
45 #endif |
|
46 #endif |
|
47 |
|
48 #ifndef P_tmpdir |
|
49 #define P_tmpdir "/usr/tmp/" |
|
50 #endif |
|
51 |
|
52 /* Return nonzero if DIR is an existent directory. */ |
|
53 static int |
|
54 diraccess (const char *dir) |
|
55 { |
|
56 struct stat buf; |
|
57 return stat (dir, &buf) == 0 && S_ISDIR (buf.st_mode); |
|
58 } |
|
59 |
|
60 /* Return nonzero if FILE exists. */ |
|
61 static int |
|
62 exists (const char *file) |
|
63 { |
|
64 /* We can stat the file even if we can't read its data. */ |
|
65 struct stat st; |
|
66 int save = errno; |
|
67 if (stat (file, &st) == 0) |
|
68 return 1; |
|
69 else |
|
70 { |
|
71 /* We report that the file exists if stat failed for a reason other |
|
72 than nonexistence. In this case, it may or may not exist, and we |
|
73 don't know; but reporting that it does exist will never cause any |
|
74 trouble, while reporting that it doesn't exist when it does would |
|
75 violate the interface of __stdio_gen_tempname. */ |
|
76 int exists = errno != ENOENT; |
|
77 errno = save; |
|
78 return exists; |
|
79 } |
|
80 } |
|
81 |
|
82 |
|
83 /* These are the characters used in temporary filenames. */ |
|
84 static const char letters[] = |
|
85 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
|
86 |
|
87 /* Generate a temporary filename and return it (in a static buffer). If |
|
88 STREAMPTR is not NULL, open a stream "w+b" on the file and set |
|
89 *STREAMPTR to it. If DIR_SEARCH is nonzero, DIR and PFX are used as |
|
90 described for tempnam. If not, a temporary filename in P_tmpdir with no |
|
91 special prefix is generated. If LENPTR is not NULL, *LENPTR is set the |
|
92 to length (including the terminating '\0') of the resultant filename, |
|
93 which is returned. This goes through a cyclic pattern of all possible |
|
94 filenames consisting of five decimal digits of the current pid and three |
|
95 of the characters in `letters'. Data for tempnam and tmpnam is kept |
|
96 separate, but when tempnam is using P_tmpdir and no prefix (i.e, it is |
|
97 identical to tmpnam), the same data is used. Each potential filename is |
|
98 tested for an already-existing file of the same name, and no name of an |
|
99 existing file will be returned. When the cycle reaches its end |
|
100 (12345ZZZ), NULL is returned. */ |
|
101 char * |
|
102 __stdio_gen_tempname (const char *dir, const char *pfx, |
|
103 int dir_search, size_t *lenptr, |
|
104 FILE **streamptr) |
|
105 { |
|
106 int saverrno = errno; |
|
107 static const char tmpdir[] = P_tmpdir; |
|
108 static size_t indices[2]; |
|
109 size_t *idx; |
|
110 static char buf[FILENAME_MAX]; |
|
111 static pid_t oldpid = (pid_t) 0; |
|
112 pid_t pid = getpid(); |
|
113 register size_t len, plen, dlen; |
|
114 |
|
115 if (dir_search) |
|
116 { |
|
117 register const char *d = getenv("TMPDIR"); |
|
118 if (d != NULL && !diraccess(d)) |
|
119 d = NULL; |
|
120 if (d == NULL && dir != NULL && diraccess(dir)) |
|
121 d = dir; |
|
122 if (d == NULL && diraccess(tmpdir)) |
|
123 d = tmpdir; |
|
124 if (d == NULL && diraccess("/tmp")) |
|
125 d = "/tmp"; |
|
126 if (d == NULL) |
|
127 { |
|
128 errno = ENOENT; |
|
129 return NULL; |
|
130 } |
|
131 dir = d; |
|
132 } |
|
133 else |
|
134 dir = tmpdir; |
|
135 |
|
136 dlen = strlen (dir); |
|
137 |
|
138 /* Remove trailing slashes from the directory name. */ |
|
139 while (dlen > 1 && dir[dlen - 1] == '/') |
|
140 --dlen; |
|
141 |
|
142 if (pfx != NULL && *pfx != '\0') |
|
143 { |
|
144 plen = strlen(pfx); |
|
145 if (plen > 5) |
|
146 plen = 5; |
|
147 } |
|
148 else |
|
149 plen = 0; |
|
150 |
|
151 if (dir != tmpdir && !strcmp(dir, tmpdir)) |
|
152 dir = tmpdir; |
|
153 idx = &indices[(plen == 0 && dir == tmpdir) ? 1 : 0]; |
|
154 |
|
155 if (pid != oldpid) |
|
156 { |
|
157 oldpid = pid; |
|
158 indices[0] = indices[1] = 0; |
|
159 } |
|
160 |
|
161 len = dlen + 1 + plen + 5 + 3; |
|
162 for (; *idx < ((sizeof (letters) - 1) * (sizeof (letters) - 1) * |
|
163 (sizeof (letters) - 1)); |
|
164 ++*idx) |
|
165 { |
|
166 /* Construct a file name and see if it already exists. |
|
167 |
|
168 We use a single counter in *IDX to cycle each of three |
|
169 character positions through each of 62 possible letters. */ |
|
170 |
|
171 if (sizeof (buf) < len) |
|
172 return NULL; |
|
173 |
|
174 sprintf (buf, "%.*s/%.*s%.5d%c%c%c", |
|
175 (int) dlen, dir, (int) plen, |
|
176 pfx, pid % 100000, |
|
177 letters[*idx |
|
178 % (sizeof (letters) - 1)], |
|
179 letters[(*idx / (sizeof (letters) - 1)) |
|
180 % (sizeof (letters) - 1)], |
|
181 letters[(*idx / ((sizeof (letters) - 1) * |
|
182 (sizeof (letters) - 1))) |
|
183 % (sizeof (letters) - 1)] |
|
184 ); |
|
185 |
|
186 if (! buf || strlen (buf) != (int) len) |
|
187 return NULL; |
|
188 |
|
189 if (streamptr != NULL) |
|
190 abort (); |
|
191 else if (exists (buf)) |
|
192 continue; |
|
193 |
|
194 /* If the file already existed we have continued the loop above, |
|
195 so we only get here when we have a winning name to return. */ |
|
196 |
|
197 errno = saverrno; |
|
198 |
|
199 if (lenptr != NULL) |
|
200 *lenptr = len + 1; |
|
201 return buf; |
|
202 } |
|
203 |
|
204 /* We got out of the loop because we ran out of combinations to try. */ |
|
205 errno = EEXIST; /* ? */ |
|
206 return NULL; |
|
207 } |
|
208 |
|
209 #endif |