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 **************************************************************/
54 typedef void (prfun)(char *, size_t *, size_t, int);
57 dopr(char *buffer, size_t maxlen, const char *format, va_list args, prfun);
61 fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags,
62 int min, int max, prfun);
65 fmtint(char *buffer, size_t *currlen, size_t maxlen, INT64 value, int base,
66 int min, int max, int flags, prfun);
69 fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
70 int min, int max, int flags, prfun);
73 dopr_outch(char *buffer, size_t *currlen, size_t maxlen, int c);
76 * dopr(): poor man's version of doprintf
79 #define MAX(a,b) ((a)>(b)?(a):(b))
81 /* format read states */
82 #define DP_S_DEFAULT 0
91 /* format flags - Bits */
92 #define DP_F_MINUS (1 << 0)
93 #define DP_F_PLUS (1 << 1)
94 #define DP_F_SPACE (1 << 2)
95 #define DP_F_NUM (1 << 3)
96 #define DP_F_ZERO (1 << 4)
97 #define DP_F_UP (1 << 5)
98 #define DP_F_UNSIGNED (1 << 6)
100 /* Conversion Flags */
103 #define DP_C_LDOUBLE 3
104 #define DP_C_LONG_LONG 4
106 #define char_to_int(p) (p - '0')
107 #define abs_val(p) (p < 0 ? -p : p)
109 static const char digitval[] = "0123456789abcdef0123456789ABCDEF";
112 dopr(char *buffer, size_t maxlen, const char *format, va_list args, prfun outch)
120 int state = DP_S_DEFAULT;
127 while (state != DP_S_DONE)
129 if ((ch == '\0') || (currlen >= maxlen))
138 outch(buffer, &currlen, maxlen, ch);
170 if (isdigit((unsigned char)ch))
172 min = 10*min + char_to_int(ch);
177 min = va_arg(args, int);
194 if (isdigit((unsigned char)ch))
198 max = 10*max + char_to_int(ch);
203 max = va_arg(args, int);
222 cflags = DP_C_LONG_LONG;
227 cflags = DP_C_LONG_LONG;
231 cflags = DP_C_LDOUBLE;
243 flags |= DP_F_UNSIGNED;
244 if (cflags == DP_C_SHORT)
245 value = va_arg(args, unsigned int);
246 else if (cflags == DP_C_LONG)
247 value = va_arg(args, unsigned long int);
248 else if (cflags == DP_C_LONG_LONG)
249 value = va_arg(args, UINT64);
251 value = va_arg(args, unsigned int);
252 fmtint(buffer, &currlen, maxlen, value, 2, min, max, flags, outch);
256 if (cflags == DP_C_SHORT)
257 value = va_arg(args, int);
258 else if (cflags == DP_C_LONG)
259 value = va_arg(args, long int);
260 else if (cflags == DP_C_LONG_LONG)
261 value = va_arg(args, INT64);
263 value = va_arg(args, int);
264 fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags, outch);
267 flags |= DP_F_UNSIGNED;
268 if (cflags == DP_C_SHORT)
269 value = va_arg(args, unsigned int);
270 else if (cflags == DP_C_LONG)
271 value = va_arg(args, unsigned long int);
272 else if (cflags == DP_C_LONG_LONG)
273 value = va_arg(args, UINT64);
275 value = va_arg(args, unsigned int);
276 fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags, outch);
279 flags |= DP_F_UNSIGNED;
280 if (cflags == DP_C_SHORT)
281 value = va_arg(args, unsigned int);
282 else if (cflags == DP_C_LONG)
283 value = va_arg(args, unsigned long int);
284 else if (cflags == DP_C_LONG_LONG)
285 value = va_arg(args, UINT64);
287 value = va_arg(args, unsigned int);
288 fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags, outch);
293 flags |= DP_F_UNSIGNED;
294 if (cflags == DP_C_SHORT)
295 value = va_arg(args, unsigned int);
296 else if (cflags == DP_C_LONG)
297 value = va_arg(args, unsigned long int);
298 else if (cflags == DP_C_LONG_LONG)
299 value = va_arg(args, UINT64);
301 value = va_arg(args, unsigned int);
302 fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags, outch);
305 if (cflags == DP_C_LDOUBLE)
306 fvalue = va_arg(args, long double);
308 fvalue = va_arg(args, double);
309 /* um, floating point? */
310 fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags, outch);
315 if (cflags == DP_C_LDOUBLE)
316 fvalue = va_arg(args, long double);
318 fvalue = va_arg(args, double);
323 if (cflags == DP_C_LDOUBLE)
324 fvalue = va_arg(args, long double);
326 fvalue = va_arg(args, double);
329 outch(buffer, &currlen, maxlen, va_arg(args, int));
332 strvalue = va_arg(args, char *);
334 max = maxlen; /* ie, no max */
335 fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max, outch);
338 strvalue = (char *) va_arg(args, void *);
339 flags |= DP_F_UNSIGNED;
340 fmtint(buffer, &currlen, maxlen, (long) strvalue, 16, min, max,
344 if (cflags == DP_C_SHORT)
347 num = va_arg(args, short int *);
350 else if (cflags == DP_C_LONG)
353 num = va_arg(args, long int *);
356 else if (cflags == DP_C_LONG_LONG)
359 num = va_arg(args, INT64 *);
365 num = va_arg(args, int *);
370 outch(buffer, &currlen, maxlen, ch);
372 case 'w': /* not supported yet, treat as next char */
375 default: /* Unknown, skip */
379 state = DP_S_DEFAULT;
380 flags = cflags = min = 0;
386 break; /* some picky compilers need this */
389 outch(buffer, &currlen, maxlen, -1);
394 fmtstr(char *buffer, size_t *currlen, size_t maxlen,
395 char *value, int flags, int min, int max, prfun outch)
397 int padlen, strln; /* amount to pad */
403 for (strln = 0; value[strln]; ++strln); /* strlen */
404 padlen = min - strln;
407 if (flags & DP_F_MINUS)
408 padlen = -padlen; /* Left Justify */
410 while ((padlen > 0) && (cnt < max))
412 outch(buffer, currlen, maxlen, ' ');
416 while (*value && (cnt < max))
418 outch(buffer, currlen, maxlen, *value++);
421 while ((padlen < 0) && (cnt < max))
423 outch(buffer, currlen, maxlen, ' ');
429 /* Have to handle DP_F_NUM(ie 0x and 0 alternates) */
432 fmtint(char *buffer, size_t *currlen, size_t maxlen,
433 INT64 value, int base, int min, int max, int flags, prfun outch)
439 int spadlen = 0; /* amount to space pad */
440 int zpadlen = 0; /* amount to zero pad */
448 if (!(flags & DP_F_UNSIGNED))
455 else if (flags & DP_F_PLUS) /* Do a sign(+/i) */
457 else if (flags & DP_F_SPACE)
462 caps = 16; /* Should characters be upper case? */
466 convert[place++] = digitval[(uvalue%base)+caps];
467 uvalue = (uvalue / (unsigned)base);
468 } while (uvalue && (place < 20));
475 zpadlen = max - place;
476 spadlen = min - MAX(max, place) - (signvalue ? 1 : 0);
482 if (flags & DP_F_ZERO)
484 zpadlen = MAX(zpadlen, spadlen);
487 if (flags & DP_F_MINUS)
488 spadlen = -spadlen; /* Left Justifty */
494 outch(buffer, currlen, maxlen, ' ');
500 outch(buffer, currlen, maxlen, signvalue);
507 outch(buffer, currlen, maxlen, '0');
514 outch(buffer, currlen, maxlen, convert[--place]);
516 /* Left Justified spaces */
519 outch(buffer, currlen, maxlen, ' ');
527 long double result = 1;
539 round(long double value)
541 long intpart = (long)value;
551 fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
552 int min, int max, int flags, prfun outch)
559 int padlen = 0; /* amount to pad */
566 * AIX manpage says the default is 0, but Solaris says the default
567 * is 6, and sprintf on AIX defaults to 6
572 ufvalue = abs_val(fvalue);
576 else if (flags & DP_F_PLUS) /* Do a sign(+/i) */
578 else if (flags & DP_F_SPACE)
581 intpart = (long)ufvalue;
584 * Sorry, we only support 9 digits past the decimal because of our
590 /* We "cheat" by converting the fractional part to integer by
591 * multiplying by a factor of 10
593 fracpart = round((pow10 (max)) * (ufvalue - intpart));
595 if (fracpart >= pow10 (max))
598 fracpart -= (long)pow10 (max);
601 /* Convert integer part */
604 iconvert[iplace++] = digitval[intpart % 10];
605 intpart = (intpart / 10);
606 } while (intpart && (iplace < 20));
611 iconvert[iplace] = 0;
613 /* Convert fractional part */
616 fconvert[fplace++] = digitval[fracpart % 10];
617 fracpart = (fracpart / 10);
618 } while (fracpart && (fplace < 20));
623 fconvert[fplace] = 0;
625 /* -1 for decimal point, another -1 if we are printing a sign */
626 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
627 zpadlen = max - fplace;
632 if (flags & DP_F_MINUS)
633 padlen = -padlen; /* Left Justifty */
635 if ((flags & DP_F_ZERO) && (padlen > 0))
639 outch(buffer, currlen, maxlen, signvalue);
645 outch(buffer, currlen, maxlen, '0');
652 outch(buffer, currlen, maxlen, ' ');
657 outch(buffer, currlen, maxlen, signvalue);
660 outch(buffer, currlen, maxlen, iconvert[--iplace]);
663 * Decimal point. This should probably use locale to find the correct
666 outch(buffer, currlen, maxlen, '.');
669 outch(buffer, currlen, maxlen, fconvert[--fplace]);
673 outch(buffer, currlen, maxlen, '0');
679 outch(buffer, currlen, maxlen, ' ');
685 dopr_outch(char *buffer, size_t *currlen, size_t maxlen, int c)
689 if (*currlen < maxlen - 1)
690 buffer[*currlen] = '\0';
692 buffer[maxlen - 1] = '\0';
694 else if (*currlen < maxlen)
695 buffer[(*currlen)++] = c;
699 __sprintf(char *str, const char *fmt, ...)
704 rval = vsnprintf(str, 128*1024, fmt, ap);
711 __snprintf(char *str, size_t count, const char *fmt, ...)
717 rval = vsnprintf(str, count, fmt, ap);
724 __vsprintf(char *s, const char *format, va_list args)
727 return dopr(s, 0xfffff, format, args, dopr_outch);
731 __vsnprintf(char *s, size_t count, const char *format, va_list args)
734 return dopr(s, count, format, args, dopr_outch);