diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index f0e7317b94fffe28fd9145e34dc8a622e3e1c47f..ba010eb95874eb73c730233996de6e0c0e65719d 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2005-01-15  Bud Davis  <bdavis9659@comcast.net>
+
+	PR fortran/18983
+	* gfortran.dg/write_to_null.f90: New test.
+
 2005-01-14  Andrew Pinski  <pinskia@physics.uc.edu>
             John David Anglin  <dave.anglin@nrc-cnrc.gc.ca>
 
diff --git a/gcc/testsuite/gfortran.dg/write_to_null.f90 b/gcc/testsuite/gfortran.dg/write_to_null.f90
new file mode 100644
index 0000000000000000000000000000000000000000..c6f12cb2ec5307ceae583748f3e0eefe00a26b86
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/write_to_null.f90
@@ -0,0 +1,9 @@
+! { dg-do run }
+! pr18983
+! could not write to /dev/null
+       integer i
+       open(10,file="/dev/null")
+       do i = 1,100
+         write(10,*) "Hello, world"
+       end do
+       end
diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 6c2b12eaa6c9179ee8475a26d8df2fba36e7dcd2..c0534b8ef442d78e5d9bdbdc022922bd04f6734b 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,9 @@
+2005-01-15  Bud Davis  <bdavis9659@comcast.net>
+
+	PR fortran/18983
+	* io/transfer.c (st_write_done): only truncate when it
+	is required.
+
 2005-01-12  Toon Moene  <toon@moene.indiv.nluug.nl>
 
 	PR libfortran/19280
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index 73b0b9aba5fb1238297987b412a4a81adcfcb795..54544079073102633d4c0da0f755f6a7d256f538 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -1557,9 +1557,13 @@ st_write_done (void)
 	current_unit->endfile = AT_ENDFILE;	/* Just at it now.  */
 	break;
 
-      case NO_ENDFILE:	/* Get rid of whatever is after this record.  */
-	if (struncate (current_unit->s) == FAILURE)
-	  generate_error (ERROR_OS, NULL);
+      case NO_ENDFILE:
+	if (current_unit->current_record > current_unit->last_record)
+          {
+            /* Get rid of whatever is after this record.  */
+            if (struncate (current_unit->s) == FAILURE)
+              generate_error (ERROR_OS, NULL);
+          }
 
 	current_unit->endfile = AT_ENDFILE;
 	break;