Provide mkdtemp() for systems without it.

This commit is contained in:
Todd C. Miller
2010-12-27 13:49:49 -05:00
parent 36d8fbb900
commit 5cf56a77ec
5 changed files with 50 additions and 11 deletions

View File

@@ -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;

View File

@@ -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

14
configure vendored
View File

@@ -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

View File

@@ -2004,9 +2004,9 @@ AC_CHECK_FUNCS(closefrom, [], [AC_LIBOBJ(closefrom)
[ #include <limits.h>
#include <fcntl.h> ])
])
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

View File

@@ -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