From 4be93f32f5bd5ccaac343b4d03fb861a02d6de96 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 30 Sep 2024 15:20:54 +0100 Subject: win32: work around problem with stderr in MSVCRT Try to run a non-existent command with standard error closed: xyz 2>&- In recent versions of busybox-w32 this resulted in problems with moving through history (either using up/down keys or ctrl-r) and tab completion. In all cases the order of the prompt and the command were reversed. Bisection showed the problem was first seen in PRE-5396 which merged some commits from upstream, including fd47f0567 (lineedit: print prompt and editing operations to stderr). This (eventually) called to mind a previous problem with stderr in xargs which was fixed by commit f192e6539 (xargs: fix 'xargs -sNUM' tests). In both cases it seemed that mixing calls to bb_putchar_stderr() and fprintf(stderr, ...) was at fault. The former uses a file descriptor while the latter uses a stream. It was almost as if the stream was buffered. - The problem with xargs affected 32-bit and 64-bit builds with MSVCRT. - The problem with '2>&-' only affected 32-bit builds with MSVCRT. - Neither problem was present with UCRT builds. As a workaround change bb_putchar_stderr() to use the stderr stream in builds for MSVCRT. Saves 16 bytes in the 32-bit build. (GitHub issue #460) --- libbb/xfuncs.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'libbb/xfuncs.c') diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index c365fb8be..7df1a4cd3 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -221,7 +221,12 @@ off_t FAST_FUNC fdlength(int fd) int FAST_FUNC bb_putchar_stderr(char ch) { +#if ENABLE_PLATFORM_MINGW32 && !defined(_UCRT) + // Workaround for problems with stderr in MSVCRT + return fputc(ch, stderr); +#else return write(STDERR_FILENO, &ch, 1); +#endif } ssize_t FAST_FUNC full_write1_str(const char *str) -- cgit v1.2.3-55-g6feb