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 */