1 /* Provide a version of _doprnt in terms of fprintf.
2 Copyright (C) 1998-2019 Free Software Foundation, Inc.
3 Contributed by Kaveh Ghazi (ghazi@caip.rutgers.edu) 3/29/98
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "safe-ctype.h"
38 #ifdef TEST /* Make sure to use the internal one. */
39 #define _doprnt my_doprnt
44 const int value = abs (va_arg (ap, int)); \
46 ptr++; /* Go past the asterisk. */ \
47 *sptr = '\0'; /* NULL terminate sptr. */ \
48 sprintf(buf, "%d", value); \
50 while (*sptr) sptr++; \
53 #define PRINT_CHAR(CHAR) \
61 #define PRINT_TYPE(TYPE) \
64 TYPE value = va_arg (ap, TYPE); \
65 *sptr++ = *ptr++; /* Copy the type specifier. */ \
66 *sptr = '\0'; /* NULL terminate sptr. */ \
67 result = fprintf(stream, specifier, value); \
72 total_printed += result; \
78 _doprnt (const char *format
, va_list ap
, FILE *stream
)
80 const char * ptr
= format
;
82 int total_printed
= 0;
86 if (*ptr
!= '%') /* While we have regular characters, print them. */
88 else /* We got a format specifier! */
90 char * sptr
= specifier
;
91 int wide_width
= 0, short_width
= 0;
93 *sptr
++ = *ptr
++; /* Copy the % and move forward. */
95 while (strchr ("-+ #0", *ptr
)) /* Move past flags. */
101 while (ISDIGIT(*ptr
)) /* Handle explicit numeric value. */
106 *sptr
++ = *ptr
++; /* Copy and go past the period. */
110 while (ISDIGIT(*ptr
)) /* Handle explicit numeric value. */
113 while (strchr ("hlL", *ptr
))
142 /* Short values are promoted to int, so just copy it
143 as an int and trust the C library printf to cast it
144 to the right width. */
159 #if defined(__GNUC__) || defined(HAVE_LONG_LONG)
160 PRINT_TYPE(long long);
162 PRINT_TYPE(long); /* Fake it and hope for the best. */
165 } /* End of switch (wide_width) */
166 } /* End of else statement */
167 } /* End of integer case */
179 #if defined(__GNUC__) || defined(HAVE_LONG_DOUBLE)
180 PRINT_TYPE(long double);
182 PRINT_TYPE(double); /* Fake it and hope for the best. */
198 } /* End of switch (*ptr) */
199 } /* End of else statement */
202 return total_printed
;
209 #define M_PI (3.1415926535897932385)
212 #define RESULT(x) do \
215 printf ("printed %d characters\n", i); \
219 static int checkit (const char * format
, ...) ATTRIBUTE_PRINTF_1
;
222 checkit (const char* format
, ...)
226 va_start (args
, format
);
228 result
= _doprnt (format
, args
, stdout
);
237 RESULT(checkit ("<%d>\n", 0x12345678));
238 RESULT(printf ("<%d>\n", 0x12345678));
240 RESULT(checkit ("<%200d>\n", 5));
241 RESULT(printf ("<%200d>\n", 5));
243 RESULT(checkit ("<%.300d>\n", 6));
244 RESULT(printf ("<%.300d>\n", 6));
246 RESULT(checkit ("<%100.150d>\n", 7));
247 RESULT(printf ("<%100.150d>\n", 7));
249 RESULT(checkit ("<%s>\n",
250 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
251 777777777777777777333333333333366666666666622222222222777777777777733333"));
252 RESULT(printf ("<%s>\n",
253 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
254 777777777777777777333333333333366666666666622222222222777777777777733333"));
256 RESULT(checkit ("<%f><%0+#f>%s%d%s>\n",
257 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
258 RESULT(printf ("<%f><%0+#f>%s%d%s>\n",
259 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
261 RESULT(checkit ("<%4f><%.4f><%%><%4.4f>\n", M_PI
, M_PI
, M_PI
));
262 RESULT(printf ("<%4f><%.4f><%%><%4.4f>\n", M_PI
, M_PI
, M_PI
));
264 RESULT(checkit ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI
, 3, M_PI
, 3, 3, M_PI
));
265 RESULT(printf ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI
, 3, M_PI
, 3, 3, M_PI
));
267 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
268 75, 75, 75, 75, 75, 75, 75));
269 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
270 75, 75, 75, 75, 75, 75, 75));
272 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
273 75, 75, 75, 75, 75, 75, 75));
274 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
275 75, 75, 75, 75, 75, 75, 75));
277 RESULT(checkit ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
278 RESULT(printf ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
280 #if defined(__GNUC__) || defined (HAVE_LONG_LONG)
281 RESULT(checkit ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
282 RESULT(printf ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
283 RESULT(checkit ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
284 RESULT(printf ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
287 #if defined(__GNUC__) || defined (HAVE_LONG_DOUBLE)
288 RESULT(checkit ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
289 1.23456, 1.234567890123456789L, 1.23456));
290 RESULT(printf ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
291 1.23456, 1.234567890123456789L, 1.23456));