5780296049babb39b2af001b314676e49e33e459 — Simon Ser 6 months ago d57d7e1
Flush stdout/stderr after executing builtins

When stdout/stderr are set to a pipe, they're not flushed on newlines. This can
result in out-of-order output. For instance:

    command ls
    echo middle
    command ll

Since echo flushes its output before exiting, the output of the first builtin
call will appear after echo's.

The solution is to flush stdout/stderr after builtins have been executed.

dash does the same thing.
1 files changed, 5 insertions(+), 0 deletions(-)

M shell/task/command_builtin.c
M shell/task/command_builtin.c => shell/task/command_builtin.c +5 -0
@@ 71,6 71,11 @@
 	char **argv = (char **)tc->args.data;
 	int ret = mrsh_run_builtin(ctx->state, argc, argv);
 
+	// In case stdout/stderr are pipes, we need to flush to ensure output lines
+	// aren't out-of-order
+	fflush(stdout);
+	fflush(stderr);
+
 	// Restore old FDs
 	for (size_t i = 0; i < sizeof(fds) / sizeof(fds[0]); ++i) {
 		if (fds[i].dup_fd < 0) {