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.
10 #ifdef TEST /* Make sure to use the internal one. */
11 #define _doprnt my_doprnt
16 const int value = abs (va_arg (ap, int)); \
18 ptr++; /* Go past the asterisk. */ \
19 *sptr = '\0'; /* NULL terminate sptr. */ \
20 sprintf(buf, "%d", value); \
22 while (*sptr) sptr++; \
25 #define PRINT_CHAR(CHAR) \
33 #define PRINT_TYPE(TYPE) \
36 TYPE value = va_arg (ap, TYPE); \
37 *sptr++ = *ptr++; /* Copy the type specifier. */ \
38 *sptr = '\0'; /* NULL terminate sptr. */ \
39 result = fprintf(stream, specifier, value); \
44 total_printed += result; \
50 _doprnt (format, ap, stream)
55 const char * ptr = format;
57 int total_printed = 0;
61 if (*ptr != '%') /* While we have regular characters, print them. */
63 else /* We got a format specifier! */
65 char * sptr = specifier;
66 int wide_width = 0, short_width = 0;
68 *sptr++ = *ptr++; /* Copy the % and move forward. */
70 while (strchr ("-+ #0", *ptr)) /* Move past flags. */
76 while (isdigit(*ptr)) /* Handle explicit numeric value. */
81 *sptr++ = *ptr++; /* Copy and go past the period. */
85 while (isdigit(*ptr)) /* Handle explicit numeric value. */
88 while (strchr ("hlL", *ptr))
117 /* Short values are promoted to int, so just copy it
118 as an int and trust the C library printf to cast it
119 to the right width. */
134 #if defined(__GNUC__) || defined(HAVE_LONG_LONG)
135 PRINT_TYPE(long long);
137 PRINT_TYPE(long); /* Fake it and hope for the best. */
140 } /* End of switch (wide_width) */
141 } /* End of else statement */
142 } /* End of integer case */
154 #if defined(__GNUC__) || defined(HAVE_LONG_DOUBLE)
155 PRINT_TYPE(long double);
157 PRINT_TYPE(double); /* Fake it and hope for the best. */
173 } /* End of switch (*ptr) */
174 } /* End of else statement */
177 return total_printed;
184 #define M_PI (3.1415926535897932385)
187 #define RESULT(x) do \
190 printf ("printed %d characters\n", i); \
194 static int checkit PVPROTO ((const char * format, ...)) ATTRIBUTE_PRINTF_1;
197 checkit VPROTO ((const char* format, ...))
202 #ifndef ANSI_PROTOTYPES
206 VA_START (args, format);
208 #ifndef ANSI_PROTOTYPES
209 format = va_arg (args, char *);
212 result = _doprnt (format, args, stdout);
221 RESULT(checkit ("<%d>\n", 0x12345678));
222 RESULT(printf ("<%d>\n", 0x12345678));
224 RESULT(checkit ("<%200d>\n", 5));
225 RESULT(printf ("<%200d>\n", 5));
227 RESULT(checkit ("<%.300d>\n", 6));
228 RESULT(printf ("<%.300d>\n", 6));
230 RESULT(checkit ("<%100.150d>\n", 7));
231 RESULT(printf ("<%100.150d>\n", 7));
233 RESULT(checkit ("<%s>\n",
234 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
235 777777777777777777333333333333366666666666622222222222777777777777733333"));
236 RESULT(printf ("<%s>\n",
237 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
238 777777777777777777333333333333366666666666622222222222777777777777733333"));
240 RESULT(checkit ("<%f><%0+#f>%s%d%s>\n",
241 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
242 RESULT(printf ("<%f><%0+#f>%s%d%s>\n",
243 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
245 RESULT(checkit ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
246 RESULT(printf ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
248 RESULT(checkit ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
249 RESULT(printf ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
251 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
252 75, 75, 75, 75, 75, 75, 75));
253 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
254 75, 75, 75, 75, 75, 75, 75));
256 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
257 75, 75, 75, 75, 75, 75, 75));
258 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
259 75, 75, 75, 75, 75, 75, 75));
261 RESULT(checkit ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
262 RESULT(printf ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
264 #if defined(__GNUC__) || defined (HAVE_LONG_LONG)
265 RESULT(checkit ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
266 RESULT(printf ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
267 RESULT(checkit ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
268 RESULT(printf ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
271 #if defined(__GNUC__) || defined (HAVE_LONG_DOUBLE)
272 RESULT(checkit ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
273 1.23456, 1.234567890123456789L, 1.23456));
274 RESULT(printf ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
275 1.23456, 1.234567890123456789L, 1.23456));