diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 19c2a92a3f6785f183d434a6476fefaa0746e2c8..a9e70825f8021182f30797e026e56d692783276c 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,9 @@
+2006-10-21  Steven G. Kargl  <kargl@gcc.gnu.org>
+
+	* runtime/error.c: Add errno.h
+	(generate_error): Set iostat to errno on OS error.
+	* libgfortran.h: Set ERROR_OS to 5000
+
 2006-10-20  Francois-Xavier Coudert  <coudert@clipper.ens.fr>
 
 	* generated/minval_r8.c: Regenerate.
diff --git a/libgfortran/libgfortran.h b/libgfortran/libgfortran.h
index cdbde51f0af700aa8b5f7821f5d2c307879c850e..e023f0ea26e9136d7c1b4863fbe3e174ba12496e 100644
--- a/libgfortran/libgfortran.h
+++ b/libgfortran/libgfortran.h
@@ -397,7 +397,7 @@ typedef enum
   ERROR_EOR = -2,
   ERROR_END = -1,
   ERROR_OK = 0,			/* Indicates success, must be zero.  */
-  ERROR_OS,			/* Operating system error, more info in errno.  */
+  ERROR_OS = 5000,		/* Operating system error, more info in errno.  */
   ERROR_OPTION_CONFLICT,
   ERROR_BAD_OPTION,
   ERROR_MISSING_OPTION,
diff --git a/libgfortran/runtime/error.c b/libgfortran/runtime/error.c
index 9960733f16a0b0357aaf8bb04c94b33a3f5d2d93..f8f76d3d26fcc5665d85ac39696cfbe9cc02043f 100644
--- a/libgfortran/runtime/error.c
+++ b/libgfortran/runtime/error.c
@@ -34,6 +34,7 @@ Boston, MA 02110-1301, USA.  */
 #include <stdarg.h>
 #include <string.h>
 #include <float.h>
+#include <errno.h>
 
 #include "libgfortran.h"
 #include "../io/io.h"
@@ -457,7 +458,7 @@ generate_error (st_parameter_common *cmp, int family, const char *message)
 {
   /* Set the error status.  */
   if ((cmp->flags & IOPARM_HAS_IOSTAT))
-    *cmp->iostat = family;
+    *cmp->iostat = (family == ERROR_OS) ? errno : family;
 
   if (message == NULL)
     message =