zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] Improve stdio management when redirecting descriptors
@ 2024-01-29  1:06 Bart Schaefer
  2024-01-29 10:59 ` Mikael Magnusson
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2024-01-29  1:06 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 116 bytes --]

The attached patch assures that we're properly managing the internals
of FILE objects when performing redirections.

[-- Attachment #2: dup-purge.txt --]
[-- Type: text/plain, Size: 1453 bytes --]

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 \

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Improve stdio management when redirecting descriptors
  2024-01-29  1:06 [PATCH] Improve stdio management when redirecting descriptors Bart Schaefer
@ 2024-01-29 10:59 ` Mikael Magnusson
  2024-01-29 13:03   ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Mikael Magnusson @ 2024-01-29 10:59 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On 1/29/24, Bart Schaefer <schaefer@brasslantern.com> wrote:
> The attached patch assures that we're properly managing the internals
> of FILE objects when performing redirections.

According to my manpages, int fpurge is not available on linux
(glibc), but there is void __fpurge.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Improve stdio management when redirecting descriptors
  2024-01-29 10:59 ` Mikael Magnusson
@ 2024-01-29 13:03   ` Bart Schaefer
  0 siblings, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2024-01-29 13:03 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh hackers list

On Mon, Jan 29, 2024 at 2:59 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> According to my manpages, int fpurge is not available on linux
> (glibc), but there is void __fpurge.

Yes, however, as far as we can tell stdio implementations that don't
expose fpurge don't need to call it.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-29 13:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-29  1:06 [PATCH] Improve stdio management when redirecting descriptors Bart Schaefer
2024-01-29 10:59 ` Mikael Magnusson
2024-01-29 13:03   ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).