From ea49c14c62e66d5cfa8b827c2ccb625b1b142c06 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Tue, 10 Apr 2018 21:45:28 +0200 Subject: [PATCH] Fix read to stdout output appearing first echo banana (read) will output whatever read reads _first_ because it uses a direct write_loop(). This also removes some duplicate code. --- src/builtin_read.cpp | 7 +------ tests/read.in | 4 ++++ tests/read.out | 2 ++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/builtin_read.cpp b/src/builtin_read.cpp index 9b7586f2a..453a9df64 100644 --- a/src/builtin_read.cpp +++ b/src/builtin_read.cpp @@ -394,11 +394,6 @@ static int validate_read_args(const wchar_t *cmd, read_cmd_opts_t &opts, int arg return STATUS_CMD_OK; } -void write_stdout(wcstring val) { - const std::string &out = wcs2string(val); - write_loop(STDOUT_FILENO, out.c_str(), out.size()); -} - /// The read builtin. Reads from stdin and stores the values in environment variables. int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) { wchar_t *cmd = argv[0]; @@ -446,7 +441,7 @@ int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) { } if (opts.to_stdout) { - write_stdout(buff); + streams.out.append(buff); return exit_res; } diff --git a/tests/read.in b/tests/read.in index 7230e4cfc..59eddcdfd 100644 --- a/tests/read.in +++ b/tests/read.in @@ -247,3 +247,7 @@ begin echo a...b...c | read -l a b c echo $a; echo $b; echo $c end +echo + +# At one point, whatever was read was printed _before_ banana +echo banana (echo sausage | read) diff --git a/tests/read.out b/tests/read.out index 5dd8f66a2..d486f4af9 100644 --- a/tests/read.out +++ b/tests/read.out @@ -130,3 +130,5 @@ Multi-char delimiters with IFS a b ..c + +banana sausage