mirror of
https://github.com/troglobit/libite.git
synced 2026-11-01 13:00:33 +00:00
tempfile(): use same fallback also when O_TMPFILE is missing
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
fae83c9be3
commit
a712364d49
1 file changed
+16
-8
+16
-8
@@ -29,6 +29,18 @@
|
|||||||
#include <stdlib.h> /* mkostemp() */
|
#include <stdlib.h> /* mkostemp() */
|
||||||
#include <sys/stat.h> /* umask() */
|
#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
|
* 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);
|
fd = open(_PATH_TMP, O_TMPFILE | O_RDWR | O_EXCL | O_CLOEXEC, S_IRUSR | S_IWUSR);
|
||||||
umask(oldmask);
|
umask(oldmask);
|
||||||
if (-1 == fd) {
|
if (-1 == fd) {
|
||||||
if (errno == EOPNOTSUPP) {
|
if (errno == EOPNOTSUPP)
|
||||||
char nm[15] = _PATH_TMP "XXXXXXXX";
|
return fallback();
|
||||||
|
|
||||||
fd = mkostemp(nm, O_CLOEXEC);
|
return NULL;
|
||||||
if (-1 == fd)
|
|
||||||
return NULL;
|
|
||||||
} else
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fdopen(fd, "w+");
|
return fdopen(fd, "w+");
|
||||||
#else
|
#else
|
||||||
return tmpfile(); /* Fallback on older GLIBC/Linux and actual UNIX systems */
|
return fallback();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user