select: fix 64-bit timeout truncation on pre-time64 kernels
[musl] / src / stdio / tmpnam.c
index 14d5922..71dc8bb 100644 (file)
@@ -1,38 +1,27 @@
 #include <stdio.h>
-#include <stdlib.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/stat.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;
+#include <stdlib.h>
+#include "syscall.h"
 
-       if (access(dir, R_OK|W_OK|X_OK) != 0)
-               return NULL;
+#define MAXTRIES 100
 
-       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;
-               }
+char *tmpnam(char *buf)
+{
+       static char internal[L_tmpnam];
+       char s[] = "/tmp/tmpnam_XXXXXX";
+       int try;
+       int r;
+       for (try=0; try<MAXTRIES; try++) {
+               __randname(s+12);
+#ifdef SYS_readlink
+               r = __syscall(SYS_readlink, s, (char[1]){0}, 1);
+#else
+               r = __syscall(SYS_readlinkat, AT_FDCWD, s, (char[1]){0}, 1);
+#endif
+               if (r == -ENOENT) return strcpy(buf ? buf : internal, s);
        }
-       UNLOCK(&lock);
-       return NULL;
+       return 0;
 }