summaryrefslogtreecommitdiff
path: root/src/temp
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2013-08-02 01:06:53 -0400
committerRich Felker <dalias@aerifal.cx>2013-08-02 01:06:53 -0400
commit38f44d692310dd669ad9ee13a2993c91e81d2721 (patch)
treee967291cb8d67a12097fafe7afd633d30e9edff8 /src/temp
parent9a97d103fba259f3f789c143d97d7f22d392429b (diff)
downloadmusl-38f44d692310dd669ad9ee13a2993c91e81d2721.tar.gz
musl-38f44d692310dd669ad9ee13a2993c91e81d2721.tar.bz2
musl-38f44d692310dd669ad9ee13a2993c91e81d2721.tar.xz
musl-38f44d692310dd669ad9ee13a2993c91e81d2721.zip
fix (deprecated) mktemp logic and update it to match other temp functions
the access function cannot be used to check for existence, because it operates using real uid/gid rather than effective to determine accessibility; this matters for the non-final path components. instead, use stat. failure of stat is success if only the final component is missing (ENOENT) and otherwise is failure.
Diffstat (limited to 'src/temp')
-rw-r--r--src/temp/mktemp.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/temp/mktemp.c b/src/temp/mktemp.c
index a1c89a6c..67130e16 100644
--- a/src/temp/mktemp.c
+++ b/src/temp/mktemp.c
@@ -2,23 +2,30 @@
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
+#include <sys/stat.h>
char *__randname(char *);
char *mktemp(char *template)
{
size_t l = strlen(template);
- int retries = 10000;
+ int retries = 100;
+ struct stat st;
if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) {
errno = EINVAL;
*template = 0;
return template;
}
- while (retries--) {
+
+ do {
__randname(template+l-6);
- if (access(template, F_OK) < 0) return template;
- }
+ if (stat(template, &st)) {
+ if (errno != ENOENT) *template = 0;
+ return template;
+ }
+ } while (--retries);
+
*template = 0;
errno = EEXIST;
return template;