Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[PATCH] Improve stdio management when redirecting descriptors
- X-seq: zsh-workers 52509
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: [PATCH] Improve stdio management when redirecting descriptors
- Date: Sun, 28 Jan 2024 17:06:24 -0800
- Archived-at: <https://zsh.org/workers/52509>
- List-id: <zsh-workers.zsh.org>
The attached patch assures that we're properly managing the internals
of FILE objects when performing redirections.
diff --git a/Src/utils.c b/Src/utils.c
index 1a4f4c14b..0fda92709 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -2008,6 +2008,28 @@ redup(int x, int y)
{
int ret = y;
+#ifdef HAVE_FPURGE
+ /* Make sure buffers are cleared when changing descriptor for a
+ * FILE object. No fflush() here because the only way anything
+ * can legitimately be left in the buffer is when an error has
+ * occurred, so attempting flush here would at best error again
+ * and at worst squirt out something unexpected.
+ */
+ if (stdout && y == fileno(stdout))
+ fpurge(stdout);
+ if (stderr && y == fileno(stderr))
+ fpurge(stderr);
+ if (shout && y == fileno(shout))
+ fpurge(shout);
+ if (xtrerr && y == fileno(xtrerr))
+ fpurge(xtrerr);
+#ifndef _IONBF
+ /* See init.c setupshin() -- stdin otherwise unbuffered */
+ if (stdin && y == fileno(stdin))
+ fpurge(stdin);
+#endif
+#endif
+
if(x < 0)
zclose(y);
else if (x != y) {
diff --git a/configure.ac b/configure.ac
index 2871dcb7c..175d90433 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1295,7 +1295,7 @@ AC_CHECK_FUNCS(strftime strptime mktime timelocal \
select poll \
readlink faccessx fchdir ftruncate \
fstat lstat lchown fchown fchmod \
- fseeko ftello \
+ fpurge fseeko ftello \
mkfifo _mktemp mkstemp \
waitpid wait3 \
sigaction sigblock sighold sigrelse sigsetmask sigprocmask \
Messages sorted by:
Reverse Date,
Date,
Thread,
Author