linux: Return unsupported if procfs can not be mount on tst-ttyname-namespace
[glibc.git] / libio / vsnprintf.c
blobaab32aa9e7a35745713875b7fdfafbefcb31632c
1 /* Copyright (C) 1994-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>.
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. */
27 #include "libioP.h"
29 #include <array_length.h>
30 #include <printf.h>
31 #include <printf_buffer.h>
33 void
34 __printf_buffer_flush_snprintf (struct __printf_buffer_snprintf *buf)
36 /* Record the bytes written so far, before switching buffers. */
37 buf->base.written += buf->base.write_ptr - buf->base.write_base;
39 if (buf->base.write_base != buf->discard)
41 /* We just finished writing the caller-supplied buffer. Force
42 NUL termination if the string length is not zero. */
43 if (buf->base.write_base != buf->base.write_end)
44 buf->base.write_end[-1] = '\0';
47 /* Switch to the discard buffer. */
48 buf->base.write_base = buf->discard;
49 buf->base.write_ptr = buf->discard;
50 buf->base.write_end = array_end (buf->discard);
53 buf->base.write_base = buf->discard;
54 buf->base.write_ptr = buf->discard;
57 void
58 __printf_buffer_snprintf_init (struct __printf_buffer_snprintf *buf,
59 char *buffer, size_t length)
61 __printf_buffer_init (&buf->base, buffer, length,
62 __printf_buffer_mode_snprintf);
63 if (length > 0)
64 /* Historic behavior for trivially overlapping buffers (checked by
65 the test suite). */
66 *buffer = '\0';
69 int
70 __printf_buffer_snprintf_done (struct __printf_buffer_snprintf *buf)
72 /* NB: Do not check for buf->base.fail here. Write the null
73 terminator even in case of errors. */
75 if (buf->base.write_ptr < buf->base.write_end)
76 *buf->base.write_ptr = '\0';
77 else if (buf->base.write_ptr > buf->base.write_base)
78 /* If write_ptr == write_base, nothing has been written. No null
79 termination is needed because of the early truncation in
80 __printf_buffer_snprintf_init (the historic behavior).
82 We might also be at the start of the discard buffer, but in
83 this case __printf_buffer_flush_snprintf has already written
84 the NUL terminator. */
85 buf->base.write_ptr[-1] = '\0';
87 return __printf_buffer_done (&buf->base);
90 int
91 __vsnprintf_internal (char *string, size_t maxlen, const char *format,
92 va_list args, unsigned int mode_flags)
94 struct __printf_buffer_snprintf buf;
95 __printf_buffer_snprintf_init (&buf, string, maxlen);
96 __printf_buffer (&buf.base, format, args, mode_flags);
97 return __printf_buffer_snprintf_done (&buf);
101 ___vsnprintf (char *string, size_t maxlen, const char *format, va_list args)
103 return __vsnprintf_internal (string, maxlen, format, args, 0);
105 ldbl_weak_alias (___vsnprintf, __vsnprintf)
106 ldbl_weak_alias (___vsnprintf, vsnprintf)