[v3,0/7] Fix some libm static issues
[platform/upstream/glibc.git] / libio / iovdprintf.c
1 /* Copyright (C) 1995-2024 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.
17
18    As a special exception, if you link the code in this file with
19    files compiled with a GNU compiler to produce an executable,
20    that does not cause the resulting executable to be covered by
21    the GNU Lesser General Public License.  This exception does not
22    however invalidate any other reasons why the executable file
23    might be covered by the GNU Lesser General Public License.
24    This exception applies to code released by its copyright holders
25    in files containing the exception.  */
26
27 #include <array_length.h>
28 #include <math_ldbl_opt.h>
29 #include <printf.h>
30 #include <stdio_ext.h>
31 #include <unistd.h>
32 #include <printf_buffer.h>
33
34 struct __printf_buffer_dprintf
35 {
36   struct __printf_buffer base;
37   int fd;
38
39   char buf[PRINTF_BUFFER_SIZE_DPRINTF];
40 };
41
42 void
43 __printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *buf)
44 {
45   char *p = buf->buf;
46   char *end = buf->base.write_ptr;
47   while (p < end)
48     {
49       ssize_t ret = TEMP_FAILURE_RETRY (write (buf->fd, p, end - p));
50       if (ret < 0)
51         {
52           __printf_buffer_mark_failed (&buf->base);
53           return;
54         }
55       p += ret;
56     }
57   buf->base.written += buf->base.write_ptr - buf->base.write_base;
58   buf->base.write_ptr = buf->buf;
59 }
60
61 int
62 __vdprintf_internal (int d, const char *format, va_list arg,
63                      unsigned int mode_flags)
64 {
65   struct __printf_buffer_dprintf buf;
66   __printf_buffer_init (&buf.base, buf.buf, array_length (buf.buf),
67                         __printf_buffer_mode_dprintf);
68   buf.fd = d;
69   __printf_buffer (&buf.base, format, arg, mode_flags);
70   if (__printf_buffer_has_failed (&buf.base))
71     return -1;
72   __printf_buffer_flush_dprintf (&buf);
73   return __printf_buffer_done (&buf.base);
74 }
75
76 int
77 __vdprintf (int d, const char *format, va_list arg)
78 {
79   return __vdprintf_internal (d, format, arg, 0);
80 }
81 ldbl_weak_alias (__vdprintf, vdprintf)