From 0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sat, 12 Feb 2011 00:22:29 -0500 Subject: initial check-in, version 0.5.0 --- src/stdio/tempnam.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/stdio/tempnam.c (limited to 'src/stdio/tempnam.c') diff --git a/src/stdio/tempnam.c b/src/stdio/tempnam.c new file mode 100644 index 00000000..2cbcb864 --- /dev/null +++ b/src/stdio/tempnam.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include "libc.h" + +char *tempnam(const char *dir, const char *pfx) +{ + static int lock; + static int index; + char *s; + int pid = getpid(); + int l; + + if (!dir) dir = P_tmpdir; + if (!pfx) pfx = "temp"; + + if (access(dir, R_OK|W_OK|X_OK) != 0) + return NULL; + + l = strlen(dir) + 1 + strlen(pfx) + 2 + sizeof(int)*3*2 + 1; + s = malloc(l); + if (!s) { + errno = ENOMEM; + return NULL; + } + + LOCK(&lock); + for (; index < TMP_MAX; index++) { + snprintf(s, l, "%s/%s-%d-%d", dir, pfx, pid, index); + if (access(s, F_OK) != 0) { + UNLOCK(&lock); + return s; + } + } + UNLOCK(&lock); + free(s); + return NULL; +} -- cgit v1.2.1