about summary refs log tree commit diff
path: root/libio
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2022-12-19 18:56:55 +0100
committerFlorian Weimer <fweimer@redhat.com>2022-12-19 18:56:55 +0100
commit8ece45e4f586abd212d1c02d74d38ef681a45600 (patch)
tree2289618f579787fd06e2235806575705e7601b29 /libio
parentaf7f4165512ea242b5f711ee03a04f6afe22232d (diff)
downloadglibc-8ece45e4f586abd212d1c02d74d38ef681a45600.tar.gz
glibc-8ece45e4f586abd212d1c02d74d38ef681a45600.tar.xz
glibc-8ece45e4f586abd212d1c02d74d38ef681a45600.zip
libio: Convert __vdprintf_internal to buffers
The internal buffer size is set to 2048 bytes.  This is less than
the original BUFSIZ value used by buffered_vfprintf before
the conversion, but it hopefully covers all cases where write
boundaries matter.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
Diffstat (limited to 'libio')
-rw-r--r--libio/iovdprintf.c67
1 files changed, 39 insertions, 28 deletions
diff --git a/libio/iovdprintf.c b/libio/iovdprintf.c
index 1e483868c9..455582e0f3 100644
--- a/libio/iovdprintf.c
+++ b/libio/iovdprintf.c
@@ -24,41 +24,52 @@
    This exception applies to code released by its copyright holders
    in files containing the exception.  */
 
-#include <libioP.h>
+#include <array_length.h>
+#include <math_ldbl_opt.h>
+#include <printf.h>
 #include <stdio_ext.h>
+#include <unistd.h>
+#include <printf_buffer.h>
 
-int
-__vdprintf_internal (int d, const char *format, va_list arg,
-		     unsigned int mode_flags)
+struct __printf_buffer_dprintf
 {
-  struct _IO_FILE_plus tmpfil;
-  struct _IO_wide_data wd;
-  int done;
+  struct __printf_buffer base;
+  int fd;
+
+  char buf[PRINTF_BUFFER_SIZE_DPRINTF];
+};
 
-#ifdef _IO_MTSAFE_IO
-  tmpfil.file._lock = NULL;
-#endif
-  _IO_no_init (&tmpfil.file, _IO_USER_LOCK, 0, &wd, &_IO_wfile_jumps);
-  _IO_JUMPS (&tmpfil) = &_IO_file_jumps;
-  _IO_new_file_init_internal (&tmpfil);
-  if (_IO_file_attach (&tmpfil.file, d) == NULL)
+void
+__printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *buf)
+{
+  char *p = buf->buf;
+  char *end = buf->base.write_ptr;
+  while (p < end)
     {
-      _IO_un_link (&tmpfil);
-      return EOF;
+      ssize_t ret = TEMP_FAILURE_RETRY (write (buf->fd, p, end - p));
+      if (ret < 0)
+	{
+	  __printf_buffer_mark_failed (&buf->base);
+	  return;
+	}
+      p += ret;
     }
-  tmpfil.file._flags |= _IO_DELETE_DONT_CLOSE;
-
-  _IO_mask_flags (&tmpfil.file, _IO_NO_READS,
-		  _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
-
-  done = __vfprintf_internal (&tmpfil.file, format, arg, mode_flags);
-
-  if (done != EOF && _IO_do_flush (&tmpfil.file) == EOF)
-    done = EOF;
-
-  _IO_FINISH (&tmpfil.file);
+  buf->base.write_ptr = buf->buf;
+}
 
-  return done;
+int
+__vdprintf_internal (int d, const char *format, va_list arg,
+		     unsigned int mode_flags)
+{
+  struct __printf_buffer_dprintf buf;
+  __printf_buffer_init (&buf.base, buf.buf, array_length (buf.buf),
+			__printf_buffer_mode_dprintf);
+  buf.fd = d;
+  __printf_buffer (&buf.base, format, arg, mode_flags);
+  if (__printf_buffer_has_failed (&buf.base))
+    return -1;
+  __printf_buffer_flush_dprintf (&buf);
+  return __printf_buffer_done (&buf.base);
 }
 
 int