summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorZefram <zefram@fysh.org>2017-12-19 18:32:05 +0000
committerZefram <zefram@fysh.org>2017-12-22 16:22:52 +0000
commit2517ba9951b6aed3ab780dc0247c90deff825b4e (patch)
tree0b6053a93c0cf53654d6546046df1d57ffddaa63 /util.c
parent226394c3368f2e223fb9d10e2b5408a6bf8608f5 (diff)
downloadperl-2517ba9951b6aed3ab780dc0247c90deff825b4e.tar.gz
portable Perl_my_mkostemp()
Akin to the existing Perl_my_mkstemp(), Perl_my_mkostemp() is defined as a macro for mkostemp() where available, and otherwise as our own implementation of it. The guts of our own implementations of the two functions are shared. Perl_my_mkostemp() is not guaranteed to handle O_CLOEXEC: that depends on open() handling it.
Diffstat (limited to 'util.c')
-rw-r--r--util.c30
1 files changed, 23 insertions, 7 deletions
diff --git a/util.c b/util.c
index 91ef4ec093..14f920424f 100644
--- a/util.c
+++ b/util.c
@@ -5627,24 +5627,22 @@ Perl_my_dirfd(DIR * dir) {
#endif
}
-#ifndef HAS_MKSTEMP
+#if !defined(HAS_MKOSTEMP) || !defined(HAS_MKSTEMP)
#define TEMP_FILE_CH "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789"
#define TEMP_FILE_CH_COUNT (sizeof(TEMP_FILE_CH)-1)
-int
-Perl_my_mkstemp(char *templte) {
+static int
+S_my_mkostemp(char *templte, int flags) {
dTHX;
STRLEN len = strlen(templte);
int fd;
int attempts = 0;
- PERL_ARGS_ASSERT_MY_MKSTEMP;
-
if (len < 6 ||
templte[len-1] != 'X' || templte[len-2] != 'X' || templte[len-3] != 'X' ||
templte[len-4] != 'X' || templte[len-5] != 'X' || templte[len-6] != 'X') {
- errno = EINVAL;
+ SETERRNO(EINVAL, LIB_INVARG);
return -1;
}
@@ -5653,7 +5651,7 @@ Perl_my_mkstemp(char *templte) {
for (i = 1; i <= 6; ++i) {
templte[len-i] = TEMP_FILE_CH[(int)(Perl_internal_drand48() * TEMP_FILE_CH_COUNT)];
}
- fd = PerlLIO_open3(templte, O_RDWR | O_CREAT | O_EXCL, 0600);
+ fd = PerlLIO_open3(templte, O_RDWR | O_CREAT | O_EXCL | flags, 0600);
} while (fd == -1 && errno == EEXIST && ++attempts <= 100);
return fd;
@@ -5661,6 +5659,24 @@ Perl_my_mkstemp(char *templte) {
#endif
+#ifndef HAS_MKOSTEMP
+int
+Perl_my_mkostemp(char *templte, int flags)
+{
+ PERL_ARGS_ASSERT_MY_MKOSTEMP;
+ return S_my_mkostemp(templte, flags);
+}
+#endif
+
+#ifndef HAS_MKSTEMP
+int
+Perl_my_mkstemp(char *templte)
+{
+ PERL_ARGS_ASSERT_MY_MKSTEMP;
+ return S_my_mkostemp(templte, 0);
+}
+#endif
+
REGEXP *
Perl_get_re_arg(pTHX_ SV *sv) {