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/
This commit is contained in:
h_vogt 2013-12-30 14:34:07 +01:00 committed by rlar
parent a717fd01ab
commit 2355bfc0ff
1 changed files with 7 additions and 3 deletions

View File

@ -939,8 +939,8 @@ main(int argc, char **argv)
case 'o': /* Output file */ case 'o': /* Output file */
if (optarg) { if (optarg) {
/* turn off buffering for stdout */ /* switch to line buffering for stdout */
setbuf(stdout, NULL); setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
sprintf (log_file, "%s", optarg); sprintf (log_file, "%s", optarg);
orflag = TRUE; orflag = TRUE;
} }
@ -950,6 +950,8 @@ main(int argc, char **argv)
iflag = TRUE; iflag = TRUE;
istty = TRUE; istty = TRUE;
ft_pipemode = TRUE; ft_pipemode = TRUE;
/* switch to line buffering for stdout */
setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
break; break;
case 'q': /* Command completion */ case 'q': /* Command completion */
@ -1013,11 +1015,13 @@ main(int argc, char **argv)
} }
// oflag = TRUE; /* All further output to -o log file */ // oflag = TRUE; /* All further output to -o log file */
#else #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)) { if (!freopen (log_file, "w", stdout)) {
perror (log_file); perror (log_file);
sp_shutdown (EXIT_BAD); sp_shutdown (EXIT_BAD);
} }
dup2(fileno(stdout), fileno(stderr));
setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* enable line buffering */
#endif #endif
} /* orflag */ } /* orflag */