summaryrefslogtreecommitdiffstats
path: root/tcl8.6/compat/mkstemp.c
diff options
context:
space:
mode:
authorWilliam Joye <wjoye@cfa.harvard.edu>2017-10-17 19:52:11 (GMT)
committerWilliam Joye <wjoye@cfa.harvard.edu>2017-10-17 19:52:11 (GMT)
commitda0aaf36f00f649cd910b1bcb972b4ad476cd5fd (patch)
tree3d5215355c39587ed375451b86b7f9962f739a5f /tcl8.6/compat/mkstemp.c
parent9bfb1e415c87790341c6a3520b081292fcdb058b (diff)
parentc8bc058c566bbcbcd23e732197d64fecc739008f (diff)
downloadblt-da0aaf36f00f649cd910b1bcb972b4ad476cd5fd.zip
blt-da0aaf36f00f649cd910b1bcb972b4ad476cd5fd.tar.gz
blt-da0aaf36f00f649cd910b1bcb972b4ad476cd5fd.tar.bz2
Merge commit 'c8bc058c566bbcbcd23e732197d64fecc739008f' as 'tcl8.6'
Diffstat (limited to 'tcl8.6/compat/mkstemp.c')
-rw-r--r--tcl8.6/compat/mkstemp.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/tcl8.6/compat/mkstemp.c b/tcl8.6/compat/mkstemp.c
new file mode 100644
index 0000000..1a44dfa
--- /dev/null
+++ b/tcl8.6/compat/mkstemp.c
@@ -0,0 +1,78 @@
+/*
+ * mkstemp.c --
+ *
+ * Source code for the "mkstemp" library routine.
+ *
+ * Copyright (c) 2009 Donal K. Fellows
+ *
+ * See the file "license.terms" for information on usage and redistribution of
+ * this file, and for a DISCLAIMER OF ALL WARRANTIES.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * mkstemp --
+ *
+ * Create an open temporary file from a template.
+ *
+ * Results:
+ * A file descriptor, or -1 (with errno set) in the case of an error.
+ *
+ * Side effects:
+ * The template is updated to contain the real filename.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+mkstemp(
+ char *template) /* Template for filename. */
+{
+ static const char alphanumerics[] =
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ register char *a, *b;
+ int fd, count, alphanumericsLen = strlen(alphanumerics); /* == 62 */
+
+ a = template + strlen(template);
+ while (a > template && *(a-1) == 'X') {
+ a--;
+ }
+
+ if (a == template) {
+ errno = ENOENT;
+ return -1;
+ }
+
+ /*
+ * We'll only try up to 10 times; after that, we're suffering from enemy
+ * action and should let the caller know.
+ */
+
+ count = 10;
+ do {
+ /*
+ * Replace the X's in the original template with random alphanumeric
+ * digits.
+ */
+
+ for (b=a ; *b ; b++) {
+ float r = rand() / ((float) RAND_MAX);
+
+ *b = alphanumerics[(int)(r * alphanumericsLen)];
+ }
+
+ /*
+ * Template is now realized; try to open (with correct options).
+ */
+
+ fd = open(template, O_RDWR|O_CREAT|O_EXCL, 0600);
+ } while (fd == -1 && errno == EEXIST && --count > 0);
+
+ return fd;
+}