1 /* Printf Extension Example
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>.
31 print_widget (FILE *stream
,
32 const struct printf_info
*info
,
33 const void *const *args
)
39 /* Format the output into a string. */
40 w
= *((const Widget
**) (args
[0]));
41 len
= asprintf (&buffer
, "<Widget %p: %s>", w
, w
->name
);
45 /* Pad to the minimum field width and print to the stream. */
46 len
= fprintf (stream
, "%*s",
47 (info
->left
? -info
->width
: info
->width
),
50 /* Clean up and return. */
57 print_widget_arginfo (const struct printf_info
*info
, size_t n
,
60 /* We always take exactly one argument and this is a pointer to the
63 argtypes
[0] = PA_POINTER
;
71 /* Make a widget to print. */
73 mywidget
.name
= "mywidget";
75 /* Register the print function for widgets. */
76 register_printf_function ('W', print_widget
, print_widget_arginfo
);
78 /* Now print the widget. */
79 printf ("|%W|\n", &mywidget
);
80 printf ("|%35W|\n", &mywidget
);
81 printf ("|%-35W|\n", &mywidget
);