Index: /trunk/Ohana/src/opihi/lib.shell/gprint.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/gprint.c	(revision 16901)
+++ /trunk/Ohana/src/opihi/lib.shell/gprint.c	(revision 16902)
@@ -91,4 +91,43 @@
 }
 
+// close if necessary, set to file (may be NULL)
+void gprintCloseFile (gpStream *stream, FILE *file) {
+
+  int i, Nmatch;
+
+  // do not close the file if the new file is the same
+  if (stream[0].file == file) return;
+
+  // do not close the file if the old one is NULL
+  if (stream[0].file == NULL) {
+    stream[0].file = 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 still being used, then no
+  Nmatch = 0;
+  for (i = 0; i < Nstreams; i++) {
+    if (stream == &streams[i]) continue;
+    if (streams[i].file == stream[0].file) Nmatch ++;
+  }
+  if (Nmatch == 0) {
+    fflush (stream[0].file);
+    fclose (stream[0].file);
+  }
+
+  stream[0].file = file;
+  return;
+}
+
 void gprintSetBuffer (gpDest dest) {
 
@@ -97,23 +136,10 @@
   stream = gprintGetStream (dest);
 
-  /* if we have an open file: flush it, close it, null 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].file = NULL;
-  }
+  // close the existing file (if needed), set to NULL
+  gprintCloseFile (stream, NULL);
+
+  assert (stream[0].buffer);
+  FlushIOBuffer (stream[0].buffer);
   
-  if (stream[0].buffer == NULL) {
-    ALLOCATE (stream[0].buffer, IOBuffer, 1);
-    InitIOBuffer (stream[0].buffer, 64);
-  } else {
-    FlushIOBuffer (stream[0].buffer);
-  }
-  
-  /* this element may be redundant with the NULL state of file and buffer */
   stream[0].mode = GP_BUFF;
 }
@@ -152,9 +178,10 @@
   FILE *file;
 
+  assert (stream[0].buffer);
+
   /* if we have an open buffer, free it and null it */
-  if (stream[0].buffer != NULL) {
-    FreeIOBuffer (stream[0].buffer);
-    free (stream[0].buffer);
-    stream[0].buffer = NULL;
+  if (stream[0].buffer[0].Nbuffer) {
+    // XXX we drop what was on the buffer, send it to the old or the new file?
+    FlushIOBuffer (stream[0].buffer);
   }
 
@@ -167,5 +194,5 @@
   }
 
-  /* we allow the user to set stdout to ERR and stderr to LOG if they want */
+  // we allow the user to set stdout to ERR and stderr to LOG if they want
   if (!strcmp (stream[0].name, "stdout")) {
     stream[0].file = stdout;
@@ -177,6 +204,5 @@
   }
   
-  // open the file : use a local FILE pointer. only close old pointer if no one else is
-  // using it
+  // open the file. only close old pointer if no one else is using it
   file = fopen (stream[0].name, "a");
   if (file == NULL) {
@@ -188,27 +214,6 @@
   }
 
-  /* 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;
-  }
+  // close the existing file (if needed), set to new file
+  gprintCloseFile (stream, file);
   return;
 }
@@ -237,11 +242,6 @@
 
   va_start (argp, format);
-  
-  // gprint (GP_ERR, "GP_FILE: %d\n", GP_FILE);
-  // gprint (GP_ERR, "GP_BUFF: %d\n", GP_BUFF);
-  // gprint (GP_ERR, "mode: %d\n", stream[0].mode);
 
   if (stream[0].mode == GP_FILE) {
-    // gprint (GP_ERR, "printing to FILE\n");
     status = vfprintf (stream[0].file, format, argp);
     if (status < 0) {
@@ -249,5 +249,4 @@
     }
   } else {
-    // gprint (GP_ERR, "printing to BUFFER\n");
     vPrintIOBuffer (stream[0].buffer, format, argp);
   }
@@ -267,4 +266,5 @@
     fwrite (buffer, size, N, stream[0].file);
   } else {
+    // XXX can we not use exising IOBuffer APIs here?
     outbuff = stream[0].buffer;
     Nbyte = size * N;
