Index: /trunk/Ohana/src/opihi/lib.shell/gprint.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/gprint.c	(revision 16900)
+++ /trunk/Ohana/src/opihi/lib.shell/gprint.c	(revision 16901)
@@ -146,5 +146,9 @@
 }
 
+
 void gprintSetFile (gpStream *stream, gpDest dest, char *filename) {
+
+  int i, Nmatch;
+  FILE *file;
 
   /* if we have an open buffer, free it and null it */
@@ -155,12 +159,5 @@
   }
 
-  /* if we have an open file, flush it and close it */
-  if (stream[0].file != NULL) {
-    fflush (stream[0].file);
-    if (stream[0].file == stdout) goto skip_close;
-    if (stream[0].file == stderr) goto skip_close;
-    fclose (stream[0].file);
-  }
-skip_close:
+  stream[0].mode = GP_FILE;
 
   // if NULL, reuse exising stream name
@@ -173,22 +170,45 @@
   if (!strcmp (stream[0].name, "stdout")) {
     stream[0].file = stdout;
-    goto skip_open;
+    return;
   }
   if (!strcmp (stream[0].name, "stderr")) {
     stream[0].file = stderr;
-    goto skip_open;
-  }
-  
-  /* open the file */
-  stream[0].file = fopen (stream[0].name, "a");
-  if (stream[0].file == NULL) {
+    return;
+  }
+  
+  // open the file : use a local FILE pointer. only close old pointer if no one else is
+  // using it
+  file = fopen (stream[0].name, "a");
+  if (file == NULL) {
     fprintf (stderr, "cannot open file %s\n", stream[0].name);
     free (stream[0].name);
     stream[0].file = (dest == GP_LOG) ? stdout : stderr;
     stream[0].name = (dest == GP_LOG) ? strcreate ("stdout") : strcreate("stderr");
-  }
-
-skip_open:
-  stream[0].mode = GP_FILE;
+    return;
+  }
+
+  /* check the special cases */
+  if (stream[0].file == stdout) {
+    stream[0].file = file;
+    return;
+  }
+  if (stream[0].file == stderr) {
+    stream[0].file = file;
+    return;
+  }
+
+  /* must we close the existing file? */
+  if (stream[0].file != NULL) {
+    Nmatch = 0;
+    for (i = 0; i < Nstreams; i++) {
+      if (streams[i].file == stream[0].file) Nmatch ++;
+    }
+    if (file == stream[0].file) Nmatch ++;
+    if (Nmatch == 1) {
+      fflush (stream[0].file);
+      fclose (stream[0].file);
+    }
+    stream[0].file = file;
+  }
   return;
 }
