--- lib/tst_res.c.orig	2008-06-18 10:12:28.000000000 -0700
+++ lib/tst_res.c	2008-06-18 10:11:22.000000000 -0700
@@ -465,6 +465,37 @@
       break;
    }  /* switch ( ttype ) */
 
+   FILE *T_out_old = NULL;
+
+   /* Backup old "T_out" to write data to "File" */
+   if (T_out != NULL && File != NULL) {
+
+      T_out_old = T_out;
+
+      /*
+       * Open file for appending -- backup old File value to avoid recursive
+       * errors with tst_print().
+       */
+      if ( (T_out = fopen(File, "a+")) == NULL ) {
+
+            /* Backup current File var */
+            char *old_File = File;
+
+	    File = NULL;
+
+            /* Print out warning message */
+            sprintf(Warn_mesg,
+               "tst_res(): While trying to open file for writing \"%s\", fopen() failed, errno = %d: %s",
+               File, errno, strerror(errno));
+            tst_print(TCID, 0, 1, TWARN, Warn_mesg);
+
+            /* Restore old File var */
+            File = old_File;
+
+      }
+
+   }
+
    /*
     * Build the result line and print it.
     */
@@ -484,9 +515,15 @@
     * If tst_res() was called with a file, append file contents to the
     * end of last printed result.
     */
-   if ( File != NULL )
+   if ( File != NULL ) {
       cat_file(File);
-   File = NULL;
+      /* Restore previous FH, _after_ the cat'ing has been done. */
+      if (T_out_old != T_out) {
+         T_out = T_out_old;
+      }
+      File = NULL;
+   }
+
 }  /* tst_print() */
 
 
@@ -808,6 +845,9 @@
 
 /*
  * cat_file() - Print the contents of a file to standard out.
+ *
+ * XXX: But what happens if the "filename" points to a device,
+ * e.g. /dev/stdout, /dev/ttys0, that can't be "replayed"?
  */
 static void
 cat_file(char *filename)
