summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embed.fnc4
-rw-r--r--proto.h5
-rw-r--r--util.c34
-rw-r--r--util.h4
4 files changed, 47 insertions, 0 deletions
diff --git a/embed.fnc b/embed.fnc
index 44d8d40adf..46fdf46764 100644
--- a/embed.fnc
+++ b/embed.fnc
@@ -3053,6 +3053,10 @@ Apnod |Size_t |my_strlcat |NULLOK char *dst|NULLOK const char *src|Size_t size
Apnod |Size_t |my_strlcpy |NULLOK char *dst|NULLOK const char *src|Size_t size
#endif
+#ifndef HAS_MKSTEMP
+pno |int |my_mkstemp |NN char *templte
+#endif
+
APpdn |bool |isinfnan |NV nv
p |bool |isinfnansv |NN SV *sv
diff --git a/proto.h b/proto.h
index 637b3c913b..6bb89e54ee 100644
--- a/proto.h
+++ b/proto.h
@@ -3856,6 +3856,11 @@ STATIC int S_dooneliner(pTHX_ const char *cmd, const char *filename)
# endif
#endif
+#if !defined(HAS_MKSTEMP)
+PERL_CALLCONV int Perl_my_mkstemp(char *templte);
+#define PERL_ARGS_ASSERT_MY_MKSTEMP \
+ assert(templte)
+#endif
#if !defined(HAS_RENAME)
PERL_CALLCONV I32 Perl_same_dirent(pTHX_ const char* a, const char* b);
#define PERL_ARGS_ASSERT_SAME_DIRENT \
diff --git a/util.c b/util.c
index 136e4ca1ec..e2feb7f473 100644
--- a/util.c
+++ b/util.c
@@ -5789,6 +5789,40 @@ Perl_my_dirfd(DIR * dir) {
#endif
}
+#ifndef HAS_MKSTEMP
+
+#define TEMP_FILE_CH "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789"
+#define TEMP_FILE_CH_COUNT (sizeof(TEMP_FILE_CH)-1)
+
+int
+Perl_my_mkstemp(char *templte) {
+ 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;
+ return -1;
+ }
+
+ do {
+ int i;
+ 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);
+ } while (fd == -1 && errno == EEXIST && ++attempts <= 100);
+
+ return fd;
+}
+
+#endif
+
REGEXP *
Perl_get_re_arg(pTHX_ SV *sv) {
diff --git a/util.h b/util.h
index 4589808ee1..6b63d90e4f 100644
--- a/util.h
+++ b/util.h
@@ -246,6 +246,10 @@ means arg not present, 1 is empty string/null byte */
((char *) memmem(big, bigend - big, little, lend - little))
#endif
+#if defined(HAS_MKSTEMP) && defined(PERL_CORE)
+# define Perl_my_mkstemp(templte) mkstemp(templte)
+#endif
+
#endif /* PERL_UTIL_H_ */
/*