]> gcc.gnu.org Git - gcc.git/blob - gcc/doprint.c
system.h: Include stdarg.h/varargs.h...
[gcc.git] / gcc / doprint.c
1 /* Provide a version _doprnt in terms of fprintf.
2 By Kaveh Ghazi (ghazi@caip.rutgers.edu) 3/29/98
3 Copyright (C) 1998 Free Software Foundation, Inc.
4 */
5
6 #include "config.h"
7 #include "system.h"
8 #include "gansidecl.h"
9 #undef _doprnt
10
11 #ifdef TEST /* Make sure to use the internal one. */
12 #define _doprnt my_doprnt
13 #endif
14
15 #define COPY_VA_INT \
16 do { \
17 const int value = abs (va_arg (ap, int)); \
18 char buf[32]; \
19 ptr++; /* Go past the asterisk. */ \
20 *sptr = '\0'; /* NULL terminate sptr. */ \
21 sprintf(buf, "%d", value); \
22 strcat(sptr, buf); \
23 while (*sptr) sptr++; \
24 } while (0)
25
26 #define PRINT_CHAR(CHAR) \
27 do { \
28 putc(CHAR, stream); \
29 ptr++; \
30 total_printed++; \
31 continue; \
32 } while (0)
33
34 #define PRINT_TYPE(TYPE) \
35 do { \
36 int result; \
37 TYPE value = va_arg (ap, TYPE); \
38 *sptr++ = *ptr++; /* Copy the type specifier. */ \
39 *sptr = '\0'; /* NULL terminate sptr. */ \
40 result = fprintf(stream, specifier, value); \
41 if (result == -1) \
42 return -1; \
43 else \
44 { \
45 total_printed += result; \
46 continue; \
47 } \
48 } while (0)
49
50 int
51 _doprnt (format, ap, stream)
52 const char * format;
53 va_list ap;
54 FILE * stream;
55 {
56 const char * ptr = format;
57 char specifier[128];
58 int total_printed = 0;
59
60 while (*ptr != '\0')
61 {
62 if (*ptr != '%') /* While we have regular characters, print them. */
63 PRINT_CHAR(*ptr);
64 else /* We got a format specifier! */
65 {
66 char * sptr = specifier;
67 int wide_width = 0, short_width = 0;
68
69 *sptr++ = *ptr++; /* Copy the % and move forward. */
70
71 while (strchr ("-+ #0", *ptr)) /* Move past flags. */
72 *sptr++ = *ptr++;
73
74 if (*ptr == '*')
75 COPY_VA_INT;
76 else
77 while (isdigit(*ptr)) /* Handle explicit numeric value. */
78 *sptr++ = *ptr++;
79
80 if (*ptr == '.')
81 {
82 *sptr++ = *ptr++; /* Copy and go past the period. */
83 if (*ptr == '*')
84 COPY_VA_INT;
85 else
86 while (isdigit(*ptr)) /* Handle explicit numeric value. */
87 *sptr++ = *ptr++;
88 }
89 while (strchr ("hlL", *ptr))
90 {
91 switch (*ptr)
92 {
93 case 'h':
94 short_width = 1;
95 break;
96 case 'l':
97 wide_width++;
98 break;
99 case 'L':
100 wide_width = 2;
101 break;
102 default:
103 abort();
104 }
105 *sptr++ = *ptr++;
106 }
107
108 switch (*ptr)
109 {
110 case 'd':
111 case 'i':
112 case 'o':
113 case 'u':
114 case 'x':
115 case 'X':
116 case 'c':
117 {
118 /* Short values are promoted to int, so just copy it
119 as an int and trust the C library printf to cast it
120 to the right width. */
121 if (short_width)
122 PRINT_TYPE(int);
123 else
124 {
125 switch (wide_width)
126 {
127 case 0:
128 PRINT_TYPE(int);
129 break;
130 case 1:
131 PRINT_TYPE(long);
132 break;
133 case 2:
134 default:
135 #if defined(__GNUC__) || defined(HAVE_LONG_LONG)
136 PRINT_TYPE(long long);
137 #else
138 PRINT_TYPE(long); /* Fake it and hope for the best. */
139 #endif
140 break;
141 } /* End of switch (wide_width) */
142 } /* End of else statement */
143 } /* End of integer case */
144 break;
145 case 'f':
146 case 'e':
147 case 'E':
148 case 'g':
149 case 'G':
150 {
151 if (wide_width == 0)
152 PRINT_TYPE(double);
153 else
154 {
155 #if defined(__GNUC__) || defined(HAVE_LONG_DOUBLE)
156 PRINT_TYPE(long double);
157 #else
158 PRINT_TYPE(double); /* Fake it and hope for the best. */
159 #endif
160 }
161 }
162 break;
163 case 's':
164 PRINT_TYPE(char *);
165 break;
166 case 'p':
167 PRINT_TYPE(void *);
168 break;
169 case '%':
170 PRINT_CHAR('%');
171 break;
172 default:
173 abort();
174 } /* End of switch (*ptr) */
175 } /* End of else statement */
176 }
177
178 return total_printed;
179 }
180
181 #ifdef TEST
182
183 #include <math.h>
184 #ifndef M_PI
185 #define M_PI (3.1415926535897932385)
186 #endif
187
188 #define RESULT(x) do \
189 { \
190 int i = (x); \
191 printf ("printed %d characters\n", i); \
192 fflush(stdin); \
193 } while (0)
194
195 static int checkit PVPROTO ((const char * format, ...)) ATTRIBUTE_PRINTF_1;
196
197 static int
198 checkit VPROTO ((const char* format, ...))
199 {
200 va_list args;
201 int result;
202
203 #ifndef __STDC__
204 char *format;
205 #endif
206
207 VA_START (args, format);
208
209 #ifndef __STDC__
210 format = va_arg (args, char *);
211 #endif
212
213 result = _doprnt (format, args, stdout);
214 va_end(args);
215
216 return result;
217 }
218
219 int
220 main ()
221 {
222 RESULT(checkit ("<%d>\n", 0x12345678));
223 RESULT(printf ("<%d>\n", 0x12345678));
224
225 RESULT(checkit ("<%200d>\n", 5));
226 RESULT(printf ("<%200d>\n", 5));
227
228 RESULT(checkit ("<%.300d>\n", 6));
229 RESULT(printf ("<%.300d>\n", 6));
230
231 RESULT(checkit ("<%100.150d>\n", 7));
232 RESULT(printf ("<%100.150d>\n", 7));
233
234 RESULT(checkit ("<%s>\n",
235 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
236 777777777777777777333333333333366666666666622222222222777777777777733333"));
237 RESULT(printf ("<%s>\n",
238 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
239 777777777777777777333333333333366666666666622222222222777777777777733333"));
240
241 RESULT(checkit ("<%f><%0+#f>%s%d%s>\n",
242 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
243 RESULT(printf ("<%f><%0+#f>%s%d%s>\n",
244 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
245
246 RESULT(checkit ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
247 RESULT(printf ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
248
249 RESULT(checkit ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
250 RESULT(printf ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
251
252 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
253 75, 75, 75, 75, 75, 75, 75));
254 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
255 75, 75, 75, 75, 75, 75, 75));
256
257 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
258 75, 75, 75, 75, 75, 75, 75));
259 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
260 75, 75, 75, 75, 75, 75, 75));
261
262 RESULT(checkit ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
263 RESULT(printf ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
264
265 #if defined(__GNUC__) || defined (HAVE_LONG_LONG)
266 RESULT(checkit ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
267 RESULT(printf ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
268 RESULT(checkit ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
269 RESULT(printf ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
270 #endif
271
272 #if defined(__GNUC__) || defined (HAVE_LONG_DOUBLE)
273 RESULT(checkit ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
274 1.23456, 1.234567890123456789L, 1.23456));
275 RESULT(printf ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
276 1.23456, 1.234567890123456789L, 1.23456));
277 #endif
278
279 return 0;
280 }
281 #endif /* TEST */
This page took 0.055411 seconds and 6 git commands to generate.