From 5cf56a77ec5e65b939d4c519d0a30dbdcf7badbf Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 27 Dec 2010 13:49:49 -0500 Subject: [PATCH] Provide mkdtemp() for systems without it. --- compat/{mkstemps.c => mktemp.c} | 37 ++++++++++++++++++++++++++++++--- config.h.in | 3 +++ configure | 14 +++++++------ configure.in | 4 ++-- include/missing.h | 3 +++ 5 files changed, 50 insertions(+), 11 deletions(-) rename compat/{mkstemps.c => mktemp.c} (81%) diff --git a/compat/mkstemps.c b/compat/mktemp.c similarity index 81% rename from compat/mkstemps.c rename to compat/mktemp.c index 04898937e..c3a43abf4 100644 --- a/compat/mkstemps.c +++ b/compat/mktemp.c @@ -41,6 +41,9 @@ static unsigned int get_random(void); static void seed_random(void); +#define MKTEMP_FILE 1 +#define MKTEMP_DIR 2 + #define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" #define NUM_CHARS (sizeof(TEMPCHARS) - 1) @@ -48,8 +51,26 @@ static void seed_random(void); #define INT_MAX 0x7fffffff #endif +#ifndef HAVE_MKSTEMPS int mkstemps(char *path, int slen) +{ + return(mktemp_internal(path, slen, MKTEMP_FILE)); +} +#endif /* HAVE_MKSTEMPS */ + +#ifndef HAVE_MKDTEMP +char * +mkdtemp(char *path) +{ + if (mktemp_internal(path, 0, MKTEMP_DIR) == -1) + return(NULL); + return(path); +} +#endif /* HAVE_MKDTEMP */ + +int +mktemp_internal(char *path, int slen, int mode) { char *start, *cp, *ep; const char *tempchars = TEMPCHARS; @@ -77,9 +98,19 @@ mkstemps(char *path, int slen) *cp = tempchars[r]; } - fd = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR); - if (fd != -1 || errno != EEXIST) - return(fd); + switch (mode) { + case MKTEMP_FILE: + fd = open(path, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR); + if (fd != -1 || errno != EEXIST) + return(fd); + break; + case MKTEMP_DIR: + if (mkdir(path, S_IRWXU) == 0) + return(0); + if (errno != EEXIST) + return(-1); + break; + } } while (--tries); errno = EEXIST; diff --git a/config.h.in b/config.h.in index 77d7409e6..b9adebc62 100644 --- a/config.h.in +++ b/config.h.in @@ -331,6 +331,9 @@ /* Define to 1 if you have the `memrchr' function. */ #undef HAVE_MEMRCHR +/* Define to 1 if you have the `mkdtemp' function. */ +#undef HAVE_MKDTEMP + /* Define to 1 if you have the `mkstemps' function. */ #undef HAVE_MKSTEMPS diff --git a/configure b/configure index ef23b5a24..d13ed48e8 100755 --- a/configure +++ b/configure @@ -15684,12 +15684,14 @@ fi fi done -for ac_func in mkstemps +for ac_func in mkstemps mkdtemp do : - ac_fn_c_check_func "$LINENO" "mkstemps" "ac_cv_func_mkstemps" -if test "x$ac_cv_func_mkstemps" = x""yes; then : + as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` +ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" +eval as_val=\$$as_ac_var + if test "x$as_val" = x""yes; then : cat >>confdefs.h <<_ACEOF -#define HAVE_MKSTEMPS 1 +#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1 _ACEOF else @@ -15708,8 +15710,8 @@ fi done case " $LIBOBJS " in - *" mkstemps.$ac_objext "* ) ;; - *) LIBOBJS="$LIBOBJS mkstemps.$ac_objext" + *" mkstemp.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS mkstemp.$ac_objext" ;; esac diff --git a/configure.in b/configure.in index 68f160e34..cb3477d20 100644 --- a/configure.in +++ b/configure.in @@ -2004,9 +2004,9 @@ AC_CHECK_FUNCS(closefrom, [], [AC_LIBOBJ(closefrom) [ #include #include ]) ]) -AC_CHECK_FUNCS(mkstemps, [], [ +AC_CHECK_FUNCS(mkstemps mkdtemp, [], [ AC_CHECK_FUNCS(random lrand48, [break]) - AC_LIBOBJ(mkstemps) + AC_LIBOBJ(mkstemp) ]) AC_CHECK_FUNCS(snprintf vsnprintf asprintf vasprintf, , [NEED_SNPRINTF=1]) if test X"$ac_cv_type_struct_timespec" != X"no"; then diff --git a/include/missing.h b/include/missing.h index 56d5973d3..68d5879b8 100644 --- a/include/missing.h +++ b/include/missing.h @@ -316,6 +316,9 @@ size_t strlcpy(char *, const char *, size_t); #ifndef HAVE_MEMRCHR void *memrchr(const void *, int, size_t); #endif +#ifndef HAVE_MKDTEMP +char *mkdtemp(char *); +#endif #ifndef HAVE_MKSTEMPS int mkstemps(char *, int); #endif -- 2.50.1