about summary refs log tree commit diff
path: root/Src
diff options
context:
space:
mode:
authorWayne Davison <wayned@users.sourceforge.net>2004-10-21 00:33:42 +0000
committerWayne Davison <wayned@users.sourceforge.net>2004-10-21 00:33:42 +0000
commit65729f55704b22dbe781bd826a852bfe48b3e8e1 (patch)
tree4643af18fd7f5e6934c88db3d51b955ab2335777 /Src
parent9b5ff81b47b5a49cd419d57ff7227c7a78fa7d26 (diff)
downloadzsh-65729f55704b22dbe781bd826a852bfe48b3e8e1.tar.gz
zsh-65729f55704b22dbe781bd826a852bfe48b3e8e1.tar.xz
zsh-65729f55704b22dbe781bd826a852bfe48b3e8e1.zip
Added gettempfile(), which works like a custom mkstemp() (in addition
to the existing gettempname(), which works like a custom mktemp()).
Diffstat (limited to 'Src')
-rw-r--r--Src/utils.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/Src/utils.c b/Src/utils.c
index 31e9c2b26..047aa2644 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -1156,6 +1156,47 @@ gettempname(const char *prefix, int use_heap)
     return ret;
 }
 
+/**/
+mod_export int
+gettempfile(const char *prefix, int use_heap, char **tempname)
+{
+    char *fn;
+    int fd;
+#if HAVE_MKSTEMP
+    char *suffix = prefix ? ".XXXXXX" : "XXXXXX";
+
+    if (!prefix && !(prefix = getsparam("TMPPREFIX")))
+	prefix = DEFAULT_TMPPREFIX;
+    if (use_heap)
+	fn = dyncat(unmeta(prefix), suffix);
+    else
+	fn = bicat(unmeta(prefix), suffix);
+
+    fd = mkstemp(fn);
+    if (fd < 0) {
+	if (!use_heap)
+	    free(fn);
+	fn = NULL;
+    }
+#else
+    int failures = 0;
+
+    do {
+	if (!(fn = gettempname(prefix, use_heap))) {
+	    fd = -1;
+	    break;
+	}
+	if ((fd = open(fn, O_RDWR | O_CREAT | O_EXCL, 0600)) >= 0)
+	    break;
+	if (!use_heap)
+	    free(fn);
+	fn = NULL;
+    } while (errno == EEXIST && ++failures < 16);
+#endif
+    *tempname = fn;
+    return fd;
+}
+ 
 /* Check if a string contains a token */
 
 /**/