>From 59fa03a4a72bac8119fa77f6450cd4fae4446911 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Sat, 2 Feb 2013 20:48:26 +0100 Subject: [PATCH] refactor mktemp code and add mkstemps, mkostemp(s) loosely based on patch by Anthony G. Basile --- include/stdlib.h | 7 +++++++ src/temp/mkostemp.c | 10 ++++++++++ src/temp/mkostemps.c | 29 +++++++++++++++++++++++++++++ src/temp/mkstemp.c | 22 ++-------------------- src/temp/mkstemps.c | 10 ++++++++++ src/temp/mktemp.c | 21 +++++++++++++-------- 6 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 src/temp/mkostemp.c create mode 100644 src/temp/mkostemps.c create mode 100644 src/temp/mkstemps.c diff --git a/include/stdlib.h b/include/stdlib.h index 671d188..a2aafde 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -134,6 +134,10 @@ void lcong48 (unsigned short [7]); #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) #include char *mktemp (char *); +int mkostemp (char *, int); +int mkstemps (char *, int); +int mkostemps (char *, int, int); + void *valloc (size_t); void *memalign(size_t, size_t); #define WCOREDUMP(s) ((s) & 0x80) @@ -150,6 +154,9 @@ char *gcvt(double, int, char *); #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) #define mkstemp64 mkstemp +#define mkostemp64 mkostemp +#define mkstemps64 mkstemps +#define mkostemps64 mkostemps #endif #ifdef __cplusplus diff --git a/src/temp/mkostemp.c b/src/temp/mkostemp.c new file mode 100644 index 0000000..f3e45cb --- /dev/null +++ b/src/temp/mkostemp.c @@ -0,0 +1,10 @@ +#include "libc.h" + +int __mkostemps (char *template, int suffixlen, int flags); + +int mkostemp(char *template, int flags) +{ + return __mkostemps(template, 0, flags); +} + +LFS64(mkostemp); diff --git a/src/temp/mkostemps.c b/src/temp/mkostemps.c new file mode 100644 index 0000000..d389ec8 --- /dev/null +++ b/src/temp/mkostemps.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include "libc.h" + +void __randname(char *); + +int __mkostemps (char *template, int suffixlen, int flags) +{ + size_t l = strlen(template); + char *x6 = template + l - suffixlen; + if(l - suffixlen < 6 || memcmp(x6, "XXXXXX", 6)) { + errno = EINVAL; + *template = 0; + return -1; + } + + int fd, retries = 100; + while (retries--) { + __randname(x6); + if ((fd = open(template, flags | O_CREAT | O_EXCL, 0600))>=0) + return fd; + if (errno != EEXIST) return -1; + } + return -1; +} + +weak_alias(__mkostemps, mkostemps); +LFS64(mkostemps); diff --git a/src/temp/mkstemp.c b/src/temp/mkstemp.c index a390d42..07ec4d7 100644 --- a/src/temp/mkstemp.c +++ b/src/temp/mkstemp.c @@ -1,28 +1,10 @@ -#include -#include -#include -#include -#include -#include -#include #include "libc.h" -char *__mktemp(char *); +int __mkostemps (char *template, int suffixlen, int flags); int mkstemp(char *template) { - int fd, retries = 100, t0 = *template; - while (retries--) { - if (!*__mktemp(template)) return -1; - if ((fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600))>=0) - return fd; - if (errno != EEXIST) return -1; - /* this is safe because mktemp verified - * that we have a valid template string */ - template[0] = t0; - strcpy(template+strlen(template)-6, "XXXXXX"); - } - return -1; + return __mkostemps(template, 0, 0); } LFS64(mkstemp); diff --git a/src/temp/mkstemps.c b/src/temp/mkstemps.c new file mode 100644 index 0000000..6d16d9c --- /dev/null +++ b/src/temp/mkstemps.c @@ -0,0 +1,10 @@ +#include "libc.h" + +int __mkostemps (char *template, int suffixlen, int flags); + +int mkstemps (char *template, int suffixlen) +{ + return __mkostemps(template, suffixlen, 0); +} + +LFS64(mkstemps); diff --git a/src/temp/mktemp.c b/src/temp/mktemp.c index c0e06f5..a22f69c 100644 --- a/src/temp/mktemp.c +++ b/src/temp/mktemp.c @@ -8,23 +8,28 @@ #include #include "libc.h" -char *__mktemp(char *template) +void __randname(char *x6) { + size_t i = 0; struct timespec ts; - size_t i, l = strlen(template); + clock_gettime(CLOCK_REALTIME, &ts); + unsigned long r = ts.tv_nsec + (uintptr_t)&ts / 16 + (uintptr_t)x6; + for (; i<6; i++, r>>=4) x6[i] = 'A'+(r&15); +} + +char *__mktemp(char *template) +{ int retries = 10000; - unsigned long r; + size_t l = strlen(template); + char *x6 = template +l -6; - if (l < 6 || strcmp(template+l-6, "XXXXXX")) { + if(l < 6 || memcmp(x6, "XXXXXX", 6)) { errno = EINVAL; *template = 0; return template; } while (retries--) { - clock_gettime(CLOCK_REALTIME, &ts); - r = ts.tv_nsec + (uintptr_t)&ts / 16 + (uintptr_t)template; - for (i=1; i<=6; i++, r>>=4) - template[l-i] = 'A'+(r&15); + __randname(x6); if (access(template, F_OK) < 0) return template; } *template = 0; -- 1.7.3.4