]>
Commit | Line | Data |
---|---|---|
c7bf2e99 | 1 | /* Invoke a printf specifier handler. Generic version. |
dff8da6b | 2 | Copyright (C) 1991-2024 Free Software Foundation, Inc. |
c7bf2e99 FW |
3 | This file is part of the GNU C Library. |
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 | |
16 | License along with the GNU C Library; if not, see | |
17 | <https://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <array_length.h> | |
20 | ||
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) | |
27 | { | |
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 | |
35 | { | |
36 | args = calloc (ndata_args, sizeof (*args)); | |
37 | if (args == NULL) | |
38 | return -1; | |
39 | } | |
40 | ||
41 | for (unsigned int i = 0; i < ndata_args; ++i) | |
42 | args[i] = &args_value[i]; | |
43 | ||
44 | struct Xprintf (buffer_as_file) s; | |
45 | Xprintf (buffer_as_file_init) (&s, buf); | |
46 | ||
47 | /* Call the function. */ | |
48 | int done = callback (Xprintf (buffer_as_file_get) (&s), info, args); | |
49 | ||
50 | if (!Xprintf (buffer_as_file_terminate) (&s)) | |
51 | done = -1; | |
52 | ||
53 | if (args != onstack_args) | |
54 | free (args); | |
55 | ||
56 | /* Potential error from the callback function. */ | |
57 | return done; | |
58 | } |