diff --git a/glib/gfileutils.c b/glib/gfileutils.c
index 76335fa..f6c5863 100644
--- a/glib/gfileutils.c
+++ b/glib/gfileutils.c
@@ -1148,6 +1148,27 @@ g_file_set_contents (const gchar  *filename,
   return retval;
 }
 
+static void
+gen_tempname (gchar *XXXXXX, int value)
+{
+  static const char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+  static const int NLETTERS = sizeof (letters) - 1;
+  int v = value;
+  
+  /* Fill in the random bits.  */
+  XXXXXX[0] = letters[v % NLETTERS];
+  v /= NLETTERS;
+  XXXXXX[1] = letters[v % NLETTERS];
+  v /= NLETTERS;
+  XXXXXX[2] = letters[v % NLETTERS];
+  v /= NLETTERS;
+  XXXXXX[3] = letters[v % NLETTERS];
+  v /= NLETTERS;
+  XXXXXX[4] = letters[v % NLETTERS];
+  v /= NLETTERS;
+  XXXXXX[5] = letters[v % NLETTERS];
+}
+
 /**
  * g_mkstemp_full:
  * @tmpl: template filename
@@ -1184,20 +1205,16 @@ g_mkstemp_full (gchar *tmpl,
 {
   char *XXXXXX;
   int count, fd;
-  static const char letters[] =
-    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-  static const int NLETTERS = sizeof (letters) - 1;
   glong value;
   GTimeVal tv;
   static int counter = 0;
 
   g_return_val_if_fail (tmpl != NULL, -1);
 
-
   /* find the last occurrence of "XXXXXX" */
   XXXXXX = g_strrstr (tmpl, "XXXXXX");
 
-  if (!XXXXXX || strncmp (XXXXXX, "XXXXXX", 6))
+  if (!XXXXXX)
     {
       errno = EINVAL;
       return -1;
@@ -1209,20 +1226,7 @@ g_mkstemp_full (gchar *tmpl,
 
   for (count = 0; count < 100; value += 7777, ++count)
     {
-      glong v = value;
-
-      /* Fill in the random bits.  */
-      XXXXXX[0] = letters[v % NLETTERS];
-      v /= NLETTERS;
-      XXXXXX[1] = letters[v % NLETTERS];
-      v /= NLETTERS;
-      XXXXXX[2] = letters[v % NLETTERS];
-      v /= NLETTERS;
-      XXXXXX[3] = letters[v % NLETTERS];
-      v /= NLETTERS;
-      XXXXXX[4] = letters[v % NLETTERS];
-      v /= NLETTERS;
-      XXXXXX[5] = letters[v % NLETTERS];
+      gen_tempname (XXXXXX, value);
 
       /* tmpl is in UTF-8 on Windows, thus use g_open() */
       fd = g_open (tmpl, flags | O_CREAT | O_EXCL, mode);
@@ -1558,6 +1562,69 @@ g_build_path (const gchar *separator,
   return str;
 }
 
+/**
+ * g_mkdtemp:
+ * @tmpl: template directory name
+ *
+ * Creates a temporary directory. See the mkdtemp() documentation
+ * on most UNIX-like systems. 
+ *
+ * The parameter is a string that should follow the rules for
+ * mkdtemp() templates, i.e. contain the string "XXXXXX".  g_mkdtemp()
+ * is slightly more flexible than mkdtemp() in that the sequence does
+ * not have to occur at the very end of the template. The X string
+ * will be modified to form the name of a directory that didn't
+ * already exist.  The string should be in the GLib file name
+ * encoding. Most importantly, on Windows it should be in UTF-8.
+ *
+ * Return value: If a temporary directory was successfully created,
+ * @tmpl will be returned with the XXXXXX string modified in such a
+ * way as to make the path unique.  In case of errors, %NULL is
+ * returned.
+ */ 
+gchar *
+g_mkdtemp (gchar *tmpl)
+{
+  static int counter = 0;
+  char *XXXXXX, *p;
+  GTimeVal tv;
+  glong value;
+  int count;
+  int rv;
+  
+  /* find the last occurrence of "XXXXXX" */
+  XXXXXX = g_strrstr (tmpl, "XXXXXX");
+  
+  if (!XXXXXX)
+    {
+      errno = EINVAL;
+      return NULL;
+    }
+  
+  /* Get some more or less random data.  */
+  g_get_current_time (&tv);
+  value = (tv.tv_usec ^ tv.tv_sec) + counter++;
+  
+  for (count = 0; count < 100; value += 7777, ++count)
+    {
+      gen_tempname (XXXXXX, value);
+      
+      /* tmpl is in UTF-8 on Windows, thus use g_mkdir() */
+      if (g_mkdir (tmpl, 0700) == 0)
+        return tmpl;
+      
+      if (errno != EEXIST)
+	/* Any other error will apply also to other names we might
+	 *  try, and there are 2^32 or so of them, so give up now.
+	 */
+	return NULL;
+    }
+  
+  /* We got out of the loop because we ran out of combinations to try.  */
+  errno = EEXIST;
+  return NULL;
+}
+
 #ifdef G_OS_WIN32
 
 static gchar *
@@ -1931,9 +1998,6 @@ g_mkstemp (gchar *tmpl)
 {
   char *XXXXXX;
   int count, fd;
-  static const char letters[] =
-    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-  static const int NLETTERS = sizeof (letters) - 1;
   glong value;
   GTimeVal tv;
   static int counter = 0;
@@ -1953,20 +2017,7 @@ g_mkstemp (gchar *tmpl)
 
   for (count = 0; count < 100; value += 7777, ++count)
     {
-      glong v = value;
-
-      /* Fill in the random bits.  */
-      XXXXXX[0] = letters[v % NLETTERS];
-      v /= NLETTERS;
-      XXXXXX[1] = letters[v % NLETTERS];
-      v /= NLETTERS;
-      XXXXXX[2] = letters[v % NLETTERS];
-      v /= NLETTERS;
-      XXXXXX[3] = letters[v % NLETTERS];
-      v /= NLETTERS;
-      XXXXXX[4] = letters[v % NLETTERS];
-      v /= NLETTERS;
-      XXXXXX[5] = letters[v % NLETTERS];
+      gen_tempname (XXXXXX, value);
 
       /* This is the backward compatibility system codepage version,
        * thus use normal open().
diff --git a/glib/gfileutils.h b/glib/gfileutils.h
index d8f9d3b..c6c04b7 100644
--- a/glib/gfileutils.h
+++ b/glib/gfileutils.h
@@ -103,6 +103,9 @@ gint    g_mkstemp_full       (gchar        *tmpl,
                               int           flags,
                               int           mode);
 
+/* Wrapper / workalike for mkdtemp() */
+gchar  *g_mkdtemp            (gchar        *tmpl);
+
 /* Wrapper for g_mkstemp */
 gint    g_file_open_tmp      (const gchar  *tmpl,
 			      gchar       **name_used,
