]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/posix/tempname.c
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
24 #include <sys/types.h>
34 /* Return nonzero if DIR is an existent directory. */
36 diraccess (const char *dir
)
39 return __stat (dir
, &buf
) == 0 && S_ISDIR (buf
.st_mode
);
42 /* Return nonzero if FILE exists. */
44 exists (const char *file
)
46 /* We can stat the file even if we can't read its data. */
49 if (__stat (file
, &st
) == 0)
53 /* We report that the file exists if stat failed for a reason other
54 than nonexistence. In this case, it may or may not exist, and we
55 don't know; but reporting that it does exist will never cause any
56 trouble, while reporting that it doesn't exist when it does would
57 violate the interface of __stdio_gen_tempname. */
58 int exists
= errno
!= ENOENT
;
65 /* These are the characters used in temporary filenames. */
66 static const char letters
[] =
67 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
69 /* Generate a temporary filename and return it (in a static buffer). If
70 STREAMPTR is not NULL, open a stream "w+b" on the file and set
71 *STREAMPTR to it. If DIR_SEARCH is nonzero, DIR and PFX are used as
72 described for tempnam. If not, a temporary filename in P_tmpdir with no
73 special prefix is generated. If LENPTR is not NULL, *LENPTR is set the
74 to length (including the terminating '\0') of the resultant filename,
75 which is returned. This goes through a cyclic pattern of all possible
76 filenames consisting of five decimal digits of the current pid and three
77 of the characters in `letters'. Data for tempnam and tmpnam is kept
78 separate, but when tempnam is using P_tmpdir and no prefix (i.e, it is
79 identical to tmpnam), the same data is used. Each potential filename is
80 tested for an already-existing file of the same name, and no name of an
81 existing file will be returned. When the cycle reaches its end
82 (12345ZZZ), NULL is returned. */
84 __stdio_gen_tempname (char *buf
, size_t bufsize
, const char *dir
,
85 const char *pfx
, int dir_search
, size_t *lenptr
,
89 static const char tmpdir
[] = P_tmpdir
;
90 static size_t indices
[2];
93 static pid_t oldpid
= (pid_t
) 0;
95 pid_t pid
= __getpid();
96 register size_t len
, plen
, dlen
;
101 register const char *d
= __secure_getenv ("TMPDIR");
102 if (d
!= NULL
&& !diraccess (d
))
104 if (d
== NULL
&& dir
!= NULL
&& diraccess (dir
))
106 if (d
== NULL
&& diraccess (tmpdir
))
108 if (d
== NULL
&& diraccess ("/tmp"))
112 __set_errno (ENOENT
);
122 /* Remove trailing slashes from the directory name. */
123 while (dlen
> 1 && dir
[dlen
- 1] == '/')
126 if (pfx
!= NULL
&& *pfx
!= '\0')
135 if (dir
!= tmpdir
&& !strcmp (dir
, tmpdir
))
137 idx
= &indices
[(plen
== 0 && dir
== tmpdir
) ? 1 : 0];
140 /* XXX Is this ever useful??? At least when using a thread package
141 which uses different PIDs for the threads it is not helpful. */
145 indices
[0] = indices
[1] = 0;
149 wrapped
= 0; /* We have not yet wrapped around the index counter. */
150 len
= dlen
+ 1 + plen
+ 5 + 3;
155 if (*idx
>= ((sizeof (letters
) - 1) * (sizeof (letters
) - 1) *
156 (sizeof (letters
) - 1)))
159 /* We really wrapped around this call. Can't believe it
160 but nevertheless stop the endless loop. */
163 indices
[0] = indices
[1] = 0;
169 /* Construct a file name and see if it already exists.
171 We use a single counter in *IDX to cycle each of three
172 character positions through each of 62 possible letters. */
174 if (__snprintf (buf
, bufsize
, "%.*s/%.*s%.5d%c%c%c",
175 (int) dlen
, dir
, (int) plen
,
177 letters
[i
% (sizeof (letters
) - 1)],
178 letters
[(i
/ (sizeof (letters
) - 1))
179 % (sizeof (letters
) - 1)],
180 letters
[(i
/ ((sizeof (letters
) - 1) *
181 (sizeof (letters
) - 1)))
182 % (sizeof (letters
) - 1)]
186 if (streamptr
!= NULL
)
188 /* Try to create the file atomically. */
189 int fd
= __open (buf
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
192 /* We got a new file that did not previously exist.
193 Create a stream for it. */
198 struct _IO_FILE_plus fp
;
203 struct _IO_FILE_plus
*fp
;
205 new_f
= (struct locked_FILE
*)
206 malloc (sizeof (struct locked_FILE
));
209 /* We lost trying to create a stream (out of memory?).
210 Nothing to do but remove the file, close the descriptor,
211 and return failure. */
221 fp
->file
._lock
= &new_f
->lock
;
223 _IO_init (&fp
->file
, 0);
224 _IO_JUMPS (&fp
->file
) = &_IO_file_jumps
;
225 _IO_file_init (&fp
->file
);
226 # if !_IO_UNIFIED_JUMPTABLES
229 if (_IO_file_attach (&fp
->file
, fd
) == NULL
)
235 fp
->file
._flags
&= ~_IO_DELETE_DONT_CLOSE
;
237 *streamptr
= (FILE *) fp
;
239 *streamptr
= __newstream ();
240 if (*streamptr
== NULL
)
242 /* We lost trying to create a stream (out of memory?).
243 Nothing to do but remove the file, close the descriptor,
244 and return failure. */
245 const int save
= errno
;
251 (*streamptr
)->__cookie
= (__ptr_t
) (long int) fd
;
252 (*streamptr
)->__mode
.__write
= 1;
253 (*streamptr
)->__mode
.__read
= 1;
254 (*streamptr
)->__mode
.__binary
= 1;
257 #if defined EMFILE || defined ENFILE || defined EINTR
269 /* We cannot open anymore files since all descriptors are
270 used or because we got a signal. */
276 else if (exists (buf
))
279 /* If the file already existed we have continued the loop above,
280 so we only get here when we have a winning name to return. */
282 __set_errno (saverrno
);
289 /* We got out of the loop because we ran out of combinations to try. */
290 __set_errno (EEXIST
); /* ? */