]>
git.ipfire.org Git - thirdparty/glibc.git/blob - 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.
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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
25 #include <sys/types.h>
31 /* Return nonzero if DIR is an existent directory. */
33 direxists (const char *dir
)
36 return __xstat (_STAT_VER
, dir
, &buf
) == 0 && S_ISDIR (buf
.st_mode
);
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. */
46 __path_search (char *tmpl
, size_t tmpl_len
, const char *dir
, const char *pfx
,
66 d
= __secure_getenv ("TMPDIR");
67 if (d
!= NULL
&& direxists (d
))
69 else if (dir
!= NULL
&& direxists (dir
))
74 if (direxists (P_tmpdir
))
76 else if (strcmp (P_tmpdir
, "/tmp") != 0 && direxists ("/tmp"))
86 while (dlen
> 1 && dir
[dlen
- 1] == '/')
87 dlen
--; /* remove trailing slashes */
89 /* check we have room for "${dir}/${pfx}XXXXXX\0" */
90 if (tmpl_len
< dlen
+ 1 + plen
+ 6 + 1)
96 sprintf (tmpl
, "%*s/%*sXXXXXX", dlen
, dir
, plen
, pfx
);
100 /* These are the characters used in temporary filenames. */
101 static const char letters
[] =
102 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
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().
111 We use a clever algorithm to get hard-to-predict names. */
113 __gen_tempname (char *tmpl
, int openit
, int largefile
)
117 static uint64_t value
;
120 int save_errno
= errno
;
123 if (len
< 6 || strcmp (&tmpl
[len
- 6], "XXXXXX"))
125 __set_errno (EINVAL
);
129 /* This is where the Xs start. */
130 XXXXXX
= &tmpl
[len
- 6];
132 /* Get some more or less random data. */
133 __gettimeofday (&tv
, NULL
);
134 value
+= ((uint64_t) tv
.tv_usec
<< 16) ^ tv
.tv_sec
^ __getpid ();
136 for (count
= 0; count
< TMP_MAX
; value
+= 7777, ++count
)
140 /* Fill in the random bits. */
141 XXXXXX
[0] = letters
[v
% 62];
143 XXXXXX
[1] = letters
[v
% 62];
145 XXXXXX
[2] = letters
[v
% 62];
147 XXXXXX
[3] = letters
[v
% 62];
149 XXXXXX
[4] = letters
[v
% 62];
151 XXXXXX
[5] = letters
[v
% 62];
156 ? __open (tmpl
, O_RDWR
| O_CREAT
| O_EXCL
, 0666)
157 : __open64 (tmpl
, O_RDWR
| O_CREAT
| O_EXCL
, 0666));
160 __set_errno (save_errno
);
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. */
171 if (__xstat (_STAT_VER
, tmpl
, &st
) < 0)
175 __set_errno (save_errno
);
185 /* We got out of the loop because we ran out of combinations to try. */
186 __set_errno (EEXIST
);