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.
16 * snprintf() and vsnprintf() replacement functions
20 * snprintf.c: Copyright Patrick Powell 1995
21 * This code is based on code written by Patrick Powell (papowell@astart.com)
22 * It may be used for any purpose as long as this notice remains intact
23 * on all source code distributions
25 * Original: Patrick Powell Tue Apr 11 09:48:21 PDT 1995
26 * A bombproof version of doprnt (dopr) included.
27 * Sigh. This sort of thing is always nasty do deal with. Note that
28 * the version here does not include floating point...
30 * snprintf() is used instead of sprintf() as it does limit checks
31 * for string length. This covers a nasty loophole.
33 * The other functions are there to prevent NULL pointers from
34 * causing nast effects.
37 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
38 * This was ugly. It is still ugly. I opted out of floating point
39 * numbers, but the formatter understands just about everything
40 * from the normal C string format, at least as far as I can tell from
41 * the Solaris 2.5 printf(3S) man page.
43 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
44 * Ok, added some minimal floating point support, which means this
45 * probably requires libm on most operating systems. Don't yet
46 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
47 * was pretty badly broken, it just wasn't being exercised in ways
48 * which showed it, so that's been fixed. Also, formated the code
49 * to mutt conventions, and removed dead code left over from the
50 * original. Also, there is now a builtin-test, just compile with:
51 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
52 * and run snprintf for results.
54 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
55 * The PGP code was using unsigned hexadecimal formats.
56 * Unfortunately, unsigned formats simply didn't work.
58 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
59 * The original code assumed that both snprintf() and vsnprintf() were
60 * missing. Some systems only have snprintf() but not vsnprintf(), so
61 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
63 * Andrew Tridgell <tridge@samba.org>, October 1998
64 * fixed handling of %.0f
65 * added test for HAVE_LONG_DOUBLE
67 * tridge@samba.org, idra@samba.org, April 2001
68 * got rid of fcvt code (twas buggy and made testing harder)
71 * Alexander Barton, <alex@barton.de>, 2002-05-19
72 * removed [v]asprintf() and C99 tests: not needed by ngIRCd.
85 #include <sys/types.h>
92 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF)
93 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
95 /* make the compiler happy with an empty file */
96 void dummy_snprintf PARAMS(( void ));
97 void dummy_snprintf PARAMS(( void )) { }
100 #ifdef HAVE_LONG_DOUBLE
101 #define LDOUBLE long double
103 #define LDOUBLE double
106 #ifdef HAVE_LONG_LONG
107 #define LLONG long long
112 static size_t dopr(char *buffer, size_t maxlen, const char *format,
114 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
115 char *value, int flags, int min, int max);
116 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
117 long value, int base, int min, int max, int flags);
118 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
119 LDOUBLE fvalue, int min, int max, int flags);
120 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
123 * dopr(): poor man's version of doprintf
126 /* format read states */
127 #define DP_S_DEFAULT 0
136 /* format flags - Bits */
137 #define DP_F_MINUS (1 << 0)
138 #define DP_F_PLUS (1 << 1)
139 #define DP_F_SPACE (1 << 2)
140 #define DP_F_NUM (1 << 3)
141 #define DP_F_ZERO (1 << 4)
142 #define DP_F_UP (1 << 5)
143 #define DP_F_UNSIGNED (1 << 6)
145 /* Conversion Flags */
148 #define DP_C_LDOUBLE 3
151 #define char_to_int(p) ((p)- '0')
153 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
156 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
169 state = DP_S_DEFAULT;
170 currlen = flags = cflags = min = 0;
174 while (state != DP_S_DONE) {
183 dopr_outch (buffer, &currlen, maxlen, ch);
214 if (isdigit((unsigned char)ch)) {
215 min = 10*min + char_to_int (ch);
217 } else if (ch == '*') {
218 min = va_arg (args, int);
234 if (isdigit((unsigned char)ch)) {
237 max = 10*max + char_to_int (ch);
239 } else if (ch == '*') {
240 max = va_arg (args, int);
256 if (ch == 'l') { /* It's a long long */
262 cflags = DP_C_LDOUBLE;
274 if (cflags == DP_C_SHORT)
275 value = va_arg (args, int);
276 else if (cflags == DP_C_LONG)
277 value = va_arg (args, long int);
278 else if (cflags == DP_C_LLONG)
279 value = va_arg (args, LLONG);
281 value = va_arg (args, int);
282 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
285 flags |= DP_F_UNSIGNED;
286 if (cflags == DP_C_SHORT)
287 value = va_arg (args, unsigned int);
288 else if (cflags == DP_C_LONG)
289 value = (long)va_arg (args, unsigned long int);
290 else if (cflags == DP_C_LLONG)
291 value = (long)va_arg (args, unsigned LLONG);
293 value = (long)va_arg (args, unsigned int);
294 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
297 flags |= DP_F_UNSIGNED;
298 if (cflags == DP_C_SHORT)
299 value = va_arg (args, unsigned int);
300 else if (cflags == DP_C_LONG)
301 value = (long)va_arg (args, unsigned long int);
302 else if (cflags == DP_C_LLONG)
303 value = (LLONG)va_arg (args, unsigned LLONG);
305 value = (long)va_arg (args, unsigned int);
306 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
311 flags |= DP_F_UNSIGNED;
312 if (cflags == DP_C_SHORT)
313 value = va_arg (args, unsigned int);
314 else if (cflags == DP_C_LONG)
315 value = (long)va_arg (args, unsigned long int);
316 else if (cflags == DP_C_LLONG)
317 value = (LLONG)va_arg (args, unsigned LLONG);
319 value = (long)va_arg (args, unsigned int);
320 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
323 if (cflags == DP_C_LDOUBLE)
324 fvalue = va_arg (args, LDOUBLE);
326 fvalue = va_arg (args, double);
327 /* um, floating point? */
328 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
333 if (cflags == DP_C_LDOUBLE)
334 fvalue = va_arg (args, LDOUBLE);
336 fvalue = va_arg (args, double);
341 if (cflags == DP_C_LDOUBLE)
342 fvalue = va_arg (args, LDOUBLE);
344 fvalue = va_arg (args, double);
347 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
350 strvalue = va_arg (args, char *);
352 max = strlen(strvalue);
354 if (min > 0 && max >= 0 && min > max) max = min;
355 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
358 strvalue = va_arg (args, void *);
359 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
362 if (cflags == DP_C_SHORT) {
364 num = va_arg (args, short int *);
366 } else if (cflags == DP_C_LONG) {
368 num = va_arg (args, long int *);
369 *num = (long int)currlen;
370 } else if (cflags == DP_C_LLONG) {
372 num = va_arg (args, LLONG *);
373 *num = (LLONG)currlen;
376 num = va_arg (args, int *);
381 dopr_outch (buffer, &currlen, maxlen, ch);
384 /* not supported yet, treat as next char */
392 state = DP_S_DEFAULT;
393 flags = cflags = min = 0;
400 break; /* some picky compilers need this */
404 if (currlen < maxlen - 1)
405 buffer[currlen] = '\0';
407 buffer[maxlen - 1] = '\0';
413 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
414 char *value, int flags, int min, int max)
416 int padlen, strln; /* amount to pad */
419 #ifdef DEBUG_SNPRINTF
420 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
426 for (strln = 0; value[strln]; ++strln); /* strlen */
427 padlen = min - strln;
430 if (flags & DP_F_MINUS)
431 padlen = -padlen; /* Left Justify */
433 while ((padlen > 0) && (cnt < max)) {
434 dopr_outch (buffer, currlen, maxlen, ' ');
438 while (*value && (cnt < max)) {
439 dopr_outch (buffer, currlen, maxlen, *value++);
442 while ((padlen < 0) && (cnt < max)) {
443 dopr_outch (buffer, currlen, maxlen, ' ');
449 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
451 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
452 long value, int base, int min, int max, int flags)
455 unsigned long uvalue;
458 int spadlen = 0; /* amount to space pad */
459 int zpadlen = 0; /* amount to zero pad */
467 if(!(flags & DP_F_UNSIGNED)) {
472 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
474 else if (flags & DP_F_SPACE)
479 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
483 (caps? "0123456789ABCDEF":"0123456789abcdef")
484 [uvalue % (unsigned)base ];
485 uvalue = (uvalue / (unsigned)base );
486 } while(uvalue && (place < 20));
487 if (place == 20) place--;
490 zpadlen = max - place;
491 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
492 if (zpadlen < 0) zpadlen = 0;
493 if (spadlen < 0) spadlen = 0;
494 if (flags & DP_F_ZERO) {
495 zpadlen = MAX(zpadlen, spadlen);
498 if (flags & DP_F_MINUS)
499 spadlen = -spadlen; /* Left Justifty */
501 #ifdef DEBUG_SNPRINTF
502 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
503 zpadlen, spadlen, min, max, place);
507 while (spadlen > 0) {
508 dopr_outch (buffer, currlen, maxlen, ' ');
514 dopr_outch (buffer, currlen, maxlen, signvalue);
518 while (zpadlen > 0) {
519 dopr_outch (buffer, currlen, maxlen, '0');
526 dopr_outch (buffer, currlen, maxlen, convert[--place]);
528 /* Left Justified spaces */
529 while (spadlen < 0) {
530 dopr_outch (buffer, currlen, maxlen, ' ');
535 static LDOUBLE abs_val(LDOUBLE value)
537 LDOUBLE result = value;
545 static LDOUBLE POW10(int exp)
557 static LLONG ROUND(LDOUBLE value)
561 intpart = (LLONG)value;
562 value = value - intpart;
563 if (value >= 0.5) intpart++;
568 /* a replacement for modf that doesn't need the math library. Should
569 be portable, but slow */
570 static double my_modf(double x0, double *iptr)
577 for (i=0;i<100;i++) {
579 if (l <= (x+1) && l >= (x-1)) break;
585 /* yikes! the number is beyond what we can handle. What do we do? */
594 ret = my_modf(x0-l*f, &i2);
604 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
605 LDOUBLE fvalue, int min, int max, int flags)
613 int padlen = 0; /* amount to pad */
622 * AIX manpage says the default is 0, but Solaris says the default
623 * is 6, and sprintf on AIX defaults to 6
628 ufvalue = abs_val (fvalue);
633 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
636 if (flags & DP_F_SPACE)
642 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
646 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
650 * Sorry, we only support 16 digits past the decimal because of our
656 /* We "cheat" by converting the fractional part to integer by
657 * multiplying by a factor of 10
661 my_modf(temp, &intpart);
663 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
665 if (fracpart >= POW10(max)) {
667 fracpart -= POW10(max);
671 /* Convert integer part */
674 my_modf(intpart*0.1, &intpart);
676 index = (int) ((temp -intpart +0.05)* 10.0);
677 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
678 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
680 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
681 } while (intpart && (iplace < 311));
682 if (iplace == 311) iplace--;
683 iconvert[iplace] = 0;
685 /* Convert fractional part */
690 my_modf(fracpart*0.1, &fracpart);
692 index = (int) ((temp -fracpart +0.05)* 10.0);
693 /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
694 /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
696 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
697 } while(fracpart && (fplace < 311));
698 if (fplace == 311) fplace--;
700 fconvert[fplace] = 0;
702 /* -1 for decimal point, another -1 if we are printing a sign */
703 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
704 zpadlen = max - fplace;
705 if (zpadlen < 0) zpadlen = 0;
708 if (flags & DP_F_MINUS)
709 padlen = -padlen; /* Left Justifty */
711 if ((flags & DP_F_ZERO) && (padlen > 0)) {
713 dopr_outch (buffer, currlen, maxlen, signvalue);
718 dopr_outch (buffer, currlen, maxlen, '0');
723 dopr_outch (buffer, currlen, maxlen, ' ');
727 dopr_outch (buffer, currlen, maxlen, signvalue);
730 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
732 #ifdef DEBUG_SNPRINTF
733 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
737 * Decimal point. This should probably use locale to find the correct
741 dopr_outch (buffer, currlen, maxlen, '.');
744 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
747 while (zpadlen > 0) {
748 dopr_outch (buffer, currlen, maxlen, '0');
753 dopr_outch (buffer, currlen, maxlen, ' ');
758 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
760 if (*currlen < maxlen) {
761 buffer[(*currlen)] = c;
766 #if !defined(HAVE_VSNPRINTF)
767 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
769 return dopr(str, count, fmt, args);
773 #if !defined(HAVE_SNPRINTF)
774 int snprintf(char *str,size_t count,const char *fmt,...)
780 ret = vsnprintf(str, count, fmt, ap);
790 int sprintf(char *str,const char *fmt,...);
813 double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
814 0.9996, 1.996, 4.136, 0};
828 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
842 char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
847 printf ("Testing snprintf format codes against system sprintf...\n");
849 for (x = 0; fp_fmt[x] ; x++) {
850 for (y = 0; fp_nums[y] != 0 ; y++) {
851 int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
852 int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
853 sprintf (buf2, fp_fmt[x], fp_nums[y]);
854 if (strcmp (buf1, buf2)) {
855 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
856 fp_fmt[x], buf1, buf2);
860 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
867 for (x = 0; int_fmt[x] ; x++) {
868 for (y = 0; int_nums[y] != 0 ; y++) {
869 int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
870 int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
871 sprintf (buf2, int_fmt[x], int_nums[y]);
872 if (strcmp (buf1, buf2)) {
873 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
874 int_fmt[x], buf1, buf2);
878 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
885 for (x = 0; str_fmt[x] ; x++) {
886 for (y = 0; str_vals[y] != 0 ; y++) {
887 int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
888 int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
889 sprintf (buf2, str_fmt[x], str_vals[y]);
890 if (strcmp (buf1, buf2)) {
891 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
892 str_fmt[x], buf1, buf2);
896 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
903 printf ("%d tests failed out of %d.\n", fail, num);
905 printf("seeing how many digits we support\n");
907 double v0 = 0.12345678901234567890123456789012345678901;
908 for (x=0; x<100; x++) {
909 snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
910 sprintf(buf2, "%1.1f", v0*pow(10, x));
911 if (strcmp(buf1, buf2)) {
912 printf("we seem to support %d digits\n", x-1);
919 #endif /* SNPRINTF_TEST */