mirror of
https://github.com/troglobit/libite.git
synced 2026-10-02 13:52:51 +07:00
tempfile(): use same fallback also when O_TMPFILE is missing
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
+16
-8
@@ -29,6 +29,18 @@
|
||||
#include <stdlib.h> /* mkostemp() */
|
||||
#include <sys/stat.h> /* umask() */
|
||||
|
||||
static FILE *fallback(void)
|
||||
{
|
||||
char nm[15] = _PATH_TMP "XXXXXXXX";
|
||||
int fd;
|
||||
|
||||
fd = mkostemp(nm, O_CLOEXEC);
|
||||
if (-1 == fd)
|
||||
return NULL;
|
||||
|
||||
return fdopen(fd, "w+");
|
||||
}
|
||||
|
||||
/**
|
||||
* A secure tmpfile() replacement
|
||||
*
|
||||
@@ -54,19 +66,15 @@ FILE *tempfile(void)
|
||||
fd = open(_PATH_TMP, O_TMPFILE | O_RDWR | O_EXCL | O_CLOEXEC, S_IRUSR | S_IWUSR);
|
||||
umask(oldmask);
|
||||
if (-1 == fd) {
|
||||
if (errno == EOPNOTSUPP) {
|
||||
char nm[15] = _PATH_TMP "XXXXXXXX";
|
||||
if (errno == EOPNOTSUPP)
|
||||
return fallback();
|
||||
|
||||
fd = mkostemp(nm, O_CLOEXEC);
|
||||
if (-1 == fd)
|
||||
return NULL;
|
||||
} else
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fdopen(fd, "w+");
|
||||
#else
|
||||
return tmpfile(); /* Fallback on older GLIBC/Linux and actual UNIX systems */
|
||||
return fallback();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user