src/hg/hgTables/great.c 1.3

1.3 2010/06/01 20:09:36 galt
stdout stream is not assignable on some operating systems
Index: src/hg/hgTables/great.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/hgTables/great.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -b -B -U 4 -r1.2 -r1.3
--- src/hg/hgTables/great.c	28 Apr 2010 22:07:49 -0000	1.2
+++ src/hg/hgTables/great.c	1 Jun 2010 20:09:36 -0000	1.3
@@ -122,20 +122,31 @@
 void doGetGreatOutput(void (*dispatch)())
 {
 char hgsid[64];
 struct tempName tn;
-FILE *saveStdout = stdout;
+int saveStdout;
+FILE *f;
 
 safef(hgsid, sizeof(hgsid), "%u", cartSessionId(cart));
 trashDirFile(&tn, "great", hgsid, ".bed");
-stdout = fopen(tn.forCgi, "w");
+f = fopen(tn.forCgi, "w");
 
-dispatch();
+/* We want to capture hgTables stdout output to a trash file
+ * which will later be fetched by Great via URL. */
+/* Workaround because stdout stream is not assignable on some operating systems */
+fflush(stdout);
+saveStdout = dup(STDOUT_FILENO);
+dup2(fileno(f),STDOUT_FILENO);   /* closes STDOUT before setting it again */
+fclose(f);
+
+dispatch();  /* this writes to stdout */
 
+/* restore stdout */
 fflush(stdout);
-fclose(stdout);
+dup2(saveStdout ,STDOUT_FILENO);  /* closes STDOUT before setting it back to saved descriptor */
+close(saveStdout);
+
 cartRemove(cart, hgtaDoGreatOutput);
-stdout = saveStdout;
 htmlOpen("Table Browser integration with GREAT");
 doSubmitToGreat(tn.forCgi);
 htmlClose();
 }