1 /**************************************************************
3 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
4 * A bombproof version of doprnt(dopr) included.
5 * Sigh. This sort of thing is always nasty do deal with. Note that
6 * the version here does not include floating point...
8 * snprintf() is used instead of sprintf() as it does limit checks
9 * for string length. This covers a nasty loophole.
11 * The other functions are there to prevent NULL pointers from
12 * causing nast effects.
15 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
16 * This was ugly. It is still ugly. I opted out of floating point
17 * numbers, but the formatter understands just about everything
18 * from the normal C string format, at least as far as I can tell from
19 * the Solaris 2.5 printf(3S) man page.
21 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
22 * Ok, added some minimal floating point support, which means this
23 * probably requires libm on most operating systems. Don't yet
24 * support the exponent(e, E) and sigfig(g, G). Also, fmtint()
25 * was pretty badly broken, it just wasn't being exercised in ways
26 * which showed it, so that's been fixed. Also, formated the code
27 * to mutt conventions, and removed dead code left over from the
28 * original. Also, there is now a builtin-test, just compile with:
29 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
30 * and run snprintf for results.
32 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
33 * The PGP code was using unsigned hexadecimal formats.
34 * Unfortunately, unsigned formats simply didn't work.
36 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
37 * The original code assumed that both snprintf() and vsnprintf() were
38 * missing. Some systems only have snprintf() but not vsnprintf(), so
39 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
41 * Ben Lindstrom <mouring@eviladmin.org> 09/27/00 for OpenSSH
42 * Welcome to the world of %lld and %qd support. With other
43 * long long support. This is needed for sftp-server to work
46 * Ben Lindstrom <mouring@eviladmin.org> 02/12/01 for OpenSSH
47 * Removed all hint of VARARGS stuff and banished it to the void,
48 * and did a bit of KNF style work to make things a bit more
49 * acceptable. Consider stealing from mutt or enlightenment.
50 **************************************************************/
55 typedef void (prfun)(char *, size_t *, size_t, int);
58 dopr(char *buffer, size_t maxlen, const char *format, va_list args, prfun);
62 fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags,
63 int min, int max, prfun);
66 fmtint(char *buffer, size_t *currlen, size_t maxlen, INT64 value, int base,
67 int min, int max, int flags, prfun);
70 fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
71 int min, int max, int flags, prfun);
74 dopr_outch(char *buffer, size_t *currlen, size_t maxlen, int c);
77 * dopr(): poor man's version of doprintf
81 #define MAX(a,b) ((a)>(b)?(a):(b))
84 /* format read states */
85 #define DP_S_DEFAULT 0
94 /* format flags - Bits */
95 #define DP_F_MINUS (1 << 0)
96 #define DP_F_PLUS (1 << 1)
97 #define DP_F_SPACE (1 << 2)
98 #define DP_F_NUM (1 << 3)
99 #define DP_F_ZERO (1 << 4)
100 #define DP_F_UP (1 << 5)
101 #define DP_F_UNSIGNED (1 << 6)
103 /* Conversion Flags */
106 #define DP_C_LDOUBLE 3
107 #define DP_C_LONG_LONG 4
109 #define char_to_int(p) (p - '0')
110 #define abs_val(p) (p < 0 ? -p : p)
112 static const char digitval[] = "0123456789abcdef0123456789ABCDEF";
115 dopr(char *buffer, size_t maxlen, const char *format, va_list args, prfun outch)
123 int state = DP_S_DEFAULT;
130 while (state != DP_S_DONE)
132 if ((ch == '\0') || (currlen >= maxlen))
141 outch(buffer, &currlen, maxlen, ch);
173 if (isdigit((unsigned char)ch))
175 min = 10*min + char_to_int(ch);
180 min = va_arg(args, int);
197 if (isdigit((unsigned char)ch))
201 max = 10*max + char_to_int(ch);
206 max = va_arg(args, int);
225 cflags = DP_C_LONG_LONG;
230 cflags = DP_C_LONG_LONG;
234 cflags = DP_C_LDOUBLE;
246 flags |= DP_F_UNSIGNED;
247 if (cflags == DP_C_SHORT)
248 value = va_arg(args, unsigned int);
249 else if (cflags == DP_C_LONG)
250 value = va_arg(args, unsigned long int);
251 else if (cflags == DP_C_LONG_LONG)
252 value = va_arg(args, UINT64);
254 value = va_arg(args, unsigned int);
255 fmtint(buffer, &currlen, maxlen, value, 2, min, max, flags, outch);
259 if (cflags == DP_C_SHORT)
260 value = va_arg(args, int);
261 else if (cflags == DP_C_LONG)
262 value = va_arg(args, long int);
263 else if (cflags == DP_C_LONG_LONG)
264 value = va_arg(args, INT64);
266 value = va_arg(args, int);
267 fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags, outch);
270 flags |= DP_F_UNSIGNED;
271 if (cflags == DP_C_SHORT)
272 value = va_arg(args, unsigned int);
273 else if (cflags == DP_C_LONG)
274 value = va_arg(args, unsigned long int);
275 else if (cflags == DP_C_LONG_LONG)
276 value = va_arg(args, UINT64);
278 value = va_arg(args, unsigned int);
279 fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags, outch);
282 flags |= DP_F_UNSIGNED;
283 if (cflags == DP_C_SHORT)
284 value = va_arg(args, unsigned int);
285 else if (cflags == DP_C_LONG)
286 value = va_arg(args, unsigned long int);
287 else if (cflags == DP_C_LONG_LONG)
288 value = va_arg(args, UINT64);
290 value = va_arg(args, unsigned int);
291 fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags, outch);
296 flags |= DP_F_UNSIGNED;
297 if (cflags == DP_C_SHORT)
298 value = va_arg(args, unsigned int);
299 else if (cflags == DP_C_LONG)
300 value = va_arg(args, unsigned long int);
301 else if (cflags == DP_C_LONG_LONG)
302 value = va_arg(args, UINT64);
304 value = va_arg(args, unsigned int);
305 fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags, outch);
308 if (cflags == DP_C_LDOUBLE)
309 fvalue = va_arg(args, long double);
311 fvalue = va_arg(args, double);
312 /* um, floating point? */
313 fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags, outch);
318 if (cflags == DP_C_LDOUBLE)
319 fvalue = va_arg(args, long double);
321 fvalue = va_arg(args, double);
326 if (cflags == DP_C_LDOUBLE)
327 fvalue = va_arg(args, long double);
329 fvalue = va_arg(args, double);
332 outch(buffer, &currlen, maxlen, va_arg(args, int));
335 strvalue = va_arg(args, char *);
337 max = maxlen; /* ie, no max */
339 fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max, outch);
342 flags |= DP_F_UNSIGNED;
343 if (sizeof(char *) == 4) {
344 value = va_arg(args, uint32_t);
345 } else if (sizeof(char *) == 8) {
346 value = va_arg(args, uint64_t);
348 value = 0; /* we have a problem */
350 fmtint(buffer, &currlen, maxlen, value, 16, min, max,
354 if (cflags == DP_C_SHORT)
357 num = va_arg(args, short int *);
360 else if (cflags == DP_C_LONG)
363 num = va_arg(args, long int *);
366 else if (cflags == DP_C_LONG_LONG)
369 num = va_arg(args, INT64 *);
375 num = va_arg(args, int *);
380 outch(buffer, &currlen, maxlen, ch);
382 case 'w': /* not supported yet, treat as next char */
385 default: /* Unknown, skip */
389 state = DP_S_DEFAULT;
390 flags = cflags = min = 0;
396 break; /* some picky compilers need this */
399 outch(buffer, &currlen, maxlen, -1);
404 fmtstr(char *buffer, size_t *currlen, size_t maxlen,
405 char *value, int flags, int min, int max, prfun outch)
407 int padlen, strln; /* amount to pad */
411 value = (char *)"<NULL>";
413 for (strln = 0; value[strln]; ++strln); /* strlen */
414 padlen = min - strln;
417 if (flags & DP_F_MINUS)
418 padlen = -padlen; /* Left Justify */
420 while ((padlen > 0) && (cnt < max))
422 outch(buffer, currlen, maxlen, ' ');
426 while (*value && (cnt < max))
428 outch(buffer, currlen, maxlen, *value++);
431 while ((padlen < 0) && (cnt < max))
433 outch(buffer, currlen, maxlen, ' ');
439 /* Have to handle DP_F_NUM(ie 0x and 0 alternates) */
442 fmtint(char *buffer, size_t *currlen, size_t maxlen,
443 INT64 value, int base, int min, int max, int flags, prfun outch)
449 int spadlen = 0; /* amount to space pad */
450 int zpadlen = 0; /* amount to zero pad */
458 if (!(flags & DP_F_UNSIGNED))
465 else if (flags & DP_F_PLUS) /* Do a sign(+/i) */
467 else if (flags & DP_F_SPACE)
472 caps = 16; /* Should characters be upper case? */
476 convert[place++] = digitval[(uvalue%base)+caps];
477 uvalue = (uvalue / (unsigned)base);
478 } while (uvalue && (place < 20));
485 zpadlen = max - place;
486 spadlen = min - MAX(max, place) - (signvalue ? 1 : 0);
492 if (flags & DP_F_ZERO)
494 zpadlen = MAX(zpadlen, spadlen);
497 if (flags & DP_F_MINUS)
498 spadlen = -spadlen; /* Left Justifty */
504 outch(buffer, currlen, maxlen, ' ');
510 outch(buffer, currlen, maxlen, signvalue);
517 outch(buffer, currlen, maxlen, '0');
524 outch(buffer, currlen, maxlen, convert[--place]);
526 /* Left Justified spaces */
529 outch(buffer, currlen, maxlen, ' ');
537 long double result = 1;
549 round(long double value)
551 long intpart = (long)value;
561 fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
562 int min, int max, int flags, prfun outch)
569 int padlen = 0; /* amount to pad */
576 * AIX manpage says the default is 0, but Solaris says the default
577 * is 6, and sprintf on AIX defaults to 6
582 ufvalue = abs_val(fvalue);
586 else if (flags & DP_F_PLUS) /* Do a sign(+/i) */
588 else if (flags & DP_F_SPACE)
591 intpart = (long)ufvalue;
594 * Sorry, we only support 9 digits past the decimal because of our
600 /* We "cheat" by converting the fractional part to integer by
601 * multiplying by a factor of 10
603 fracpart = round((pow10 (max)) * (ufvalue - intpart));
605 if (fracpart >= pow10 (max))
608 fracpart -= (long)pow10 (max);
611 /* Convert integer part */
614 iconvert[iplace++] = digitval[intpart % 10];
615 intpart = (intpart / 10);
616 } while (intpart && (iplace < 20));
621 iconvert[iplace] = 0;
623 /* Convert fractional part */
626 fconvert[fplace++] = digitval[fracpart % 10];
627 fracpart = (fracpart / 10);
628 } while (fracpart && (fplace < 20));
633 fconvert[fplace] = 0;
635 /* -1 for decimal point, another -1 if we are printing a sign */
636 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
637 zpadlen = max - fplace;
642 if (flags & DP_F_MINUS)
643 padlen = -padlen; /* Left Justifty */
645 if ((flags & DP_F_ZERO) && (padlen > 0))
649 outch(buffer, currlen, maxlen, signvalue);
655 outch(buffer, currlen, maxlen, '0');
662 outch(buffer, currlen, maxlen, ' ');
667 outch(buffer, currlen, maxlen, signvalue);
670 outch(buffer, currlen, maxlen, iconvert[--iplace]);
673 * Decimal point. This should probably use locale to find the correct
676 outch(buffer, currlen, maxlen, '.');
679 outch(buffer, currlen, maxlen, fconvert[--fplace]);
683 outch(buffer, currlen, maxlen, '0');
689 outch(buffer, currlen, maxlen, ' ');
695 dopr_outch(char *buffer, size_t *currlen, size_t maxlen, int c)
699 if (*currlen < maxlen - 1)
700 buffer[*currlen] = '\0';
702 buffer[maxlen - 1] = '\0';
704 else if (*currlen < maxlen)
705 buffer[(*currlen)++] = c;
709 __sprintf(char *str, const char *fmt, ...)
714 rval = vsnprintf(str, 128*1024, fmt, ap);
721 __snprintf(char *str, size_t count, const char *fmt, ...)
727 rval = vsnprintf(str, count, fmt, ap);
734 __vsprintf(char *s, const char *format, va_list args)
737 return dopr(s, 0xfffff, format, args, dopr_outch);
741 __vsnprintf(char *s, size_t count, const char *format, va_list args)
744 return dopr(s, count, format, args, dopr_outch);