]> granicus.if.org Git - sudo/commitdiff
Provide mkdtemp() for systems without it.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Mon, 27 Dec 2010 18:49:49 +0000 (13:49 -0500)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Mon, 27 Dec 2010 18:49:49 +0000 (13:49 -0500)
compat/mktemp.c [moved from compat/mkstemps.c with 81% similarity]
config.h.in
configure
configure.in
include/missing.h

similarity index 81%
rename from compat/mkstemps.c
rename to compat/mktemp.c
index 04898937ea2639b4d5892c821f8e5228633fe8b1..c3a43abf4e24cc4c085571b39fc2712da52891f6 100644 (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;
index 77d7409e68f367afe35c1c1a359bb82370b0a737..b9adebc6244f2f3d7a83fa7ab86da8ba3ab45e01 100644 (file)
 /* 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
 
index ef23b5a2430288bb2e18a07a00aaf8c2a8cdde01..d13ed48e89754928ecebed33af096590e0d6082a 100755 (executable)
--- a/configure
+++ b/configure
 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
 
index 68f160e343bbcf266dacc6823fe7352f6e2e3e03..cb3477d20c1262821019c5838139b3a1561c2ed5 100644 (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
index 56d5973d386b09edca101672d8cb6b0a6e78c0fd..68d5879b877849c30be80455f4ab82925b115373 100644 (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