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