1 /* This is taken from cvslock. The same code is used in several
2 * freeware and GPLd applications. I contacted the email addresses
3 * listed in the header and have been told that the code is probably
4 * public domain, but I am still seeking confirmation from Patrick
5 * Powell. The original code was posted to BugTraq by Patrick in 1995
6 * but without any notice as to the copyright or license status.
8 * October 1998, Andrew Tridgell (tridge@samba.anu.edu.au)
11 /**************************************************************
13 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
14 * A bombproof version of doprnt (dopr) included.
15 * Sigh. This sort of thing is always nasty do deal with. Note that
16 * the version here does not include floating point...
18 * snprintf() is used instead of sprintf() as it does limit checks
19 * for string length. This covers a nasty loophole.
21 * The other functions are there to prevent NULL pointers from
22 * causing nast effects.
25 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
26 * This was ugly. It is still ugly. I opted out of floating point
27 * numbers, but the formatter understands just about everything
28 * from the normal C string format, at least as far as I can tell from
29 * the Solaris 2.5 printf(3S) man page.
31 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
32 * Ok, added some minimal floating point support, which means this
33 * probably requires libm on most operating systems. Don't yet
34 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
35 * was pretty badly broken, it just wasn't being exercised in ways
36 * which showed it, so that's been fixed. Also, formated the code
37 * to mutt conventions, and removed dead code left over from the
38 * original. Also, there is now a builtin-test, just compile with:
39 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
40 * and run snprintf for results.
42 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
43 * The PGP code was using unsigned hexadecimal formats.
44 * Unfortunately, unsigned formats simply didn't work.
46 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
47 * The original code assumed that both snprintf() and vsnprintf() were
48 * missing. Some systems only have snprintf() but not vsnprintf(), so
49 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
51 **************************************************************/
55 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
59 #include <sys/types.h>
61 /* Define this as a fall through, HAVE_STDARG_H is probably already set */
63 #define HAVE_VARARGS_H
65 /* varargs declarations: */
67 #if defined(HAVE_STDARG_H)
69 # define HAVE_STDARGS /* let's hope that works everywhere (mj) */
70 # define VA_LOCAL_DECL va_list ap
71 # define VA_START(f) va_start(ap, f)
72 # define VA_SHIFT(v,t) ; /* no-op for ANSI */
73 # define VA_END va_end(ap)
75 # if defined(HAVE_VARARGS_H)
78 # define VA_LOCAL_DECL va_list ap
79 # define VA_START(f) va_start(ap) /* f is ignored! */
80 # define VA_SHIFT(v,t) v = va_arg(ap,t)
81 # define VA_END va_end(ap)
83 /*XX ** NO VARARGS ** XX*/
87 /*int snprintf (char *str, size_t count, const char *fmt, ...);*/
88 /*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/
90 static void dopr (char *buffer, size_t maxlen, const char *format,
92 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
93 char *value, int flags, int min, int max);
94 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
95 long value, int base, int min, int max, int flags);
96 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
97 long double fvalue, int min, int max, int flags);
98 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
101 * dopr(): poor man's version of doprintf
104 /* format read states */
105 #define DP_S_DEFAULT 0
114 /* format flags - Bits */
115 #define DP_F_MINUS (1 << 0)
116 #define DP_F_PLUS (1 << 1)
117 #define DP_F_SPACE (1 << 2)
118 #define DP_F_NUM (1 << 3)
119 #define DP_F_ZERO (1 << 4)
120 #define DP_F_UP (1 << 5)
121 #define DP_F_UNSIGNED (1 << 6)
123 /* Conversion Flags */
126 #define DP_C_LDOUBLE 3
128 #define char_to_int(p) (p - '0')
129 #define MAX(p,q) ((p >= q) ? p : q)
131 static void dopr (char *buffer, size_t maxlen, const char *format, va_list args)
144 state = DP_S_DEFAULT;
145 currlen = flags = cflags = min = 0;
149 while (state != DP_S_DONE)
151 if ((ch == '\0') || (currlen >= maxlen))
160 dopr_outch (buffer, &currlen, maxlen, ch);
192 if (isdigit((unsigned char)ch))
194 min = 10*min + char_to_int (ch);
199 min = va_arg (args, int);
216 if (isdigit((unsigned char)ch))
220 max = 10*max + char_to_int (ch);
225 max = va_arg (args, int);
233 /* Currently, we don't support Long Long, bummer */
245 cflags = DP_C_LDOUBLE;
258 if (cflags == DP_C_SHORT)
259 value = va_arg (args, short int);
260 else if (cflags == DP_C_LONG)
261 value = va_arg (args, long int);
263 value = va_arg (args, int);
264 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
267 flags |= DP_F_UNSIGNED;
268 if (cflags == DP_C_SHORT)
269 value = va_arg (args, unsigned short int);
270 else if (cflags == DP_C_LONG)
271 value = va_arg (args, unsigned long int);
273 value = va_arg (args, unsigned int);
274 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
277 flags |= DP_F_UNSIGNED;
278 if (cflags == DP_C_SHORT)
279 value = va_arg (args, unsigned short int);
280 else if (cflags == DP_C_LONG)
281 value = va_arg (args, unsigned long int);
283 value = va_arg (args, unsigned int);
284 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
289 flags |= DP_F_UNSIGNED;
290 if (cflags == DP_C_SHORT)
291 value = va_arg (args, unsigned short int);
292 else if (cflags == DP_C_LONG)
293 value = va_arg (args, unsigned long int);
295 value = va_arg (args, unsigned int);
296 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
299 if (cflags == DP_C_LDOUBLE)
300 fvalue = va_arg (args, long double);
302 fvalue = va_arg (args, double);
303 /* um, floating point? */
304 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
309 if (cflags == DP_C_LDOUBLE)
310 fvalue = va_arg (args, long double);
312 fvalue = va_arg (args, double);
317 if (cflags == DP_C_LDOUBLE)
318 fvalue = va_arg (args, long double);
320 fvalue = va_arg (args, double);
323 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
326 strvalue = va_arg (args, char *);
328 max = maxlen; /* ie, no max */
329 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
332 strvalue = va_arg (args, void *);
333 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
336 if (cflags == DP_C_SHORT)
339 num = va_arg (args, short int *);
342 else if (cflags == DP_C_LONG)
345 num = va_arg (args, long int *);
351 num = va_arg (args, int *);
356 dopr_outch (buffer, &currlen, maxlen, ch);
359 /* not supported yet, treat as next char */
367 state = DP_S_DEFAULT;
368 flags = cflags = min = 0;
375 break; /* some picky compilers need this */
378 if (currlen < maxlen - 1)
379 buffer[currlen] = '\0';
381 buffer[maxlen - 1] = '\0';
384 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
385 char *value, int flags, int min, int max)
387 int padlen, strln; /* amount to pad */
395 for (strln = 0; value[strln]; ++strln); /* strlen */
396 padlen = min - strln;
399 if (flags & DP_F_MINUS)
400 padlen = -padlen; /* Left Justify */
402 while ((padlen > 0) && (cnt < max))
404 dopr_outch (buffer, currlen, maxlen, ' ');
408 while (*value && (cnt < max))
410 dopr_outch (buffer, currlen, maxlen, *value++);
413 while ((padlen < 0) && (cnt < max))
415 dopr_outch (buffer, currlen, maxlen, ' ');
421 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
423 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
424 long value, int base, int min, int max, int flags)
427 unsigned long uvalue;
430 int spadlen = 0; /* amount to space pad */
431 int zpadlen = 0; /* amount to zero pad */
439 if(!(flags & DP_F_UNSIGNED))
446 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
449 if (flags & DP_F_SPACE)
453 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
457 (caps? "0123456789ABCDEF":"0123456789abcdef")
458 [uvalue % (unsigned)base ];
459 uvalue = (uvalue / (unsigned)base );
460 } while(uvalue && (place < 20));
461 if (place == 20) place--;
464 zpadlen = max - place;
465 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
466 if (zpadlen < 0) zpadlen = 0;
467 if (spadlen < 0) spadlen = 0;
468 if (flags & DP_F_ZERO)
470 zpadlen = MAX(zpadlen, spadlen);
473 if (flags & DP_F_MINUS)
474 spadlen = -spadlen; /* Left Justifty */
476 #ifdef DEBUG_SNPRINTF
477 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
478 zpadlen, spadlen, min, max, place));
484 dopr_outch (buffer, currlen, maxlen, ' ');
490 dopr_outch (buffer, currlen, maxlen, signvalue);
497 dopr_outch (buffer, currlen, maxlen, '0');
504 dopr_outch (buffer, currlen, maxlen, convert[--place]);
506 /* Left Justified spaces */
507 while (spadlen < 0) {
508 dopr_outch (buffer, currlen, maxlen, ' ');
513 static long double abs_val (long double value)
515 long double result = value;
523 static long double pow10 (int exp)
525 long double result = 1;
536 static long round (long double value)
541 value = value - intpart;
548 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
549 long double fvalue, int min, int max, int flags)
557 int padlen = 0; /* amount to pad */
564 * AIX manpage says the default is 0, but Solaris says the default
565 * is 6, and sprintf on AIX defaults to 6
570 ufvalue = abs_val (fvalue);
575 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
578 if (flags & DP_F_SPACE)
582 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
588 * Sorry, we only support 9 digits past the decimal because of our
594 /* We "cheat" by converting the fractional part to integer by
595 * multiplying by a factor of 10
597 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
599 if (fracpart >= pow10 (max))
602 fracpart -= pow10 (max);
605 #ifdef DEBUG_SNPRINTF
606 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
609 /* Convert integer part */
612 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
613 intpart = (intpart / 10);
614 } while(intpart && (iplace < 20));
615 if (iplace == 20) iplace--;
616 iconvert[iplace] = 0;
618 /* Convert fractional part */
621 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
622 fracpart = (fracpart / 10);
623 } while(fracpart && (fplace < 20));
624 if (fplace == 20) fplace--;
625 fconvert[fplace] = 0;
627 /* -1 for decimal point, another -1 if we are printing a sign */
628 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
629 zpadlen = max - fplace;
634 if (flags & DP_F_MINUS)
635 padlen = -padlen; /* Left Justifty */
637 if ((flags & DP_F_ZERO) && (padlen > 0))
641 dopr_outch (buffer, currlen, maxlen, signvalue);
647 dopr_outch (buffer, currlen, maxlen, '0');
653 dopr_outch (buffer, currlen, maxlen, ' ');
657 dopr_outch (buffer, currlen, maxlen, signvalue);
660 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
663 * Decimal point. This should probably use locale to find the correct
666 dopr_outch (buffer, currlen, maxlen, '.');
669 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
673 dopr_outch (buffer, currlen, maxlen, '0');
679 dopr_outch (buffer, currlen, maxlen, ' ');
684 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
686 if (*currlen < maxlen)
687 buffer[(*currlen)++] = c;
689 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
691 #ifndef HAVE_VSNPRINTF
692 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
695 dopr(str, count, fmt, args);
698 #endif /* !HAVE_VSNPRINTF */
700 #ifndef HAVE_SNPRINTF
703 int snprintf (char *str,size_t count,const char *fmt,...)
705 int snprintf (va_alist) va_dcl
716 VA_SHIFT (str, char *);
717 VA_SHIFT (count, size_t );
718 VA_SHIFT (fmt, char *);
719 (void) vsnprintf(str, count, fmt, ap);
726 #define LONG_STRING 1024
730 char buf1[LONG_STRING];
731 char buf2[LONG_STRING];
746 double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
747 0.9996, 1.996, 4.136, 0};
760 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
765 printf ("Testing snprintf format codes against system sprintf...\n");
767 for (x = 0; fp_fmt[x] != NULL ; x++)
768 for (y = 0; fp_nums[y] != 0 ; y++)
770 snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
771 sprintf (buf2, fp_fmt[x], fp_nums[y]);
772 if (strcmp (buf1, buf2))
774 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
775 fp_fmt[x], buf1, buf2);
781 for (x = 0; int_fmt[x] != NULL ; x++)
782 for (y = 0; int_nums[y] != 0 ; y++)
784 snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
785 sprintf (buf2, int_fmt[x], int_nums[y]);
786 if (strcmp (buf1, buf2))
788 printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n",
789 int_fmt[x], buf1, buf2);
794 printf ("%d tests failed out of %d.\n", fail, num);
796 #endif /* SNPRINTF_TEST */
799 /* keep compilers happy about empty files */
800 void dummy_snprintf(void) {}
801 #endif /* !HAVE_SNPRINTF */