tunables: Use direct syscall for access (BZ#21744)
[glibc.git] / stdio-common / fxprintf.c
blob82d4f2e235c93c5c0d482893ce86385738da160e
1 /* Copyright (C) 2005-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <wchar.h>
24 #include <libioP.h>
26 static int
27 locked_vfxprintf (FILE *fp, const char *fmt, va_list ap)
29 if (_IO_fwide (fp, 0) <= 0)
30 return _IO_vfprintf (fp, fmt, ap);
32 /* We must convert the narrow format string to a wide one.
33 Each byte can produce at most one wide character. */
34 wchar_t *wfmt;
35 mbstate_t mbstate;
36 int res;
37 int used_malloc = 0;
38 size_t len = strlen (fmt) + 1;
40 if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t)))
42 __set_errno (EOVERFLOW);
43 return -1;
45 if (__libc_use_alloca (len * sizeof (wchar_t)))
46 wfmt = alloca (len * sizeof (wchar_t));
47 else if ((wfmt = malloc (len * sizeof (wchar_t))) == NULL)
48 return -1;
49 else
50 used_malloc = 1;
52 memset (&mbstate, 0, sizeof mbstate);
53 res = __mbsrtowcs (wfmt, &fmt, len, &mbstate);
55 if (res != -1)
56 res = _IO_vfwprintf (fp, wfmt, ap);
58 if (used_malloc)
59 free (wfmt);
61 return res;
64 int
65 __fxprintf (FILE *fp, const char *fmt, ...)
67 if (fp == NULL)
68 fp = stderr;
70 va_list ap;
71 va_start (ap, fmt);
72 _IO_flockfile (fp);
74 int res = locked_vfxprintf (fp, fmt, ap);
76 _IO_funlockfile (fp);
77 va_end (ap);
78 return res;
81 int
82 __fxprintf_nocancel (FILE *fp, const char *fmt, ...)
84 if (fp == NULL)
85 fp = stderr;
87 va_list ap;
88 va_start (ap, fmt);
89 _IO_flockfile (fp);
90 int save_flags2 = ((_IO_FILE *)fp)->_flags2;
91 ((_IO_FILE *)fp)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
93 int res = locked_vfxprintf (fp, fmt, ap);
95 ((_IO_FILE *)fp)->_flags2 = save_flags2;
96 _IO_funlockfile (fp);
97 va_end (ap);
98 return res;