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.
13 #include "gansidecl.h"
16 #ifdef TEST /* Make sure to use the internal one. */
17 #define _doprnt my_doprnt
22 const int value = abs (va_arg (ap, int)); \
24 ptr++; /* Go past the asterisk. */ \
25 *sptr = '\0'; /* NULL terminate sptr. */ \
26 sprintf(buf, "%d", value); \
28 while (*sptr) sptr++; \
31 #define PRINT_CHAR(CHAR) \
39 #define PRINT_TYPE(TYPE) \
42 TYPE value = va_arg (ap, TYPE); \
43 *sptr++ = *ptr++; /* Copy the type specifier. */ \
44 *sptr = '\0'; /* NULL terminate sptr. */ \
45 result = fprintf(stream, specifier, value); \
50 total_printed += result; \
56 _doprnt (format
, ap
, stream
)
61 const char * ptr
= format
;
63 int total_printed
= 0;
67 if (*ptr
!= '%') /* While we have regular characters, print them. */
69 else /* We got a format specifier! */
71 char * sptr
= specifier
;
72 int wide_width
= 0, short_width
= 0;
74 *sptr
++ = *ptr
++; /* Copy the % and move forward. */
76 while (strchr ("-+ #0", *ptr
)) /* Move past flags. */
82 while (isdigit(*ptr
)) /* Handle explicit numeric value. */
87 *sptr
++ = *ptr
++; /* Copy and go past the period. */
91 while (isdigit(*ptr
)) /* Handle explicit numeric value. */
94 while (strchr ("hlL", *ptr
))
123 /* Short values are promoted to int, so just copy it
124 as an int and trust the C library printf to cast it
125 to the right width. */
140 #if defined(__GNUC__) || defined(HAVE_LONG_LONG)
141 PRINT_TYPE(long long);
143 PRINT_TYPE(long); /* Fake it and hope for the best. */
146 } /* End of switch (wide_width) */
147 } /* End of else statement */
148 } /* End of integer case */
160 #if defined(__GNUC__) || defined(HAVE_LONG_DOUBLE)
161 PRINT_TYPE(long double);
163 PRINT_TYPE(double); /* Fake it and hope for the best. */
179 } /* End of switch (*ptr) */
180 } /* End of else statement */
183 return total_printed
;
190 #define M_PI (3.1415926535897932385)
193 #define RESULT(x) do \
196 printf ("printed %d characters\n", i); \
200 static int checkit
PVPROTO ((const char * format
, ...)) ATTRIBUTE_PRINTF_1
;
203 checkit
VPROTO ((const char* format
, ...))
212 VA_START (args
, format
);
215 format
= va_arg (args
, char *);
218 result
= _doprnt (format
, args
, stdout
);
227 RESULT(checkit ("<%d>\n", 0x12345678));
228 RESULT(printf ("<%d>\n", 0x12345678));
230 RESULT(checkit ("<%200d>\n", 5));
231 RESULT(printf ("<%200d>\n", 5));
233 RESULT(checkit ("<%.300d>\n", 6));
234 RESULT(printf ("<%.300d>\n", 6));
236 RESULT(checkit ("<%100.150d>\n", 7));
237 RESULT(printf ("<%100.150d>\n", 7));
239 RESULT(checkit ("<%s>\n",
240 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
241 777777777777777777333333333333366666666666622222222222777777777777733333"));
242 RESULT(printf ("<%s>\n",
243 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
244 777777777777777777333333333333366666666666622222222222777777777777733333"));
246 RESULT(checkit ("<%f><%0+#f>%s%d%s>\n",
247 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
248 RESULT(printf ("<%f><%0+#f>%s%d%s>\n",
249 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
251 RESULT(checkit ("<%4f><%.4f><%%><%4.4f>\n", M_PI
, M_PI
, M_PI
));
252 RESULT(printf ("<%4f><%.4f><%%><%4.4f>\n", M_PI
, M_PI
, M_PI
));
254 RESULT(checkit ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI
, 3, M_PI
, 3, 3, M_PI
));
255 RESULT(printf ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI
, 3, M_PI
, 3, 3, M_PI
));
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));
262 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
263 75, 75, 75, 75, 75, 75, 75));
264 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
265 75, 75, 75, 75, 75, 75, 75));
267 RESULT(checkit ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
268 RESULT(printf ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
270 #if defined(__GNUC__) || defined (HAVE_LONG_LONG)
271 RESULT(checkit ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
272 RESULT(printf ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
273 RESULT(checkit ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
274 RESULT(printf ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
277 #if defined(__GNUC__) || defined (HAVE_LONG_DOUBLE)
278 RESULT(checkit ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
279 1.23456, 1.234567890123456789L, 1.23456));
280 RESULT(printf ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
281 1.23456, 1.234567890123456789L, 1.23456));