From 2fe6579125fe042f2255afbf00fc8e4b80d6a6be Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Tue, 27 May 2014 00:44:23 -0400 Subject: overhaul tmpfile, tmpnam, and tempnam functions these all now use the shared __randname function internally, rather than duplicating logic for producing a random name. incorrect usage of the access syscall (which works with real uid/gid, not effective) has been removed, along with unnecessary heavy dependencies like snprintf. --- src/stdio/tmpnam.c | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) (limited to 'src/stdio/tmpnam.c') diff --git a/src/stdio/tmpnam.c b/src/stdio/tmpnam.c index 2bd72b3b..92f699c2 100644 --- a/src/stdio/tmpnam.c +++ b/src/stdio/tmpnam.c @@ -1,31 +1,23 @@ #include -#include -#include -#include -#include -#include "libc.h" +#include +#include +#include #include "syscall.h" -#include "atomic.h" #define MAXTRIES 100 -char *tmpnam(char *s) -{ - static int index; - static char s2[L_tmpnam]; - struct timespec ts; - int try = 0; - unsigned n; - - if (!s) s = s2; +char *__randname(char *); - if (__syscall(SYS_access, P_tmpdir, R_OK|W_OK|X_OK) != 0) - return NULL; - - do { - __syscall(SYS_clock_gettime, CLOCK_REALTIME, &ts, 0); - n = ts.tv_nsec ^ (uintptr_t)&s ^ (uintptr_t)s; - snprintf(s, L_tmpnam, "/tmp/t%x-%x", a_fetch_add(&index, 1), n); - } while (!__syscall(SYS_access, s, F_OK) && try++=MAXTRIES ? 0 : s; +char *tmpnam(char *buf) +{ + static char internal[L_tmpnam]; + char s[] = "/tmp/tmpnam_XXXXXX"; + int try; + int r; + for (try=0; try