diff options
Diffstat (limited to 'gl/printf-args.c')
-rw-r--r-- | gl/printf-args.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/gl/printf-args.c b/gl/printf-args.c new file mode 100644 index 0000000..358801c --- /dev/null +++ b/gl/printf-args.c | |||
@@ -0,0 +1,141 @@ | |||
1 | /* Decomposed printf argument list. | ||
2 | Copyright (C) 1999, 2002-2003, 2005-2006 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2, or (at your option) | ||
7 | any later version. | ||
8 | |||
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. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License along | ||
15 | with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
17 | |||
18 | #include <config.h> | ||
19 | |||
20 | /* Specification. */ | ||
21 | #include "printf-args.h" | ||
22 | |||
23 | #ifdef STATIC | ||
24 | STATIC | ||
25 | #endif | ||
26 | int | ||
27 | printf_fetchargs (va_list args, arguments *a) | ||
28 | { | ||
29 | size_t i; | ||
30 | argument *ap; | ||
31 | |||
32 | for (i = 0, ap = &a->arg[0]; i < a->count; i++, ap++) | ||
33 | switch (ap->type) | ||
34 | { | ||
35 | case TYPE_SCHAR: | ||
36 | ap->a.a_schar = va_arg (args, /*signed char*/ int); | ||
37 | break; | ||
38 | case TYPE_UCHAR: | ||
39 | ap->a.a_uchar = va_arg (args, /*unsigned char*/ int); | ||
40 | break; | ||
41 | case TYPE_SHORT: | ||
42 | ap->a.a_short = va_arg (args, /*short*/ int); | ||
43 | break; | ||
44 | case TYPE_USHORT: | ||
45 | ap->a.a_ushort = va_arg (args, /*unsigned short*/ int); | ||
46 | break; | ||
47 | case TYPE_INT: | ||
48 | ap->a.a_int = va_arg (args, int); | ||
49 | break; | ||
50 | case TYPE_UINT: | ||
51 | ap->a.a_uint = va_arg (args, unsigned int); | ||
52 | break; | ||
53 | case TYPE_LONGINT: | ||
54 | ap->a.a_longint = va_arg (args, long int); | ||
55 | break; | ||
56 | case TYPE_ULONGINT: | ||
57 | ap->a.a_ulongint = va_arg (args, unsigned long int); | ||
58 | break; | ||
59 | #ifdef HAVE_LONG_LONG_INT | ||
60 | case TYPE_LONGLONGINT: | ||
61 | ap->a.a_longlongint = va_arg (args, long long int); | ||
62 | break; | ||
63 | case TYPE_ULONGLONGINT: | ||
64 | ap->a.a_ulonglongint = va_arg (args, unsigned long long int); | ||
65 | break; | ||
66 | #endif | ||
67 | case TYPE_DOUBLE: | ||
68 | ap->a.a_double = va_arg (args, double); | ||
69 | break; | ||
70 | #ifdef HAVE_LONG_DOUBLE | ||
71 | case TYPE_LONGDOUBLE: | ||
72 | ap->a.a_longdouble = va_arg (args, long double); | ||
73 | break; | ||
74 | #endif | ||
75 | case TYPE_CHAR: | ||
76 | ap->a.a_char = va_arg (args, int); | ||
77 | break; | ||
78 | #ifdef HAVE_WINT_T | ||
79 | case TYPE_WIDE_CHAR: | ||
80 | /* Although ISO C 99 7.24.1.(2) says that wint_t is "unchanged by | ||
81 | default argument promotions", this is not the case in mingw32, | ||
82 | where wint_t is 'unsigned short'. */ | ||
83 | ap->a.a_wide_char = | ||
84 | (sizeof (wint_t) < sizeof (int) | ||
85 | ? va_arg (args, int) | ||
86 | : va_arg (args, wint_t)); | ||
87 | break; | ||
88 | #endif | ||
89 | case TYPE_STRING: | ||
90 | ap->a.a_string = va_arg (args, const char *); | ||
91 | /* A null pointer is an invalid argument for "%s", but in practice | ||
92 | it occurs quite frequently in printf statements that produce | ||
93 | debug output. Use a fallback in this case. */ | ||
94 | if (ap->a.a_string == NULL) | ||
95 | ap->a.a_string = "(NULL)"; | ||
96 | break; | ||
97 | #ifdef HAVE_WCHAR_T | ||
98 | case TYPE_WIDE_STRING: | ||
99 | ap->a.a_wide_string = va_arg (args, const wchar_t *); | ||
100 | /* A null pointer is an invalid argument for "%ls", but in practice | ||
101 | it occurs quite frequently in printf statements that produce | ||
102 | debug output. Use a fallback in this case. */ | ||
103 | if (ap->a.a_wide_string == NULL) | ||
104 | { | ||
105 | static const wchar_t wide_null_string[] = | ||
106 | { | ||
107 | (wchar_t)'(', | ||
108 | (wchar_t)'N', (wchar_t)'U', (wchar_t)'L', (wchar_t)'L', | ||
109 | (wchar_t)')', | ||
110 | (wchar_t)0 | ||
111 | }; | ||
112 | ap->a.a_wide_string = wide_null_string; | ||
113 | } | ||
114 | break; | ||
115 | #endif | ||
116 | case TYPE_POINTER: | ||
117 | ap->a.a_pointer = va_arg (args, void *); | ||
118 | break; | ||
119 | case TYPE_COUNT_SCHAR_POINTER: | ||
120 | ap->a.a_count_schar_pointer = va_arg (args, signed char *); | ||
121 | break; | ||
122 | case TYPE_COUNT_SHORT_POINTER: | ||
123 | ap->a.a_count_short_pointer = va_arg (args, short *); | ||
124 | break; | ||
125 | case TYPE_COUNT_INT_POINTER: | ||
126 | ap->a.a_count_int_pointer = va_arg (args, int *); | ||
127 | break; | ||
128 | case TYPE_COUNT_LONGINT_POINTER: | ||
129 | ap->a.a_count_longint_pointer = va_arg (args, long int *); | ||
130 | break; | ||
131 | #ifdef HAVE_LONG_LONG_INT | ||
132 | case TYPE_COUNT_LONGLONGINT_POINTER: | ||
133 | ap->a.a_count_longlongint_pointer = va_arg (args, long long int *); | ||
134 | break; | ||
135 | #endif | ||
136 | default: | ||
137 | /* Unknown type. */ | ||
138 | return -1; | ||
139 | } | ||
140 | return 0; | ||
141 | } | ||