linux: Decorate __libc_fatal error buffer
[glibc.git] / stdio-common / Xprintf_function_invoke.c
blob6df2ac5c80e28cbbbbc6b17e7651bd8aeca2a541
1 /* Invoke a printf specifier handler. Generic version.
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
21 int
22 Xprintf (function_invoke) (void *buf,
23 printf_function callback,
24 union printf_arg *args_value,
25 size_t ndata_args,
26 struct printf_info *info)
28 /* Most custom specifiers expect just one argument. Use the heap
29 for larger argument arrays. */
30 const void *onstack_args[4];
31 const void **args;
32 if (ndata_args <= array_length (onstack_args))
33 args = onstack_args;
34 else
36 args = calloc (ndata_args, sizeof (*args));
37 if (args == NULL)
38 return -1;
41 for (unsigned int i = 0; i < ndata_args; ++i)
42 args[i] = &args_value[i];
44 struct Xprintf (buffer_as_file) s;
45 Xprintf (buffer_as_file_init) (&s, buf);
47 /* Call the function. */
48 int done = callback (Xprintf (buffer_as_file_get) (&s), info, args);
50 if (!Xprintf (buffer_as_file_terminate) (&s))
51 done = -1;
53 if (args != onstack_args)
54 free (args);
56 /* Potential error from the callback function. */
57 return done;