]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 2005-2019 Free Software Foundation, Inc. |
10ffcd52 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@gnu.org>. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
10ffcd52 UD |
18 | |
19 | #include <stdarg.h> | |
20 | #include <stdio.h> | |
544ce845 | 21 | #include <stdlib.h> |
757beee1 | 22 | #include <string.h> |
544ce845 | 23 | #include <wchar.h> |
e62995c1 | 24 | #include <libioP.h> |
10ffcd52 | 25 | |
544ce845 | 26 | static int |
f43b8dd5 GG |
27 | locked_vfxprintf (FILE *fp, const char *fmt, va_list ap, |
28 | unsigned int mode_flags) | |
544ce845 ZW |
29 | { |
30 | if (_IO_fwide (fp, 0) <= 0) | |
f43b8dd5 | 31 | return __vfprintf_internal (fp, fmt, ap, mode_flags); |
544ce845 ZW |
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 | int used_malloc = 0; | |
39 | size_t len = strlen (fmt) + 1; | |
40 | ||
41 | if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) | |
42 | { | |
43 | __set_errno (EOVERFLOW); | |
44 | return -1; | |
45 | } | |
46 | if (__libc_use_alloca (len * sizeof (wchar_t))) | |
47 | wfmt = alloca (len * sizeof (wchar_t)); | |
48 | else if ((wfmt = malloc (len * sizeof (wchar_t))) == NULL) | |
49 | return -1; | |
50 | else | |
51 | used_malloc = 1; | |
52 | ||
53 | memset (&mbstate, 0, sizeof mbstate); | |
54 | res = __mbsrtowcs (wfmt, &fmt, len, &mbstate); | |
55 | ||
56 | if (res != -1) | |
f43b8dd5 | 57 | res = __vfwprintf_internal (fp, wfmt, ap, mode_flags); |
544ce845 ZW |
58 | |
59 | if (used_malloc) | |
60 | free (wfmt); | |
61 | ||
62 | return res; | |
63 | } | |
10ffcd52 UD |
64 | |
65 | int | |
f43b8dd5 GG |
66 | __vfxprintf (FILE *fp, const char *fmt, va_list ap, |
67 | unsigned int mode_flags) | |
10ffcd52 UD |
68 | { |
69 | if (fp == NULL) | |
70 | fp = stderr; | |
544ce845 | 71 | _IO_flockfile (fp); |
f43b8dd5 | 72 | int res = locked_vfxprintf (fp, fmt, ap, mode_flags); |
544ce845 | 73 | _IO_funlockfile (fp); |
fdb16de3 FW |
74 | return res; |
75 | } | |
76 | ||
77 | int | |
78 | __fxprintf (FILE *fp, const char *fmt, ...) | |
79 | { | |
80 | va_list ap; | |
81 | va_start (ap, fmt); | |
f43b8dd5 | 82 | int res = __vfxprintf (fp, fmt, ap, 0); |
10ffcd52 | 83 | va_end (ap); |
544ce845 ZW |
84 | return res; |
85 | } | |
10ffcd52 | 86 | |
544ce845 ZW |
87 | int |
88 | __fxprintf_nocancel (FILE *fp, const char *fmt, ...) | |
89 | { | |
90 | if (fp == NULL) | |
91 | fp = stderr; | |
92 | ||
93 | va_list ap; | |
94 | va_start (ap, fmt); | |
95 | _IO_flockfile (fp); | |
9964a145 ZW |
96 | int save_flags2 = fp->_flags2; |
97 | fp->_flags2 |= _IO_FLAGS2_NOTCANCEL; | |
544ce845 | 98 | |
f43b8dd5 | 99 | int res = locked_vfxprintf (fp, fmt, ap, 0); |
544ce845 | 100 | |
9964a145 | 101 | fp->_flags2 = save_flags2; |
544ce845 ZW |
102 | _IO_funlockfile (fp); |
103 | va_end (ap); | |
10ffcd52 UD |
104 | return res; |
105 | } |