2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 * der GNU General Public License (GPL), wie von der Free Software Foundation
7 * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 * $Id: vsnprintf.c,v 1.3 2002/05/27 13:01:04 alex Exp $
14 * vsnprintf.c: u.a. Ersatz fuer vsnprintf()
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 )) { }
104 #ifdef HAVE_LONG_DOUBLE
105 #define LDOUBLE long double
107 #define LDOUBLE double
110 #ifdef HAVE_LONG_LONG
111 #define LLONG long long
116 static size_t dopr(char *buffer, size_t maxlen, const char *format,
118 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
119 char *value, int flags, int min, int max);
120 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
121 long value, int base, int min, int max, int flags);
122 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
123 LDOUBLE fvalue, int min, int max, int flags);
124 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
127 * dopr(): poor man's version of doprintf
130 /* format read states */
131 #define DP_S_DEFAULT 0
140 /* format flags - Bits */
141 #define DP_F_MINUS (1 << 0)
142 #define DP_F_PLUS (1 << 1)
143 #define DP_F_SPACE (1 << 2)
144 #define DP_F_NUM (1 << 3)
145 #define DP_F_ZERO (1 << 4)
146 #define DP_F_UP (1 << 5)
147 #define DP_F_UNSIGNED (1 << 6)
149 /* Conversion Flags */
152 #define DP_C_LDOUBLE 3
155 #define char_to_int(p) ((p)- '0')
157 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
160 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
173 state = DP_S_DEFAULT;
174 currlen = flags = cflags = min = 0;
178 while (state != DP_S_DONE) {
187 dopr_outch (buffer, &currlen, maxlen, ch);
218 if (isdigit((unsigned char)ch)) {
219 min = 10*min + char_to_int (ch);
221 } else if (ch == '*') {
222 min = va_arg (args, int);
238 if (isdigit((unsigned char)ch)) {
241 max = 10*max + char_to_int (ch);
243 } else if (ch == '*') {
244 max = va_arg (args, int);
260 if (ch == 'l') { /* It's a long long */
266 cflags = DP_C_LDOUBLE;
278 if (cflags == DP_C_SHORT)
279 value = va_arg (args, int);
280 else if (cflags == DP_C_LONG)
281 value = va_arg (args, long int);
282 else if (cflags == DP_C_LLONG)
283 value = va_arg (args, LLONG);
285 value = va_arg (args, int);
286 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 int);
292 else if (cflags == DP_C_LONG)
293 value = (long)va_arg (args, unsigned long int);
294 else if (cflags == DP_C_LLONG)
295 value = (long)va_arg (args, unsigned LLONG);
297 value = (long)va_arg (args, unsigned int);
298 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
301 flags |= DP_F_UNSIGNED;
302 if (cflags == DP_C_SHORT)
303 value = va_arg (args, unsigned int);
304 else if (cflags == DP_C_LONG)
305 value = (long)va_arg (args, unsigned long int);
306 else if (cflags == DP_C_LLONG)
307 value = (LLONG)va_arg (args, unsigned LLONG);
309 value = (long)va_arg (args, unsigned int);
310 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
315 flags |= DP_F_UNSIGNED;
316 if (cflags == DP_C_SHORT)
317 value = va_arg (args, unsigned int);
318 else if (cflags == DP_C_LONG)
319 value = (long)va_arg (args, unsigned long int);
320 else if (cflags == DP_C_LLONG)
321 value = (LLONG)va_arg (args, unsigned LLONG);
323 value = (long)va_arg (args, unsigned int);
324 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
327 if (cflags == DP_C_LDOUBLE)
328 fvalue = va_arg (args, LDOUBLE);
330 fvalue = va_arg (args, double);
331 /* um, floating point? */
332 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
337 if (cflags == DP_C_LDOUBLE)
338 fvalue = va_arg (args, LDOUBLE);
340 fvalue = va_arg (args, double);
345 if (cflags == DP_C_LDOUBLE)
346 fvalue = va_arg (args, LDOUBLE);
348 fvalue = va_arg (args, double);
351 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
354 strvalue = va_arg (args, char *);
356 max = strlen(strvalue);
358 if (min > 0 && max >= 0 && min > max) max = min;
359 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
362 strvalue = va_arg (args, void *);
363 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
366 if (cflags == DP_C_SHORT) {
368 num = va_arg (args, short int *);
370 } else if (cflags == DP_C_LONG) {
372 num = va_arg (args, long int *);
373 *num = (long int)currlen;
374 } else if (cflags == DP_C_LLONG) {
376 num = va_arg (args, LLONG *);
377 *num = (LLONG)currlen;
380 num = va_arg (args, int *);
385 dopr_outch (buffer, &currlen, maxlen, ch);
388 /* not supported yet, treat as next char */
396 state = DP_S_DEFAULT;
397 flags = cflags = min = 0;
404 break; /* some picky compilers need this */
408 if (currlen < maxlen - 1)
409 buffer[currlen] = '\0';
411 buffer[maxlen - 1] = '\0';
417 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
418 char *value, int flags, int min, int max)
420 int padlen, strln; /* amount to pad */
423 #ifdef DEBUG_SNPRINTF
424 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
430 for (strln = 0; value[strln]; ++strln); /* strlen */
431 padlen = min - strln;
434 if (flags & DP_F_MINUS)
435 padlen = -padlen; /* Left Justify */
437 while ((padlen > 0) && (cnt < max)) {
438 dopr_outch (buffer, currlen, maxlen, ' ');
442 while (*value && (cnt < max)) {
443 dopr_outch (buffer, currlen, maxlen, *value++);
446 while ((padlen < 0) && (cnt < max)) {
447 dopr_outch (buffer, currlen, maxlen, ' ');
453 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
455 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
456 long value, int base, int min, int max, int flags)
459 unsigned long uvalue;
462 int spadlen = 0; /* amount to space pad */
463 int zpadlen = 0; /* amount to zero pad */
471 if(!(flags & DP_F_UNSIGNED)) {
476 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
478 else if (flags & DP_F_SPACE)
483 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
487 (caps? "0123456789ABCDEF":"0123456789abcdef")
488 [uvalue % (unsigned)base ];
489 uvalue = (uvalue / (unsigned)base );
490 } while(uvalue && (place < 20));
491 if (place == 20) place--;
494 zpadlen = max - place;
495 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
496 if (zpadlen < 0) zpadlen = 0;
497 if (spadlen < 0) spadlen = 0;
498 if (flags & DP_F_ZERO) {
499 zpadlen = MAX(zpadlen, spadlen);
502 if (flags & DP_F_MINUS)
503 spadlen = -spadlen; /* Left Justifty */
505 #ifdef DEBUG_SNPRINTF
506 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
507 zpadlen, spadlen, min, max, place);
511 while (spadlen > 0) {
512 dopr_outch (buffer, currlen, maxlen, ' ');
518 dopr_outch (buffer, currlen, maxlen, signvalue);
522 while (zpadlen > 0) {
523 dopr_outch (buffer, currlen, maxlen, '0');
530 dopr_outch (buffer, currlen, maxlen, convert[--place]);
532 /* Left Justified spaces */
533 while (spadlen < 0) {
534 dopr_outch (buffer, currlen, maxlen, ' ');
539 static LDOUBLE abs_val(LDOUBLE value)
541 LDOUBLE result = value;
549 static LDOUBLE POW10(int exp)
561 static LLONG ROUND(LDOUBLE value)
565 intpart = (LLONG)value;
566 value = value - intpart;
567 if (value >= 0.5) intpart++;
572 /* a replacement for modf that doesn't need the math library. Should
573 be portable, but slow */
574 static double my_modf(double x0, double *iptr)
581 for (i=0;i<100;i++) {
583 if (l <= (x+1) && l >= (x-1)) break;
589 /* yikes! the number is beyond what we can handle. What do we do? */
598 ret = my_modf(x0-l*f, &i2);
608 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
609 LDOUBLE fvalue, int min, int max, int flags)
617 int padlen = 0; /* amount to pad */
626 * AIX manpage says the default is 0, but Solaris says the default
627 * is 6, and sprintf on AIX defaults to 6
632 ufvalue = abs_val (fvalue);
637 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
640 if (flags & DP_F_SPACE)
646 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
650 if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
654 * Sorry, we only support 16 digits past the decimal because of our
660 /* We "cheat" by converting the fractional part to integer by
661 * multiplying by a factor of 10
665 my_modf(temp, &intpart);
667 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
669 if (fracpart >= POW10(max)) {
671 fracpart -= POW10(max);
675 /* Convert integer part */
678 my_modf(intpart*0.1, &intpart);
680 index = (int) ((temp -intpart +0.05)* 10.0);
681 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
682 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
684 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
685 } while (intpart && (iplace < 311));
686 if (iplace == 311) iplace--;
687 iconvert[iplace] = 0;
689 /* Convert fractional part */
694 my_modf(fracpart*0.1, &fracpart);
696 index = (int) ((temp -fracpart +0.05)* 10.0);
697 /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
698 /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
700 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
701 } while(fracpart && (fplace < 311));
702 if (fplace == 311) fplace--;
704 fconvert[fplace] = 0;
706 /* -1 for decimal point, another -1 if we are printing a sign */
707 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
708 zpadlen = max - fplace;
709 if (zpadlen < 0) zpadlen = 0;
712 if (flags & DP_F_MINUS)
713 padlen = -padlen; /* Left Justifty */
715 if ((flags & DP_F_ZERO) && (padlen > 0)) {
717 dopr_outch (buffer, currlen, maxlen, signvalue);
722 dopr_outch (buffer, currlen, maxlen, '0');
727 dopr_outch (buffer, currlen, maxlen, ' ');
731 dopr_outch (buffer, currlen, maxlen, signvalue);
734 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
736 #ifdef DEBUG_SNPRINTF
737 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
741 * Decimal point. This should probably use locale to find the correct
745 dopr_outch (buffer, currlen, maxlen, '.');
748 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
751 while (zpadlen > 0) {
752 dopr_outch (buffer, currlen, maxlen, '0');
757 dopr_outch (buffer, currlen, maxlen, ' ');
762 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
764 if (*currlen < maxlen) {
765 buffer[(*currlen)] = c;
770 #if !defined(HAVE_VSNPRINTF)
771 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
773 return dopr(str, count, fmt, args);
777 #if !defined(HAVE_SNPRINTF)
778 int snprintf(char *str,size_t count,const char *fmt,...)
784 ret = vsnprintf(str, count, fmt, ap);
794 int sprintf(char *str,const char *fmt,...);
817 double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
818 0.9996, 1.996, 4.136, 0};
832 long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
846 char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
851 printf ("Testing snprintf format codes against system sprintf...\n");
853 for (x = 0; fp_fmt[x] ; x++) {
854 for (y = 0; fp_nums[y] != 0 ; y++) {
855 int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
856 int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
857 sprintf (buf2, fp_fmt[x], fp_nums[y]);
858 if (strcmp (buf1, buf2)) {
859 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
860 fp_fmt[x], buf1, buf2);
864 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
871 for (x = 0; int_fmt[x] ; x++) {
872 for (y = 0; int_nums[y] != 0 ; y++) {
873 int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
874 int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
875 sprintf (buf2, int_fmt[x], int_nums[y]);
876 if (strcmp (buf1, buf2)) {
877 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
878 int_fmt[x], buf1, buf2);
882 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
889 for (x = 0; str_fmt[x] ; x++) {
890 for (y = 0; str_vals[y] != 0 ; y++) {
891 int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
892 int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
893 sprintf (buf2, str_fmt[x], str_vals[y]);
894 if (strcmp (buf1, buf2)) {
895 printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
896 str_fmt[x], buf1, buf2);
900 printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
907 printf ("%d tests failed out of %d.\n", fail, num);
909 printf("seeing how many digits we support\n");
911 double v0 = 0.12345678901234567890123456789012345678901;
912 for (x=0; x<100; x++) {
913 snprintf(buf1, sizeof(buf1), "%1.1f", v0*pow(10, x));
914 sprintf(buf2, "%1.1f", v0*pow(10, x));
915 if (strcmp(buf1, buf2)) {
916 printf("we seem to support %d digits\n", x-1);
923 #endif /* SNPRINTF_TEST */