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.
17 * snprintf() and vsnprintf() replacement functions
21 * snprintf.c: Copyright Patrick Powell 1995
22 * This code is based on code written by Patrick Powell (papowell@astart.com)
23 * It may be used for any purpose as long as this notice remains intact
24 * on all source code distributions
26 * Original: Patrick Powell Tue Apr 11 09:48:21 PDT 1995
27 * A bombproof version of doprnt (dopr) included.
28 * Sigh. This sort of thing is always nasty do deal with. Note that
29 * the version here does not include floating point...
31 * snprintf() is used instead of sprintf() as it does limit checks
32 * for string length. This covers a nasty loophole.
34 * The other functions are there to prevent NULL pointers from
35 * causing nast effects.
38 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
39 * This was ugly. It is still ugly. I opted out of floating point
40 * numbers, but the formatter understands just about everything
41 * from the normal C string format, at least as far as I can tell from
42 * the Solaris 2.5 printf(3S) man page.
44 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
45 * Ok, added some minimal floating point support, which means this
46 * probably requires libm on most operating systems. Don't yet
47 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
48 * was pretty badly broken, it just wasn't being exercised in ways
49 * which showed it, so that's been fixed. Also, formated the code
50 * to mutt conventions, and removed dead code left over from the
51 * original. Also, there is now a builtin-test, just compile with:
52 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
53 * and run snprintf for results.
55 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
56 * The PGP code was using unsigned hexadecimal formats.
57 * Unfortunately, unsigned formats simply didn't work.
59 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
60 * The original code assumed that both snprintf() and vsnprintf() were
61 * missing. Some systems only have snprintf() but not vsnprintf(), so
62 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
64 * Andrew Tridgell <tridge@samba.org>, October 1998
65 * fixed handling of %.0f
66 * added test for HAVE_LONG_DOUBLE
68 * tridge@samba.org, idra@samba.org, April 2001
69 * got rid of fcvt code (twas buggy and made testing harder)
72 * Alexander Barton, <alex@barton.de>, 2002-05-19
73 * removed [v]asprintf() and C99 tests: not needed by ngIRCd.
86 #include <sys/types.h>
93 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF)
94 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
96 /* make the compiler happy with an empty file */
97 void dummy_snprintf PARAMS(( void ));
98 void dummy_snprintf PARAMS(( void )) { }
101 #ifdef HAVE_LONG_DOUBLE
102 #define LDOUBLE long double
104 #define LDOUBLE double
107 #ifdef HAVE_LONG_LONG
108 #define LLONG long long
113 static size_t dopr(char *buffer, size_t maxlen, const char *format,
115 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
116 char *value, int flags, int min, int max);
117 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
118 long value, int base, int min, int max, int flags);
119 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
120 LDOUBLE fvalue, int min, int max, int flags);
121 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
124 * dopr(): poor man's version of doprintf
127 /* format read states */
128 #define DP_S_DEFAULT 0
137 /* format flags - Bits */
138 #define DP_F_MINUS (1 << 0)
139 #define DP_F_PLUS (1 << 1)
140 #define DP_F_SPACE (1 << 2)
141 #define DP_F_NUM (1 << 3)
142 #define DP_F_ZERO (1 << 4)
143 #define DP_F_UP (1 << 5)
144 #define DP_F_UNSIGNED (1 << 6)
146 /* Conversion Flags */
149 #define DP_C_LDOUBLE 3
152 #define char_to_int(p) ((p)- '0')
154 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
157 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
170 state = DP_S_DEFAULT;
171 currlen = flags = cflags = min = 0;
175 while (state != DP_S_DONE) {
184 dopr_outch (buffer, &currlen, maxlen, ch);
215 if (isdigit((unsigned char)ch)) {
216 min = 10*min + char_to_int (ch);
218 } else if (ch == '*') {
219 min = va_arg (args, int);
235 if (isdigit((unsigned char)ch)) {
238 max = 10*max + char_to_int (ch);
240 } else if (ch == '*') {
241 max = va_arg (args, int);
257 if (ch == 'l') { /* It's a long long */
263 cflags = DP_C_LDOUBLE;
275 if (cflags == DP_C_SHORT)
276 value = va_arg (args, int);
277 else if (cflags == DP_C_LONG)
278 value = va_arg (args, long int);
279 else if (cflags == DP_C_LLONG)
280 value = va_arg (args, LLONG);
282 value = va_arg (args, int);
283 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
286 flags |= DP_F_UNSIGNED;
287 if (cflags == DP_C_SHORT)
288 value = va_arg (args, unsigned int);
289 else if (cflags == DP_C_LONG)
290 value = (long)va_arg (args, unsigned long int);
291 else if (cflags == DP_C_LLONG)
292 value = (long)va_arg (args, unsigned LLONG);
294 value = (long)va_arg (args, unsigned int);
295 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
298 flags |= DP_F_UNSIGNED;
299 if (cflags == DP_C_SHORT)
300 value = va_arg (args, unsigned int);
301 else if (cflags == DP_C_LONG)
302 value = (long)va_arg (args, unsigned long int);
303 else if (cflags == DP_C_LLONG)
304 value = (LLONG)va_arg (args, unsigned LLONG);
306 value = (long)va_arg (args, unsigned int);
307 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
312 flags |= DP_F_UNSIGNED;
313 if (cflags == DP_C_SHORT)
314 value = va_arg (args, unsigned int);
315 else if (cflags == DP_C_LONG)
316 value = (long)va_arg (args, unsigned long int);
317 else if (cflags == DP_C_LLONG)
318 value = (LLONG)va_arg (args, unsigned LLONG);
320 value = (long)va_arg (args, unsigned int);
321 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
324 if (cflags == DP_C_LDOUBLE)
325 fvalue = va_arg (args, LDOUBLE);
327 fvalue = va_arg (args, double);
328 /* um, floating point? */
329 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
334 if (cflags == DP_C_LDOUBLE)
335 fvalue = va_arg (args, LDOUBLE);
337 fvalue = va_arg (args, double);
342 if (cflags == DP_C_LDOUBLE)
343 fvalue = va_arg (args, LDOUBLE);
345 fvalue = va_arg (args, double);
348 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
351 strvalue = va_arg (args, char *);
353 max = strlen(strvalue);
355 if (min > 0 && max >= 0 && min > max) max = min;
356 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
359 strvalue = va_arg (args, void *);
360 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
363 if (cflags == DP_C_SHORT) {
365 num = va_arg (args, short int *);
367 } else if (cflags == DP_C_LONG) {
369 num = va_arg (args, long int *);
370 *num = (long int)currlen;
371 } else if (cflags == DP_C_LLONG) {
373 num = va_arg (args, LLONG *);
374 *num = (LLONG)currlen;
377 num = va_arg (args, int *);
382 dopr_outch (buffer, &currlen, maxlen, ch);
385 /* not supported yet, treat as next char */
393 state = DP_S_DEFAULT;
394 flags = cflags = min = 0;
401 break; /* some picky compilers need this */
405 if (currlen < maxlen - 1)
406 buffer[currlen] = '\0';
408 buffer[maxlen - 1] = '\0';
414 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
415 char *value, int flags, int min, int max)
417 int padlen, strln; /* amount to pad */
420 #ifdef DEBUG_SNPRINTF
421 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
427 for (strln = 0; value[strln]; ++strln); /* strlen */
428 padlen = min - strln;
431 if (flags & DP_F_MINUS)
432 padlen = -padlen; /* Left Justify */
434 while ((padlen > 0) && (cnt < max)) {
435 dopr_outch (buffer, currlen, maxlen, ' ');
439 while (*value && (cnt < max)) {
440 dopr_outch (buffer, currlen, maxlen, *value++);
443 while ((padlen < 0) && (cnt < max)) {
444 dopr_outch (buffer, currlen, maxlen, ' ');
450 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
452 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
453 long value, int base, int min, int max, int flags)
456 unsigned long uvalue;
459 int spadlen = 0; /* amount to space pad */
460 int zpadlen = 0; /* amount to zero pad */
468 if(!(flags & DP_F_UNSIGNED)) {
473 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
475 else if (flags & DP_F_SPACE)
480 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
484 (caps? "0123456789ABCDEF":"0123456789abcdef")
485 [uvalue % (unsigned)base ];
486 uvalue = (uvalue / (unsigned)base );
487 } while(uvalue && (place < 20));
488 if (place == 20) place--;
491 zpadlen = max - place;
492 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
493 if (zpadlen < 0) zpadlen = 0;
494 if (spadlen < 0) spadlen = 0;
495 if (flags & DP_F_ZERO) {
496 zpadlen = MAX(zpadlen, spadlen);
499 if (flags & DP_F_MINUS)
500 spadlen = -spadlen; /* Left Justifty */
502 #ifdef DEBUG_SNPRINTF
503 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
504 zpadlen, spadlen, min, max, place);
508 while (spadlen > 0) {
509 dopr_outch (buffer, currlen, maxlen, ' ');
515 dopr_outch (buffer, currlen, maxlen, signvalue);
519 while (zpadlen > 0) {
520 dopr_outch (buffer, currlen, maxlen, '0');
527 dopr_outch (buffer, currlen, maxlen, convert[--place]);
529 /* Left Justified spaces */
530 while (spadlen < 0) {
531 dopr_outch (buffer, currlen, maxlen, ' ');
536 static LDOUBLE abs_val(LDOUBLE value)
538 LDOUBLE result = value;
546 static LDOUBLE POW10(int exp)
558 static LLONG ROUND(LDOUBLE value)
562 intpart = (LLONG)value;
563 value = value - intpart;
564 if (value >= 0.5) intpart++;
569 /* a replacement for modf that doesn't need the math library. Should
570 be portable, but slow */
571 static double my_modf(double x0, double *iptr)
578 for (i=0;i<100;i++) {
580 if (l <= (x+1) && l >= (x-1)) break;
586 /* yikes! the number is beyond what we can handle. What do we do? */
595 ret = my_modf(x0-l*f, &i2);
605 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
606 LDOUBLE fvalue, int min, int max, int flags)
614 int padlen = 0; /* amount to pad */
623 * AIX manpage says the default is 0, but Solaris says the default
624 * is 6, and sprintf on AIX defaults to 6
629 ufvalue = abs_val (fvalue);
634 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
637 if (flags & DP_F_SPACE)
643 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
647 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
651 * Sorry, we only support 16 digits past the decimal because of our
657 /* We "cheat" by converting the fractional part to integer by
658 * multiplying by a factor of 10
662 my_modf(temp, &intpart);
664 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
666 if (fracpart >= POW10(max)) {
668 fracpart -= POW10(max);
672 /* Convert integer part */
675 my_modf(intpart*0.1, &intpart);
677 index = (int) ((temp -intpart +0.05)* 10.0);
678 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
679 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
681 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
682 } while (intpart && (iplace < 311));
683 if (iplace == 311) iplace--;
684 iconvert[iplace] = 0;
686 /* Convert fractional part */
691 my_modf(fracpart*0.1, &fracpart);
693 index = (int) ((temp -fracpart +0.05)* 10.0);
694 /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
695 /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
697 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
698 } while(fracpart && (fplace < 311));
699 if (fplace == 311) fplace--;
701 fconvert[fplace] = 0;
703 /* -1 for decimal point, another -1 if we are printing a sign */
704 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
705 zpadlen = max - fplace;
706 if (zpadlen < 0) zpadlen = 0;
709 if (flags & DP_F_MINUS)
710 padlen = -padlen; /* Left Justifty */
712 if ((flags & DP_F_ZERO) && (padlen > 0)) {
714 dopr_outch (buffer, currlen, maxlen, signvalue);
719 dopr_outch (buffer, currlen, maxlen, '0');
724 dopr_outch (buffer, currlen, maxlen, ' ');
728 dopr_outch (buffer, currlen, maxlen, signvalue);
731 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
733 #ifdef DEBUG_SNPRINTF
734 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
738 * Decimal point. This should probably use locale to find the correct
742 dopr_outch (buffer, currlen, maxlen, '.');
745 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
748 while (zpadlen > 0) {
749 dopr_outch (buffer, currlen, maxlen, '0');
754 dopr_outch (buffer, currlen, maxlen, ' ');
759 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
761 if (*currlen < maxlen) {
762 buffer[(*currlen)] = c;
767 #if !defined(HAVE_VSNPRINTF)
768 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
770 return dopr(str, count, fmt, args);
774 #if !defined(HAVE_SNPRINTF)
775 int snprintf(char *str,size_t count,const char *fmt,...)
781 ret = vsnprintf(str, count, fmt, ap);
791 int sprintf(char *str,const char *fmt,...);
814 double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
815 0.9996, 1.996, 4.136, 0};
829 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
843 char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
848 printf ("Testing snprintf format codes against system sprintf...\n");
850 for (x = 0; fp_fmt[x] ; x++) {
851 for (y = 0; fp_nums[y] != 0 ; y++) {
852 int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
853 int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
854 sprintf (buf2, fp_fmt[x], fp_nums[y]);
855 if (strcmp (buf1, buf2)) {
856 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
857 fp_fmt[x], buf1, buf2);
861 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
868 for (x = 0; int_fmt[x] ; x++) {
869 for (y = 0; int_nums[y] != 0 ; y++) {
870 int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
871 int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
872 sprintf (buf2, int_fmt[x], int_nums[y]);
873 if (strcmp (buf1, buf2)) {
874 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
875 int_fmt[x], buf1, buf2);
879 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
886 for (x = 0; str_fmt[x] ; x++) {
887 for (y = 0; str_vals[y] != 0 ; y++) {
888 int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
889 int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
890 sprintf (buf2, str_fmt[x], str_vals[y]);
891 if (strcmp (buf1, buf2)) {
892 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
893 str_fmt[x], buf1, buf2);
897 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
904 printf ("%d tests failed out of %d.\n", fail, num);
906 printf("seeing how many digits we support\n");
908 double v0 = 0.12345678901234567890123456789012345678901;
909 for (x=0; x<100; x++) {
910 snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
911 sprintf(buf2, "%1.1f", v0*pow(10, x));
912 if (strcmp(buf1, buf2)) {
913 printf("we seem to support %d digits\n", x-1);
920 #endif /* SNPRINTF_TEST */