1 | /* Copyright (C) 2005-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 | #include <scratch_buffer.h> |
19 | #include <stdarg.h> |
20 | #include <stdio.h> |
21 | #include <stdlib.h> |
22 | #include <string.h> |
23 | #include <wchar.h> |
24 | #include <libioP.h> |
25 | |
26 | static int |
27 | locked_vfxprintf (FILE *fp, const char *fmt, va_list ap, |
28 | unsigned int mode_flags) |
29 | { |
30 | if (_IO_fwide (fp, 0) <= 0) |
31 | return __vfprintf_internal (fp, format: fmt, ap, mode_flags); |
32 | |
33 | /* We must convert the narrow format string to a wide one. |
34 | Each byte can produce at most one wide character. */ |
35 | wchar_t *wfmt; |
36 | mbstate_t mbstate; |
37 | int res; |
38 | size_t len = strlen (fmt) + 1; |
39 | struct scratch_buffer buf; |
40 | scratch_buffer_init (buffer: &buf); |
41 | |
42 | if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) |
43 | { |
44 | __set_errno (EOVERFLOW); |
45 | return -1; |
46 | } |
47 | if (!scratch_buffer_set_array_size (buffer: &buf, nelem: sizeof (wchar_t), size: len)) |
48 | return -1; |
49 | wfmt = buf.data; |
50 | |
51 | memset (&mbstate, 0, sizeof mbstate); |
52 | res = __mbsrtowcs (dst: wfmt, src: &fmt, len: len, ps: &mbstate); |
53 | |
54 | if (res != -1) |
55 | res = __vfwprintf_internal (fp, format: wfmt, ap, mode_flags); |
56 | |
57 | scratch_buffer_free (buffer: &buf); |
58 | |
59 | return res; |
60 | } |
61 | |
62 | int |
63 | __vfxprintf (FILE *fp, const char *fmt, va_list ap, |
64 | unsigned int mode_flags) |
65 | { |
66 | if (fp == NULL) |
67 | fp = stderr; |
68 | _IO_flockfile (fp); |
69 | int res = locked_vfxprintf (fp, fmt, ap, mode_flags); |
70 | _IO_funlockfile (fp); |
71 | return res; |
72 | } |
73 | |
74 | int |
75 | __fxprintf (FILE *fp, const char *fmt, ...) |
76 | { |
77 | va_list ap; |
78 | va_start (ap, fmt); |
79 | int res = __vfxprintf (fp, fmt, ap, mode_flags: 0); |
80 | va_end (ap); |
81 | return res; |
82 | } |
83 | |
84 | int |
85 | __fxprintf_nocancel (FILE *fp, const char *fmt, ...) |
86 | { |
87 | if (fp == NULL) |
88 | fp = stderr; |
89 | |
90 | va_list ap; |
91 | va_start (ap, fmt); |
92 | _IO_flockfile (fp); |
93 | int save_flags2 = fp->_flags2; |
94 | fp->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
95 | |
96 | int res = locked_vfxprintf (fp, fmt, ap, mode_flags: 0); |
97 | |
98 | fp->_flags2 = save_flags2; |
99 | _IO_funlockfile (fp); |
100 | va_end (ap); |
101 | return res; |
102 | } |
103 | |