diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 78c6b04e9c9a86c2361a7108d93e58a7385ac2cc..2c38ba39af1a5c8846a417fddd59110e0dc4adec 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,8 @@
+2010-04-24  Kai Tietz  <kai.tietz@onevision.com>
+
+	PR/43844
+	* io/unix.c (tempfile): Correct logic for mktemp case.
+
 2010-04-06  Tobias Burnus  <burnus@net-b.de>
 
 	PR fortran/39997
diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c
index 32f38904f341bd6dc8887e8212f1749abf8a7285..b3bd438c32dd6db7643dec2478d117cd730a4f2a 100644
--- a/libgfortran/io/unix.c
+++ b/libgfortran/io/unix.c
@@ -889,25 +889,26 @@ tempfile (st_parameter_open *opp)
 
   template = get_mem (strlen (tempdir) + 20);
 
-  sprintf (template, "%s/gfortrantmpXXXXXX", tempdir);
-
 #ifdef HAVE_MKSTEMP
+  sprintf (template, "%s/gfortrantmpXXXXXX", tempdir);
 
   fd = mkstemp (template);
 
 #else /* HAVE_MKSTEMP */
-
-  if (mktemp (template))
-    do
+  fd = -1;
+  do
+    {
+      sprintf (template, "%s/gfortrantmpXXXXXX", tempdir);
+      if (!mktemp (template))
+	break;
 #if defined(HAVE_CRLF) && defined(O_BINARY)
       fd = open (template, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
 		 S_IREAD | S_IWRITE);
 #else
       fd = open (template, O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE);
 #endif
-    while (!(fd == -1 && errno == EEXIST) && mktemp (template));
-  else
-    fd = -1;
+    }
+  while (fd == -1 && errno == EEXIST);
 
 #endif /* HAVE_MKSTEMP */