2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
11 * snprintf() and vsnprintf() replacement functions
17 static char UNUSED id[] = "$Id: vsnprintf.c,v 1.5 2003/04/21 10:53:38 alex Exp $";
25 * snprintf.c: Copyright Patrick Powell 1995
26 * This code is based on code written by Patrick Powell (papowell@astart.com)
27 * It may be used for any purpose as long as this notice remains intact
28 * on all source code distributions
30 * Original: Patrick Powell Tue Apr 11 09:48:21 PDT 1995
31 * A bombproof version of doprnt (dopr) included.
32 * Sigh. This sort of thing is always nasty do deal with. Note that
33 * the version here does not include floating point...
35 * snprintf() is used instead of sprintf() as it does limit checks
36 * for string length. This covers a nasty loophole.
38 * The other functions are there to prevent NULL pointers from
39 * causing nast effects.
42 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
43 * This was ugly. It is still ugly. I opted out of floating point
44 * numbers, but the formatter understands just about everything
45 * from the normal C string format, at least as far as I can tell from
46 * the Solaris 2.5 printf(3S) man page.
48 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
49 * Ok, added some minimal floating point support, which means this
50 * probably requires libm on most operating systems. Don't yet
51 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
52 * was pretty badly broken, it just wasn't being exercised in ways
53 * which showed it, so that's been fixed. Also, formated the code
54 * to mutt conventions, and removed dead code left over from the
55 * original. Also, there is now a builtin-test, just compile with:
56 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
57 * and run snprintf for results.
59 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
60 * The PGP code was using unsigned hexadecimal formats.
61 * Unfortunately, unsigned formats simply didn't work.
63 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
64 * The original code assumed that both snprintf() and vsnprintf() were
65 * missing. Some systems only have snprintf() but not vsnprintf(), so
66 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
68 * Andrew Tridgell <tridge@samba.org>, October 1998
69 * fixed handling of %.0f
70 * added test for HAVE_LONG_DOUBLE
72 * tridge@samba.org, idra@samba.org, April 2001
73 * got rid of fcvt code (twas buggy and made testing harder)
76 * Alexander Barton, <alex@barton.de>, 2002-05-19
77 * removed [v]asprintf() and C99 tests: not needed by ngIRCd.
90 #include <sys/types.h>
97 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF)
98 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
100 /* make the compiler happy with an empty file */
101 void dummy_snprintf PARAMS(( void ));
102 void dummy_snprintf PARAMS(( void )) { }
105 #ifdef HAVE_LONG_DOUBLE
106 #define LDOUBLE long double
108 #define LDOUBLE double
111 #ifdef HAVE_LONG_LONG
112 #define LLONG long long
117 static size_t dopr(char *buffer, size_t maxlen, const char *format,
119 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
120 char *value, int flags, int min, int max);
121 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
122 long value, int base, int min, int max, int flags);
123 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
124 LDOUBLE fvalue, int min, int max, int flags);
125 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
128 * dopr(): poor man's version of doprintf
131 /* format read states */
132 #define DP_S_DEFAULT 0
141 /* format flags - Bits */
142 #define DP_F_MINUS (1 << 0)
143 #define DP_F_PLUS (1 << 1)
144 #define DP_F_SPACE (1 << 2)
145 #define DP_F_NUM (1 << 3)
146 #define DP_F_ZERO (1 << 4)
147 #define DP_F_UP (1 << 5)
148 #define DP_F_UNSIGNED (1 << 6)
150 /* Conversion Flags */
153 #define DP_C_LDOUBLE 3
156 #define char_to_int(p) ((p)- '0')
158 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
161 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
174 state = DP_S_DEFAULT;
175 currlen = flags = cflags = min = 0;
179 while (state != DP_S_DONE) {
188 dopr_outch (buffer, &currlen, maxlen, ch);
219 if (isdigit((unsigned char)ch)) {
220 min = 10*min + char_to_int (ch);
222 } else if (ch == '*') {
223 min = va_arg (args, int);
239 if (isdigit((unsigned char)ch)) {
242 max = 10*max + char_to_int (ch);
244 } else if (ch == '*') {
245 max = va_arg (args, int);
261 if (ch == 'l') { /* It's a long long */
267 cflags = DP_C_LDOUBLE;
279 if (cflags == DP_C_SHORT)
280 value = va_arg (args, int);
281 else if (cflags == DP_C_LONG)
282 value = va_arg (args, long int);
283 else if (cflags == DP_C_LLONG)
284 value = va_arg (args, LLONG);
286 value = va_arg (args, int);
287 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
290 flags |= DP_F_UNSIGNED;
291 if (cflags == DP_C_SHORT)
292 value = va_arg (args, unsigned int);
293 else if (cflags == DP_C_LONG)
294 value = (long)va_arg (args, unsigned long int);
295 else if (cflags == DP_C_LLONG)
296 value = (long)va_arg (args, unsigned LLONG);
298 value = (long)va_arg (args, unsigned int);
299 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
302 flags |= DP_F_UNSIGNED;
303 if (cflags == DP_C_SHORT)
304 value = va_arg (args, unsigned int);
305 else if (cflags == DP_C_LONG)
306 value = (long)va_arg (args, unsigned long int);
307 else if (cflags == DP_C_LLONG)
308 value = (LLONG)va_arg (args, unsigned LLONG);
310 value = (long)va_arg (args, unsigned int);
311 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
316 flags |= DP_F_UNSIGNED;
317 if (cflags == DP_C_SHORT)
318 value = va_arg (args, unsigned int);
319 else if (cflags == DP_C_LONG)
320 value = (long)va_arg (args, unsigned long int);
321 else if (cflags == DP_C_LLONG)
322 value = (LLONG)va_arg (args, unsigned LLONG);
324 value = (long)va_arg (args, unsigned int);
325 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
328 if (cflags == DP_C_LDOUBLE)
329 fvalue = va_arg (args, LDOUBLE);
331 fvalue = va_arg (args, double);
332 /* um, floating point? */
333 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
338 if (cflags == DP_C_LDOUBLE)
339 fvalue = va_arg (args, LDOUBLE);
341 fvalue = va_arg (args, double);
346 if (cflags == DP_C_LDOUBLE)
347 fvalue = va_arg (args, LDOUBLE);
349 fvalue = va_arg (args, double);
352 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
355 strvalue = va_arg (args, char *);
357 max = strlen(strvalue);
359 if (min > 0 && max >= 0 && min > max) max = min;
360 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
363 strvalue = va_arg (args, void *);
364 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
367 if (cflags == DP_C_SHORT) {
369 num = va_arg (args, short int *);
371 } else if (cflags == DP_C_LONG) {
373 num = va_arg (args, long int *);
374 *num = (long int)currlen;
375 } else if (cflags == DP_C_LLONG) {
377 num = va_arg (args, LLONG *);
378 *num = (LLONG)currlen;
381 num = va_arg (args, int *);
386 dopr_outch (buffer, &currlen, maxlen, ch);
389 /* not supported yet, treat as next char */
397 state = DP_S_DEFAULT;
398 flags = cflags = min = 0;
405 break; /* some picky compilers need this */
409 if (currlen < maxlen - 1)
410 buffer[currlen] = '\0';
412 buffer[maxlen - 1] = '\0';
418 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
419 char *value, int flags, int min, int max)
421 int padlen, strln; /* amount to pad */
424 #ifdef DEBUG_SNPRINTF
425 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
431 for (strln = 0; value[strln]; ++strln); /* strlen */
432 padlen = min - strln;
435 if (flags & DP_F_MINUS)
436 padlen = -padlen; /* Left Justify */
438 while ((padlen > 0) && (cnt < max)) {
439 dopr_outch (buffer, currlen, maxlen, ' ');
443 while (*value && (cnt < max)) {
444 dopr_outch (buffer, currlen, maxlen, *value++);
447 while ((padlen < 0) && (cnt < max)) {
448 dopr_outch (buffer, currlen, maxlen, ' ');
454 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
456 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
457 long value, int base, int min, int max, int flags)
460 unsigned long uvalue;
463 int spadlen = 0; /* amount to space pad */
464 int zpadlen = 0; /* amount to zero pad */
472 if(!(flags & DP_F_UNSIGNED)) {
477 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
479 else if (flags & DP_F_SPACE)
484 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
488 (caps? "0123456789ABCDEF":"0123456789abcdef")
489 [uvalue % (unsigned)base ];
490 uvalue = (uvalue / (unsigned)base );
491 } while(uvalue && (place < 20));
492 if (place == 20) place--;
495 zpadlen = max - place;
496 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
497 if (zpadlen < 0) zpadlen = 0;
498 if (spadlen < 0) spadlen = 0;
499 if (flags & DP_F_ZERO) {
500 zpadlen = MAX(zpadlen, spadlen);
503 if (flags & DP_F_MINUS)
504 spadlen = -spadlen; /* Left Justifty */
506 #ifdef DEBUG_SNPRINTF
507 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
508 zpadlen, spadlen, min, max, place);
512 while (spadlen > 0) {
513 dopr_outch (buffer, currlen, maxlen, ' ');
519 dopr_outch (buffer, currlen, maxlen, signvalue);
523 while (zpadlen > 0) {
524 dopr_outch (buffer, currlen, maxlen, '0');
531 dopr_outch (buffer, currlen, maxlen, convert[--place]);
533 /* Left Justified spaces */
534 while (spadlen < 0) {
535 dopr_outch (buffer, currlen, maxlen, ' ');
540 static LDOUBLE abs_val(LDOUBLE value)
542 LDOUBLE result = value;
550 static LDOUBLE POW10(int exp)
562 static LLONG ROUND(LDOUBLE value)
566 intpart = (LLONG)value;
567 value = value - intpart;
568 if (value >= 0.5) intpart++;
573 /* a replacement for modf that doesn't need the math library. Should
574 be portable, but slow */
575 static double my_modf(double x0, double *iptr)
582 for (i=0;i<100;i++) {
584 if (l <= (x+1) && l >= (x-1)) break;
590 /* yikes! the number is beyond what we can handle. What do we do? */
599 ret = my_modf(x0-l*f, &i2);
609 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
610 LDOUBLE fvalue, int min, int max, int flags)
618 int padlen = 0; /* amount to pad */
627 * AIX manpage says the default is 0, but Solaris says the default
628 * is 6, and sprintf on AIX defaults to 6
633 ufvalue = abs_val (fvalue);
638 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
641 if (flags & DP_F_SPACE)
647 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
651 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
655 * Sorry, we only support 16 digits past the decimal because of our
661 /* We "cheat" by converting the fractional part to integer by
662 * multiplying by a factor of 10
666 my_modf(temp, &intpart);
668 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
670 if (fracpart >= POW10(max)) {
672 fracpart -= POW10(max);
676 /* Convert integer part */
679 my_modf(intpart*0.1, &intpart);
681 index = (int) ((temp -intpart +0.05)* 10.0);
682 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
683 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
685 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
686 } while (intpart && (iplace < 311));
687 if (iplace == 311) iplace--;
688 iconvert[iplace] = 0;
690 /* Convert fractional part */
695 my_modf(fracpart*0.1, &fracpart);
697 index = (int) ((temp -fracpart +0.05)* 10.0);
698 /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
699 /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
701 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
702 } while(fracpart && (fplace < 311));
703 if (fplace == 311) fplace--;
705 fconvert[fplace] = 0;
707 /* -1 for decimal point, another -1 if we are printing a sign */
708 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
709 zpadlen = max - fplace;
710 if (zpadlen < 0) zpadlen = 0;
713 if (flags & DP_F_MINUS)
714 padlen = -padlen; /* Left Justifty */
716 if ((flags & DP_F_ZERO) && (padlen > 0)) {
718 dopr_outch (buffer, currlen, maxlen, signvalue);
723 dopr_outch (buffer, currlen, maxlen, '0');
728 dopr_outch (buffer, currlen, maxlen, ' ');
732 dopr_outch (buffer, currlen, maxlen, signvalue);
735 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
737 #ifdef DEBUG_SNPRINTF
738 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
742 * Decimal point. This should probably use locale to find the correct
746 dopr_outch (buffer, currlen, maxlen, '.');
749 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
752 while (zpadlen > 0) {
753 dopr_outch (buffer, currlen, maxlen, '0');
758 dopr_outch (buffer, currlen, maxlen, ' ');
763 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
765 if (*currlen < maxlen) {
766 buffer[(*currlen)] = c;
771 #if !defined(HAVE_VSNPRINTF)
772 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
774 return dopr(str, count, fmt, args);
778 #if !defined(HAVE_SNPRINTF)
779 int snprintf(char *str,size_t count,const char *fmt,...)
785 ret = vsnprintf(str, count, fmt, ap);
795 int sprintf(char *str,const char *fmt,...);
818 double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
819 0.9996, 1.996, 4.136, 0};
833 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
847 char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
852 printf ("Testing snprintf format codes against system sprintf...\n");
854 for (x = 0; fp_fmt[x] ; x++) {
855 for (y = 0; fp_nums[y] != 0 ; y++) {
856 int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
857 int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
858 sprintf (buf2, fp_fmt[x], fp_nums[y]);
859 if (strcmp (buf1, buf2)) {
860 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
861 fp_fmt[x], buf1, buf2);
865 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
872 for (x = 0; int_fmt[x] ; x++) {
873 for (y = 0; int_nums[y] != 0 ; y++) {
874 int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
875 int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
876 sprintf (buf2, int_fmt[x], int_nums[y]);
877 if (strcmp (buf1, buf2)) {
878 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
879 int_fmt[x], buf1, buf2);
883 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
890 for (x = 0; str_fmt[x] ; x++) {
891 for (y = 0; str_vals[y] != 0 ; y++) {
892 int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
893 int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
894 sprintf (buf2, str_fmt[x], str_vals[y]);
895 if (strcmp (buf1, buf2)) {
896 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
897 str_fmt[x], buf1, buf2);
901 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
908 printf ("%d tests failed out of %d.\n", fail, num);
910 printf("seeing how many digits we support\n");
912 double v0 = 0.12345678901234567890123456789012345678901;
913 for (x=0; x<100; x++) {
914 snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
915 sprintf(buf2, "%1.1f", v0*pow(10, x));
916 if (strcmp(buf1, buf2)) {
917 printf("we seem to support %d digits\n", x-1);
924 #endif /* SNPRINTF_TEST */