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