aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2024-10-29 12:52:12 +0000
committerRon Yorston <rmy@pobox.com>2024-10-29 12:52:12 +0000
commitbed407a12630c0886532b4b2d54dbedd74d52d98 (patch)
treee2af7801c6418a4df4d22052c3610a4df585d319
parent1bdb630cc7b17ca69598bf47ef5ae1b6565c7207 (diff)
downloadbusybox-w32-bed407a12630c0886532b4b2d54dbedd74d52d98.tar.gz
busybox-w32-bed407a12630c0886532b4b2d54dbedd74d52d98.tar.bz2
busybox-w32-bed407a12630c0886532b4b2d54dbedd74d52d98.zip
win32: more problems with stream i/o and MSVCRT
These commands: cut --wrong-opt 2>&1 echo $(cut --wrong-opt 2>&1) resulted in different output. In the first case the message about the invalid option appeared before the usage message; in the second after. The command uname --wrong-opt 1>&- 2>&- displayed the error message even though both output streams were closed. These issues appear to be related to those previously fixed by commits 4be93f32f and f192e6539: - They involve the interaction between shell redirection and stream input/output. - UCRT builds aren't affected. Apply two workarounds: - When the file descriptor associated with stderr is redirected remind stderr it should be unbuffered. (32- and 64-bit MSVCRT) - When the file descriptor associated with any of the standard i/o streams is to be closed do it by closing the stream instead. (32-bit MSVCRT) Adds 48-176 bytes. (GitHub commit #472)
-rw-r--r--shell/ash.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/shell/ash.c b/shell/ash.c
index fd2a66270..a5b7dbc74 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -6542,6 +6542,12 @@ save_fd_on_redirect(int fd, int avoid_fd, struct redirtab *sq)
6542 if (fd == preverrout_fd) 6542 if (fd == preverrout_fd)
6543 preverrout_fd = new_fd; 6543 preverrout_fd = new_fd;
6544 6544
6545#if ENABLE_PLATFORM_MINGW32 && !defined(_UCRT)
6546 // Workaround for problems with stderr in MSVCRT
6547 if (fd == fileno(stderr))
6548 setvbuf(stderr, NULL, _IONBF, 0);
6549#endif
6550
6545 return 0; /* "we did not close fd" */ 6551 return 0; /* "we did not close fd" */
6546} 6552}
6547 6553
@@ -6629,6 +6635,16 @@ redirect(union node *redir, int flags)
6629 if (!closed) { 6635 if (!closed) {
6630 /* ^^^ optimization: saving may already 6636 /* ^^^ optimization: saving may already
6631 * have closed it. If not... */ 6637 * have closed it. If not... */
6638#if ENABLE_PLATFORM_MINGW32 && !defined(_UCRT) && !defined(_WIN64)
6639 // Workaround for problems with streams in 32-bit MSVCRT
6640 if (fd == fileno(stdin))
6641 fclose(stdin);
6642 else if (fd == fileno(stdout))
6643 fclose(stdout);
6644 else if (fd == fileno(stderr))
6645 fclose(stderr);
6646 else
6647#endif
6632 close(fd); 6648 close(fd);
6633 } 6649 }
6634 } else { 6650 } else {