summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2015-01-01 03:14:55 -0600
committerNicolas Williams <nico@cryptonector.com>2015-01-01 03:14:55 -0600
commit157c95b9889b523a3de7772e85ef9f3f69182c88 (patch)
treedb80d81487c043910050c7f690587c890f494559
parent426913f127de00837d53b1df108ce13c07f21131 (diff)
Add mkstemp() for mingw build
-rw-r--r--configure.ac1
-rw-r--r--util.c27
-rw-r--r--util.h4
3 files changed, 31 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac
index 885482c5..93d40748 100644
--- a/configure.ac
+++ b/configure.ac
@@ -86,6 +86,7 @@ if test "x$valgrind_cmd" = "x" ; then
AC_MSG_WARN([valgrind is required to test jq.])
fi
AC_CHECK_FUNCS(memmem)
+AC_CHECK_FUNCS(mkstemp)
dnl Don't attempt to build docs if there's no Ruby lying around
diff --git a/util.c b/util.c
index 08ec715c..9dec89b4 100644
--- a/util.c
+++ b/util.c
@@ -1,9 +1,13 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <assert.h>
+#include <fcntl.h>
+
#ifdef HAVE_MEMMEM
#define _GNU_SOURCE
#include <string.h>
#endif
-#include <assert.h>
#ifndef WIN32
#include <pwd.h>
#endif
@@ -15,6 +19,27 @@
#include "util.h"
#include "jv.h"
+#ifndef HAVE_MKSTEMP
+int mkstemp(char *template) {
+ size_t len = strlen(template);
+ int tries=5;
+ int fd;
+
+ // mktemp() truncates template when it fails
+ char *s = alloca(len + 1);
+ assert(s != NULL);
+ strcpy(s, template);
+
+ do {
+ // Restore template
+ strcpy(template, s);
+ (void) mktemp(template);
+ fd = open(template, O_CREAT | O_EXCL | O_RDWR, 0600);
+ } while (fd == -1 && tries-- > 0);
+ return fd;
+}
+#endif
+
jv expand_path(jv path) {
assert(jv_get_kind(path) == JV_KIND_STRING);
const char *pstr = jv_string_value(path);
diff --git a/util.h b/util.h
index 6e3cb9f0..101a8025 100644
--- a/util.h
+++ b/util.h
@@ -3,6 +3,10 @@
#include "jv.h"
+#ifndef HAVE_MKSTEMP
+int mkstemp(char *template);
+#endif
+
jv expand_path(jv);
jv get_home(void);
jv jq_realpath(jv);