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 **************************************************************/
56 #if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */
58 # undef HAVE_VSNPRINTF
61 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
64 dopr(char *buffer, size_t maxlen, const char *format, va_list args);
67 fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags,
71 fmtint(char *buffer, size_t *currlen, size_t maxlen, long value, int base,
72 int min, int max, int flags);
75 fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
76 int min, int max, int flags);
79 dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
82 * dopr(): poor man's version of doprintf
85 /* format read states */
86 #define DP_S_DEFAULT 0
95 /* format flags - Bits */
96 #define DP_F_MINUS (1 << 0)
97 #define DP_F_PLUS (1 << 1)
98 #define DP_F_SPACE (1 << 2)
99 #define DP_F_NUM (1 << 3)
100 #define DP_F_ZERO (1 << 4)
101 #define DP_F_UP (1 << 5)
102 #define DP_F_UNSIGNED (1 << 6)
104 /* Conversion Flags */
107 #define DP_C_LDOUBLE 3
108 #define DP_C_LONG_LONG 4
110 #define char_to_int(p) (p - '0')
111 #define abs_val(p) (p < 0 ? -p : p)
115 dopr(char *buffer, size_t maxlen, const char *format, va_list args)
123 int state = DP_S_DEFAULT;
130 while (state != DP_S_DONE) {
131 if ((ch == '\0') || (currlen >= maxlen))
139 dopr_outch(buffer, &currlen, maxlen, ch);
170 if (isdigit((unsigned char)ch)) {
171 min = 10*min + char_to_int (ch);
173 } else if (ch == '*') {
174 min = va_arg (args, int);
188 if (isdigit((unsigned char)ch)) {
191 max = 10*max + char_to_int(ch);
193 } else if (ch == '*') {
194 max = va_arg (args, int);
210 cflags = DP_C_LONG_LONG;
215 cflags = DP_C_LONG_LONG;
219 cflags = DP_C_LDOUBLE;
231 if (cflags == DP_C_SHORT)
232 value = va_arg(args, int);
233 else if (cflags == DP_C_LONG)
234 value = va_arg(args, long int);
235 else if (cflags == DP_C_LONG_LONG)
236 value = va_arg (args, long long);
238 value = va_arg (args, int);
239 fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
242 flags |= DP_F_UNSIGNED;
243 if (cflags == DP_C_SHORT)
244 value = va_arg(args, unsigned int);
245 else if (cflags == DP_C_LONG)
246 value = va_arg(args, unsigned long int);
247 else if (cflags == DP_C_LONG_LONG)
248 value = va_arg(args, unsigned long long);
250 value = va_arg(args, unsigned int);
251 fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags);
254 flags |= DP_F_UNSIGNED;
255 if (cflags == DP_C_SHORT)
256 value = va_arg(args, unsigned int);
257 else if (cflags == DP_C_LONG)
258 value = va_arg(args, unsigned long int);
259 else if (cflags == DP_C_LONG_LONG)
260 value = va_arg(args, unsigned long long);
262 value = va_arg(args, unsigned int);
263 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
268 flags |= DP_F_UNSIGNED;
269 if (cflags == DP_C_SHORT)
270 value = va_arg(args, unsigned int);
271 else if (cflags == DP_C_LONG)
272 value = va_arg(args, unsigned long int);
273 else if (cflags == DP_C_LONG_LONG)
274 value = va_arg(args, unsigned long long);
276 value = va_arg(args, unsigned int);
277 fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags);
280 if (cflags == DP_C_LDOUBLE)
281 fvalue = va_arg(args, long double);
283 fvalue = va_arg(args, double);
284 /* um, floating point? */
285 fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags);
290 if (cflags == DP_C_LDOUBLE)
291 fvalue = va_arg(args, long double);
293 fvalue = va_arg(args, double);
298 if (cflags == DP_C_LDOUBLE)
299 fvalue = va_arg(args, long double);
301 fvalue = va_arg(args, double);
304 dopr_outch(buffer, &currlen, maxlen, va_arg(args, int));
307 strvalue = va_arg(args, char *);
309 max = maxlen; /* ie, no max */
310 fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max);
313 strvalue = va_arg(args, void *);
314 fmtint(buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
317 if (cflags == DP_C_SHORT) {
319 num = va_arg(args, short int *);
321 } else if (cflags == DP_C_LONG) {
323 num = va_arg(args, long int *);
325 } else if (cflags == DP_C_LONG_LONG) {
327 num = va_arg(args, long long *);
331 num = va_arg(args, int *);
336 dopr_outch(buffer, &currlen, maxlen, ch);
338 case 'w': /* not supported yet, treat as next char */
341 default: /* Unknown, skip */
345 state = DP_S_DEFAULT;
346 flags = cflags = min = 0;
352 break; /* some picky compilers need this */
355 if (currlen < maxlen - 1)
356 buffer[currlen] = '\0';
358 buffer[maxlen - 1] = '\0';
362 fmtstr(char *buffer, size_t *currlen, size_t maxlen,
363 char *value, int flags, int min, int max)
365 int padlen, strln; /* amount to pad */
371 for (strln = 0; value[strln]; ++strln); /* strlen */
372 padlen = min - strln;
375 if (flags & DP_F_MINUS)
376 padlen = -padlen; /* Left Justify */
378 while ((padlen > 0) && (cnt < max)) {
379 dopr_outch(buffer, currlen, maxlen, ' ');
383 while (*value && (cnt < max)) {
384 dopr_outch(buffer, currlen, maxlen, *value++);
387 while ((padlen < 0) && (cnt < max)) {
388 dopr_outch(buffer, currlen, maxlen, ' ');
394 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
397 fmtint(char *buffer, size_t *currlen, size_t maxlen,
398 long value, int base, int min, int max, int flags)
400 unsigned long uvalue;
404 int spadlen = 0; /* amount to space pad */
405 int zpadlen = 0; /* amount to zero pad */
413 if (!(flags & DP_F_UNSIGNED)) {
417 } else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
419 else if (flags & DP_F_SPACE)
424 caps = 1; /* Should characters be upper case? */
428 (caps? "0123456789ABCDEF":"0123456789abcdef")
429 [uvalue % (unsigned)base];
430 uvalue = (uvalue / (unsigned)base );
431 } while (uvalue && (place < 20));
436 zpadlen = max - place;
437 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
442 if (flags & DP_F_ZERO) {
443 zpadlen = MAX(zpadlen, spadlen);
446 if (flags & DP_F_MINUS)
447 spadlen = -spadlen; /* Left Justifty */
451 while (spadlen > 0) {
452 dopr_outch(buffer, currlen, maxlen, ' ');
458 dopr_outch(buffer, currlen, maxlen, signvalue);
462 while (zpadlen > 0) {
463 dopr_outch(buffer, currlen, maxlen, '0');
470 dopr_outch(buffer, currlen, maxlen, convert[--place]);
472 /* Left Justified spaces */
473 while (spadlen < 0) {
474 dopr_outch (buffer, currlen, maxlen, ' ');
482 long double result = 1;
493 round(long double value)
495 long intpart = value;
505 fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
506 int min, int max, int flags)
513 int padlen = 0; /* amount to pad */
521 * AIX manpage says the default is 0, but Solaris says the default
522 * is 6, and sprintf on AIX defaults to 6
527 ufvalue = abs_val(fvalue);
531 else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
533 else if (flags & DP_F_SPACE)
539 * Sorry, we only support 9 digits past the decimal because of our
545 /* We "cheat" by converting the fractional part to integer by
546 * multiplying by a factor of 10
548 fracpart = round((pow10 (max)) * (ufvalue - intpart));
550 if (fracpart >= pow10 (max)) {
552 fracpart -= pow10 (max);
555 /* Convert integer part */
558 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
559 intpart = (intpart / 10);
560 } while(intpart && (iplace < 20));
563 iconvert[iplace] = 0;
565 /* Convert fractional part */
568 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
569 fracpart = (fracpart / 10);
570 } while(fracpart && (fplace < 20));
573 fconvert[fplace] = 0;
575 /* -1 for decimal point, another -1 if we are printing a sign */
576 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
577 zpadlen = max - fplace;
582 if (flags & DP_F_MINUS)
583 padlen = -padlen; /* Left Justifty */
585 if ((flags & DP_F_ZERO) && (padlen > 0)) {
587 dopr_outch(buffer, currlen, maxlen, signvalue);
592 dopr_outch(buffer, currlen, maxlen, '0');
597 dopr_outch(buffer, currlen, maxlen, ' ');
601 dopr_outch(buffer, currlen, maxlen, signvalue);
604 dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]);
607 * Decimal point. This should probably use locale to find the correct
610 dopr_outch(buffer, currlen, maxlen, '.');
613 dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]);
615 while (zpadlen > 0) {
616 dopr_outch(buffer, currlen, maxlen, '0');
621 dopr_outch(buffer, currlen, maxlen, ' ');
627 dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
629 if (*currlen < maxlen)
630 buffer[(*currlen)++] = c;
632 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
634 #ifndef HAVE_VSNPRINTF
636 vsnprintf(char *str, size_t count, const char *fmt, va_list args)
639 dopr(str, count, fmt, args);
643 #endif /* !HAVE_VSNPRINTF */
645 #ifndef HAVE_SNPRINTF
647 snprintf(char *str,size_t count,const char *fmt,...)
652 (void) vsnprintf(str, count, fmt, ap);
662 #define LONG_STRING 1024
663 char buf1[LONG_STRING];
664 char buf2[LONG_STRING];
706 long long int_nums[] = { -1, 134, 91340, 341, 0203, 0, 9999999 };
711 printf("Testing snprintf format codes against system sprintf...\n");
713 for (x = 0; fp_fmt[x] != NULL ; x++) {
714 for (y = 0; fp_nums[y] != 0 ; y++) {
715 snprintf(buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
716 sprintf (buf2, fp_fmt[x], fp_nums[y]);
717 if (strcmp (buf1, buf2)) {
718 printf("snprintf doesn't match Format: %s\n\t"
719 "snprintf = %s\n\tsprintf = %s\n",
720 fp_fmt[x], buf1, buf2);
726 for (x = 0; int_fmt[x] != NULL ; x++) {
727 for (y = 0; int_nums[y] != 0 ; y++) {
728 snprintf(buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
729 sprintf(buf2, int_fmt[x], int_nums[y]);
730 if (strcmp (buf1, buf2)) {
731 printf("snprintf doesn't match Format: %s\n\t"
732 "snprintf = %s\n\tsprintf = %s\n",
733 int_fmt[x], buf1, buf2);
739 printf("%d tests failed out of %d.\n", fail, num);
742 #endif /* SNPRINTF_TEST */
744 #endif /* !HAVE_SNPRINTF */