From 2355bfc0ff8fc1de074493f73cd46f853b5fac03 Mon Sep 17 00:00:00 2001 From: h_vogt Date: Mon, 30 Dec 2013 14:34:07 +0100 Subject: [PATCH] main.c, fix stdout and stderr redirection and buffering with regard to '-o' and '-p' in response to a bug report from Johann Glaser "#259 Pipe mode: buffering" http://sourceforge.net/p/ngspice/bugs/259/ --- src/main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index ff4cd5e43..266b3e00c 100644 --- a/src/main.c +++ b/src/main.c @@ -939,8 +939,8 @@ main(int argc, char **argv) case 'o': /* Output file */ if (optarg) { - /* turn off buffering for stdout */ - setbuf(stdout, NULL); + /* switch to line buffering for stdout */ + setvbuf(stdout, NULL, _IOLBF, BUFSIZ); sprintf (log_file, "%s", optarg); orflag = TRUE; } @@ -950,6 +950,8 @@ main(int argc, char **argv) iflag = TRUE; istty = TRUE; ft_pipemode = TRUE; + /* switch to line buffering for stdout */ + setvbuf(stdout, NULL, _IOLBF, BUFSIZ); break; case 'q': /* Command completion */ @@ -1013,11 +1015,13 @@ main(int argc, char **argv) } // oflag = TRUE; /* All further output to -o log file */ #else - /* Connect stdout to file log_file and log stdout */ + /* Connect stdout and stderr to file log_file and log stdout */ if (!freopen (log_file, "w", stdout)) { perror (log_file); sp_shutdown (EXIT_BAD); } + dup2(fileno(stdout), fileno(stderr)); + setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* enable line buffering */ #endif } /* orflag */