Update.
[glibc.git] / manual / examples / rprintf.c
blobbac17b49e5a9988d03def333e99874069bc7969c
1 #include <stdio.h>
2 #include <printf.h>
3 #include <stdarg.h>
5 /*@group*/
6 typedef struct
8 char *name;
9 } Widget;
10 /*@end group*/
12 int
13 print_widget (FILE *stream,
14 const struct printf_info *info,
15 va_list *app)
17 Widget *w;
18 char *buffer;
19 int len;
21 /* Format the output into a string. */
22 w = va_arg (*app, Widget *);
23 len = asprintf (&buffer, "<Widget %p: %s>", w, w->name);
24 if (len == -1)
25 return -1;
27 /* Pad to the minimum field width and print to the stream. */
28 len = fprintf (stream, "%*s",
29 (info->left ? - info->width : info->width),
30 buffer);
32 /* Clean up and return. */
33 free (buffer);
34 return len;
38 int
39 print_widget_arginfo (const struct printf_info *info, size_t n,
40 int *argtypes)
42 /* We always take exactly one argument and this is a pointer to the
43 structure.. */
44 if (n > 0)
45 argtypes[0] = PA_POINTER;
46 return 1;
50 int
51 main (void)
53 /* Make a widget to print. */
54 Widget mywidget;
55 mywidget.name = "mywidget";
57 /* Register the print function for widgets. */
58 register_printf_function ('W', print_widget, print_widget_arginfo);
60 /* Now print the widget. */
61 printf ("|%W|\n", &mywidget);
62 printf ("|%35W|\n", &mywidget);
63 printf ("|%-35W|\n", &mywidget);
65 return 0;