summaryrefslogtreecommitdiff
path: root/src/stdio/tmpnam.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
committerRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
commit0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 (patch)
tree6eaef0d8a720fa3da580de87b647fff796fe80b3 /src/stdio/tmpnam.c
downloadmusl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.gz
initial check-in, version 0.5.0v0.5.0
Diffstat (limited to 'src/stdio/tmpnam.c')
-rw-r--r--src/stdio/tmpnam.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/stdio/tmpnam.c b/src/stdio/tmpnam.c
new file mode 100644
index 00000000..14d59220
--- /dev/null
+++ b/src/stdio/tmpnam.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <unistd.h>
+#include "libc.h"
+
+char *tmpnam(char *s)
+{
+ static int lock;
+ static int index;
+ static char *s2;
+ int pid = getpid();
+ char *dir = getenv("TMPDIR");
+
+ if (!s) {
+ if (!s2) s2 = malloc(L_tmpnam);
+ s = s2;
+ }
+
+ /* this interface is insecure anyway but at least we can try.. */
+ if (!dir || strlen(dir) > L_tmpnam-32)
+ dir = P_tmpdir;
+
+ if (access(dir, R_OK|W_OK|X_OK) != 0)
+ return NULL;
+
+ LOCK(&lock);
+ for (index++; index < TMP_MAX; index++) {
+ snprintf(s, L_tmpnam, "%s/temp%d-%d", dir, pid, index);
+ if (access(s, F_OK) != 0) {
+ UNLOCK(&lock);
+ return s;
+ }
+ }
+ UNLOCK(&lock);
+ return NULL;
+}