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>
94 #if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF)
95 /* only include stdio.h if we are not re-defining snprintf or vsnprintf */
97 /* make the compiler happy with an empty file */
98 void dummy_snprintf PARAMS(( void ));
99 void dummy_snprintf PARAMS(( void )) { }
102 #ifdef HAVE_LONG_DOUBLE
103 #define LDOUBLE long double
105 #define LDOUBLE double
108 #ifdef HAVE_LONG_LONG
109 #define LLONG long long
114 static size_t dopr PARAMS((char *buffer, size_t maxlen, const char *format,
116 static void fmtstr PARAMS((char *buffer, size_t *currlen, size_t maxlen,
117 char *value, int flags, int min, int max));
118 static void fmtint PARAMS((char *buffer, size_t *currlen, size_t maxlen,
119 long value, int base, int min, int max, int flags));
120 static void fmtfp PARAMS((char *buffer, size_t *currlen, size_t maxlen,
121 LDOUBLE fvalue, int min, int max, int flags));
122 static void dopr_outch PARAMS((char *buffer, size_t *currlen, size_t maxlen,
126 * dopr(): poor man's version of doprintf
129 /* format read states */
130 #define DP_S_DEFAULT 0
139 /* format flags - Bits */
140 #define DP_F_MINUS (1 << 0)
141 #define DP_F_PLUS (1 << 1)
142 #define DP_F_SPACE (1 << 2)
143 #define DP_F_NUM (1 << 3)
144 #define DP_F_ZERO (1 << 4)
145 #define DP_F_UP (1 << 5)
146 #define DP_F_UNSIGNED (1 << 6)
148 /* Conversion Flags */
151 #define DP_C_LDOUBLE 3
154 #define char_to_int(p) ((p)- '0')
156 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
160 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';
418 fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags,
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) */
457 fmtint(char *buffer, size_t *currlen, size_t maxlen, long value, int base,
458 int min, int max, int flags)
461 unsigned long uvalue;
464 int spadlen = 0; /* amount to space pad */
465 int zpadlen = 0; /* amount to zero pad */
473 if(!(flags & DP_F_UNSIGNED)) {
478 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
480 else if (flags & DP_F_SPACE)
485 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
489 (caps? "0123456789ABCDEF":"0123456789abcdef")
490 [uvalue % (unsigned)base ];
491 uvalue = (uvalue / (unsigned)base );
492 } while(uvalue && (place < 20));
493 if (place == 20) place--;
496 zpadlen = max - place;
497 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
498 if (zpadlen < 0) zpadlen = 0;
499 if (spadlen < 0) spadlen = 0;
500 if (flags & DP_F_ZERO) {
501 zpadlen = MAX(zpadlen, spadlen);
504 if (flags & DP_F_MINUS)
505 spadlen = -spadlen; /* Left Justifty */
507 #ifdef DEBUG_SNPRINTF
508 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
509 zpadlen, spadlen, min, max, place);
513 while (spadlen > 0) {
514 dopr_outch (buffer, currlen, maxlen, ' ');
520 dopr_outch (buffer, currlen, maxlen, signvalue);
524 while (zpadlen > 0) {
525 dopr_outch (buffer, currlen, maxlen, '0');
532 dopr_outch (buffer, currlen, maxlen, convert[--place]);
534 /* Left Justified spaces */
535 while (spadlen < 0) {
536 dopr_outch (buffer, currlen, maxlen, ' ');
542 abs_val(LDOUBLE value)
544 LDOUBLE result = value;
570 intpart = (LLONG)value;
571 value = value - intpart;
572 if (value >= 0.5) intpart++;
577 /* a replacement for modf that doesn't need the math library. Should
578 be portable, but slow */
580 my_modf(double x0, double *iptr)
587 for (i=0;i<100;i++) {
589 if (l <= (x+1) && l >= (x-1)) break;
595 /* yikes! the number is beyond what we can handle. What do we do? */
604 ret = my_modf(x0-l*f, &i2);
615 fmtfp (char *buffer, size_t *currlen, size_t maxlen, LDOUBLE fvalue,
616 int min, int max, int flags)
624 int padlen = 0; /* amount to pad */
633 * AIX manpage says the default is 0, but Solaris says the default
634 * is 6, and sprintf on AIX defaults to 6
639 ufvalue = abs_val (fvalue);
644 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
647 if (flags & DP_F_SPACE)
653 * Sorry, we only support 16 digits past the decimal because of our
659 /* We "cheat" by converting the fractional part to integer by
660 * multiplying by a factor of 10
664 my_modf(temp, &intpart);
666 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
668 if (fracpart >= POW10(max)) {
670 fracpart -= POW10(max);
674 /* Convert integer part */
677 my_modf(intpart*0.1, &intpart);
679 index = (int) ((temp -intpart +0.05)* 10.0);
680 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
681 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
683 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
684 } while (intpart && (iplace < 311));
685 if (iplace == 311) iplace--;
686 iconvert[iplace] = 0;
688 /* Convert fractional part */
693 my_modf(fracpart*0.1, &fracpart);
695 index = (int) ((temp -fracpart +0.05)* 10.0);
696 /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
697 /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
699 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
700 } while(fracpart && (fplace < 311));
701 if (fplace == 311) fplace--;
703 fconvert[fplace] = 0;
705 /* -1 for decimal point, another -1 if we are printing a sign */
706 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
707 zpadlen = max - fplace;
708 if (zpadlen < 0) zpadlen = 0;
711 if (flags & DP_F_MINUS)
712 padlen = -padlen; /* Left Justifty */
714 if ((flags & DP_F_ZERO) && (padlen > 0)) {
716 dopr_outch (buffer, currlen, maxlen, signvalue);
721 dopr_outch (buffer, currlen, maxlen, '0');
726 dopr_outch (buffer, currlen, maxlen, ' ');
730 dopr_outch (buffer, currlen, maxlen, signvalue);
733 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
735 #ifdef DEBUG_SNPRINTF
736 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
740 * Decimal point. This should probably use locale to find the correct
744 dopr_outch (buffer, currlen, maxlen, '.');
747 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
750 while (zpadlen > 0) {
751 dopr_outch (buffer, currlen, maxlen, '0');
756 dopr_outch (buffer, currlen, maxlen, ' ');
762 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)
772 vsnprintf (char *str, size_t count, const char *fmt, va_list args)
774 return dopr(str, count, fmt, args);
778 #if !defined(HAVE_SNPRINTF)
781 snprintf(char *str, size_t count, const char *fmt, ...)
784 snprintf(str, count, fmt, va_alist)
795 ret = vsnprintf(str, count, fmt, ap);