about summary refs log tree commit diff
path: root/libio/iovdprintf.c
blob: d9fa886fdf3e2f53211f0236f74d4084e880716b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Copyright (C) 1995-2023 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.

   As a special exception, if you link the code in this file with
   files compiled with a GNU compiler to produce an executable,
   that does not cause the resulting executable to be covered by
   the GNU Lesser General Public License.  This exception does not
   however invalidate any other reasons why the executable file
   might be covered by the GNU Lesser General Public License.
   This exception applies to code released by its copyright holders
   in files containing the exception.  */

#include <array_length.h>
#include <math_ldbl_opt.h>
#include <printf.h>
#include <stdio_ext.h>
#include <unistd.h>
#include <printf_buffer.h>

struct __printf_buffer_dprintf
{
  struct __printf_buffer base;
  int fd;

  char buf[PRINTF_BUFFER_SIZE_DPRINTF];
};

void
__printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *buf)
{
  char *p = buf->buf;
  char *end = buf->base.write_ptr;
  while (p < end)
    {
      ssize_t ret = TEMP_FAILURE_RETRY (write (buf->fd, p, end - p));
      if (ret < 0)
	{
	  __printf_buffer_mark_failed (&buf->base);
	  return;
	}
      p += ret;
    }
  buf->base.written += buf->base.write_ptr - buf->base.write_base;
  buf->base.write_ptr = buf->buf;
}

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
__vdprintf (int d, const char *format, va_list arg)
{
  return __vdprintf_internal (d, format, arg, 0);
}
ldbl_weak_alias (__vdprintf, vdprintf)