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.
84 #include <sys/types.h>
90 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF)
91 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
93 /* make the compiler happy with an empty file */
94 void dummy_snprintf PARAMS(( void ));
95 void dummy_snprintf PARAMS(( void )) { }
98 #ifdef HAVE_LONG_DOUBLE
99 #define LDOUBLE long double
101 #define LDOUBLE double
104 #ifdef HAVE_LONG_LONG
105 #define LLONG long long
110 static size_t dopr(char *buffer, size_t maxlen, const char *format,
112 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
113 char *value, int flags, int min, int max);
114 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
115 long value, int base, int min, int max, int flags);
116 static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
117 LDOUBLE fvalue, int min, int max, int flags);
118 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
121 * dopr(): poor man's version of doprintf
124 /* format read states */
125 #define DP_S_DEFAULT 0
134 /* format flags - Bits */
135 #define DP_F_MINUS (1 << 0)
136 #define DP_F_PLUS (1 << 1)
137 #define DP_F_SPACE (1 << 2)
138 #define DP_F_NUM (1 << 3)
139 #define DP_F_ZERO (1 << 4)
140 #define DP_F_UP (1 << 5)
141 #define DP_F_UNSIGNED (1 << 6)
143 /* Conversion Flags */
146 #define DP_C_LDOUBLE 3
149 #define char_to_int(p) ((p)- '0')
151 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
154 static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
167 state = DP_S_DEFAULT;
168 currlen = flags = cflags = min = 0;
172 while (state != DP_S_DONE) {
181 dopr_outch (buffer, &currlen, maxlen, ch);
212 if (isdigit((unsigned char)ch)) {
213 min = 10*min + char_to_int (ch);
215 } else if (ch == '*') {
216 min = va_arg (args, int);
232 if (isdigit((unsigned char)ch)) {
235 max = 10*max + char_to_int (ch);
237 } else if (ch == '*') {
238 max = va_arg (args, int);
254 if (ch == 'l') { /* It's a long long */
260 cflags = DP_C_LDOUBLE;
272 if (cflags == DP_C_SHORT)
273 value = va_arg (args, int);
274 else if (cflags == DP_C_LONG)
275 value = va_arg (args, long int);
276 else if (cflags == DP_C_LLONG)
277 value = va_arg (args, LLONG);
279 value = va_arg (args, int);
280 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
283 flags |= DP_F_UNSIGNED;
284 if (cflags == DP_C_SHORT)
285 value = va_arg (args, unsigned int);
286 else if (cflags == DP_C_LONG)
287 value = (long)va_arg (args, unsigned long int);
288 else if (cflags == DP_C_LLONG)
289 value = (long)va_arg (args, unsigned LLONG);
291 value = (long)va_arg (args, unsigned int);
292 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
295 flags |= DP_F_UNSIGNED;
296 if (cflags == DP_C_SHORT)
297 value = va_arg (args, unsigned int);
298 else if (cflags == DP_C_LONG)
299 value = (long)va_arg (args, unsigned long int);
300 else if (cflags == DP_C_LLONG)
301 value = (LLONG)va_arg (args, unsigned LLONG);
303 value = (long)va_arg (args, unsigned int);
304 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
309 flags |= DP_F_UNSIGNED;
310 if (cflags == DP_C_SHORT)
311 value = va_arg (args, unsigned int);
312 else if (cflags == DP_C_LONG)
313 value = (long)va_arg (args, unsigned long int);
314 else if (cflags == DP_C_LLONG)
315 value = (LLONG)va_arg (args, unsigned LLONG);
317 value = (long)va_arg (args, unsigned int);
318 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
321 if (cflags == DP_C_LDOUBLE)
322 fvalue = va_arg (args, LDOUBLE);
324 fvalue = va_arg (args, double);
325 /* um, floating point? */
326 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
331 if (cflags == DP_C_LDOUBLE)
332 fvalue = va_arg (args, LDOUBLE);
334 fvalue = va_arg (args, double);
339 if (cflags == DP_C_LDOUBLE)
340 fvalue = va_arg (args, LDOUBLE);
342 fvalue = va_arg (args, double);
345 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
348 strvalue = va_arg (args, char *);
350 max = strlen(strvalue);
352 if (min > 0 && max >= 0 && min > max) max = min;
353 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
356 strvalue = va_arg (args, void *);
357 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
360 if (cflags == DP_C_SHORT) {
362 num = va_arg (args, short int *);
364 } else if (cflags == DP_C_LONG) {
366 num = va_arg (args, long int *);
367 *num = (long int)currlen;
368 } else if (cflags == DP_C_LLONG) {
370 num = va_arg (args, LLONG *);
371 *num = (LLONG)currlen;
374 num = va_arg (args, int *);
379 dopr_outch (buffer, &currlen, maxlen, ch);
382 /* not supported yet, treat as next char */
390 state = DP_S_DEFAULT;
391 flags = cflags = min = 0;
398 break; /* some picky compilers need this */
402 if (currlen < maxlen - 1)
403 buffer[currlen] = '\0';
405 buffer[maxlen - 1] = '\0';
411 static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
412 char *value, int flags, int min, int max)
414 int padlen, strln; /* amount to pad */
417 #ifdef DEBUG_SNPRINTF
418 printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
424 for (strln = 0; value[strln]; ++strln); /* strlen */
425 padlen = min - strln;
428 if (flags & DP_F_MINUS)
429 padlen = -padlen; /* Left Justify */
431 while ((padlen > 0) && (cnt < max)) {
432 dopr_outch (buffer, currlen, maxlen, ' ');
436 while (*value && (cnt < max)) {
437 dopr_outch (buffer, currlen, maxlen, *value++);
440 while ((padlen < 0) && (cnt < max)) {
441 dopr_outch (buffer, currlen, maxlen, ' ');
447 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
449 static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
450 long value, int base, int min, int max, int flags)
453 unsigned long uvalue;
456 int spadlen = 0; /* amount to space pad */
457 int zpadlen = 0; /* amount to zero pad */
465 if(!(flags & DP_F_UNSIGNED)) {
470 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
472 else if (flags & DP_F_SPACE)
477 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
481 (caps? "0123456789ABCDEF":"0123456789abcdef")
482 [uvalue % (unsigned)base ];
483 uvalue = (uvalue / (unsigned)base );
484 } while(uvalue && (place < 20));
485 if (place == 20) place--;
488 zpadlen = max - place;
489 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
490 if (zpadlen < 0) zpadlen = 0;
491 if (spadlen < 0) spadlen = 0;
492 if (flags & DP_F_ZERO) {
493 zpadlen = MAX(zpadlen, spadlen);
496 if (flags & DP_F_MINUS)
497 spadlen = -spadlen; /* Left Justifty */
499 #ifdef DEBUG_SNPRINTF
500 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
501 zpadlen, spadlen, min, max, place);
505 while (spadlen > 0) {
506 dopr_outch (buffer, currlen, maxlen, ' ');
512 dopr_outch (buffer, currlen, maxlen, signvalue);
516 while (zpadlen > 0) {
517 dopr_outch (buffer, currlen, maxlen, '0');
524 dopr_outch (buffer, currlen, maxlen, convert[--place]);
526 /* Left Justified spaces */
527 while (spadlen < 0) {
528 dopr_outch (buffer, currlen, maxlen, ' ');
533 static LDOUBLE abs_val(LDOUBLE value)
535 LDOUBLE result = value;
543 static LDOUBLE POW10(int exp)
555 static LLONG ROUND(LDOUBLE value)
559 intpart = (LLONG)value;
560 value = value - intpart;
561 if (value >= 0.5) intpart++;
566 /* a replacement for modf that doesn't need the math library. Should
567 be portable, but slow */
568 static double my_modf(double x0, double *iptr)
575 for (i=0;i<100;i++) {
577 if (l <= (x+1) && l >= (x-1)) break;
583 /* yikes! the number is beyond what we can handle. What do we do? */
592 ret = my_modf(x0-l*f, &i2);
602 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
603 LDOUBLE fvalue, int min, int max, int flags)
611 int padlen = 0; /* amount to pad */
620 * AIX manpage says the default is 0, but Solaris says the default
621 * is 6, and sprintf on AIX defaults to 6
626 ufvalue = abs_val (fvalue);
631 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
634 if (flags & DP_F_SPACE)
640 * Sorry, we only support 16 digits past the decimal because of our
646 /* We "cheat" by converting the fractional part to integer by
647 * multiplying by a factor of 10
651 my_modf(temp, &intpart);
653 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
655 if (fracpart >= POW10(max)) {
657 fracpart -= POW10(max);
661 /* Convert integer part */
664 my_modf(intpart*0.1, &intpart);
666 index = (int) ((temp -intpart +0.05)* 10.0);
667 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
668 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
670 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
671 } while (intpart && (iplace < 311));
672 if (iplace == 311) iplace--;
673 iconvert[iplace] = 0;
675 /* Convert fractional part */
680 my_modf(fracpart*0.1, &fracpart);
682 index = (int) ((temp -fracpart +0.05)* 10.0);
683 /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
684 /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
686 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
687 } while(fracpart && (fplace < 311));
688 if (fplace == 311) fplace--;
690 fconvert[fplace] = 0;
692 /* -1 for decimal point, another -1 if we are printing a sign */
693 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
694 zpadlen = max - fplace;
695 if (zpadlen < 0) zpadlen = 0;
698 if (flags & DP_F_MINUS)
699 padlen = -padlen; /* Left Justifty */
701 if ((flags & DP_F_ZERO) && (padlen > 0)) {
703 dopr_outch (buffer, currlen, maxlen, signvalue);
708 dopr_outch (buffer, currlen, maxlen, '0');
713 dopr_outch (buffer, currlen, maxlen, ' ');
717 dopr_outch (buffer, currlen, maxlen, signvalue);
720 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
722 #ifdef DEBUG_SNPRINTF
723 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
727 * Decimal point. This should probably use locale to find the correct
731 dopr_outch (buffer, currlen, maxlen, '.');
734 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
737 while (zpadlen > 0) {
738 dopr_outch (buffer, currlen, maxlen, '0');
743 dopr_outch (buffer, currlen, maxlen, ' ');
748 static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
750 if (*currlen < maxlen) {
751 buffer[(*currlen)] = c;
756 #if !defined(HAVE_VSNPRINTF)
757 int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
759 return dopr(str, count, fmt, args);
763 #if !defined(HAVE_SNPRINTF)
764 int snprintf(char *str,size_t count,const char *fmt,...)
770 ret = vsnprintf(str, count, fmt, ap);