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 PARAMS((char *buffer, size_t maxlen, const char *format,
112 static void fmtstr PARAMS((char *buffer, size_t *currlen, size_t maxlen,
113 char *value, int flags, int min, int max));
114 static void fmtint PARAMS((char *buffer, size_t *currlen, size_t maxlen,
115 long value, int base, int min, int max, int flags));
116 static void fmtfp PARAMS((char *buffer, size_t *currlen, size_t maxlen,
117 LDOUBLE fvalue, int min, int max, int flags));
118 static void dopr_outch PARAMS((char *buffer, size_t *currlen, size_t maxlen,
122 * dopr(): poor man's version of doprintf
125 /* format read states */
126 #define DP_S_DEFAULT 0
135 /* format flags - Bits */
136 #define DP_F_MINUS (1 << 0)
137 #define DP_F_PLUS (1 << 1)
138 #define DP_F_SPACE (1 << 2)
139 #define DP_F_NUM (1 << 3)
140 #define DP_F_ZERO (1 << 4)
141 #define DP_F_UP (1 << 5)
142 #define DP_F_UNSIGNED (1 << 6)
144 /* Conversion Flags */
147 #define DP_C_LDOUBLE 3
150 #define char_to_int(p) ((p)- '0')
152 #define MAX(p,q) (((p) >= (q)) ? (p) : (q))
156 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';
414 fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags,
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) */
453 fmtint(char *buffer, size_t *currlen, size_t maxlen, long value, int base,
454 int min, int max, int flags)
457 unsigned long uvalue;
460 int spadlen = 0; /* amount to space pad */
461 int zpadlen = 0; /* amount to zero pad */
469 if(!(flags & DP_F_UNSIGNED)) {
474 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
476 else if (flags & DP_F_SPACE)
481 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
485 (caps? "0123456789ABCDEF":"0123456789abcdef")
486 [uvalue % (unsigned)base ];
487 uvalue = (uvalue / (unsigned)base );
488 } while(uvalue && (place < 20));
489 if (place == 20) place--;
492 zpadlen = max - place;
493 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
494 if (zpadlen < 0) zpadlen = 0;
495 if (spadlen < 0) spadlen = 0;
496 if (flags & DP_F_ZERO) {
497 zpadlen = MAX(zpadlen, spadlen);
500 if (flags & DP_F_MINUS)
501 spadlen = -spadlen; /* Left Justifty */
503 #ifdef DEBUG_SNPRINTF
504 printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
505 zpadlen, spadlen, min, max, place);
509 while (spadlen > 0) {
510 dopr_outch (buffer, currlen, maxlen, ' ');
516 dopr_outch (buffer, currlen, maxlen, signvalue);
520 while (zpadlen > 0) {
521 dopr_outch (buffer, currlen, maxlen, '0');
528 dopr_outch (buffer, currlen, maxlen, convert[--place]);
530 /* Left Justified spaces */
531 while (spadlen < 0) {
532 dopr_outch (buffer, currlen, maxlen, ' ');
538 abs_val(LDOUBLE value)
540 LDOUBLE result = value;
566 intpart = (LLONG)value;
567 value = value - intpart;
568 if (value >= 0.5) intpart++;
573 /* a replacement for modf that doesn't need the math library. Should
574 be portable, but slow */
576 my_modf(double x0, double *iptr)
583 for (i=0;i<100;i++) {
585 if (l <= (x+1) && l >= (x-1)) break;
591 /* yikes! the number is beyond what we can handle. What do we do? */
600 ret = my_modf(x0-l*f, &i2);
611 fmtfp (char *buffer, size_t *currlen, size_t maxlen, LDOUBLE fvalue,
612 int min, int max, int flags)
620 int padlen = 0; /* amount to pad */
629 * AIX manpage says the default is 0, but Solaris says the default
630 * is 6, and sprintf on AIX defaults to 6
635 ufvalue = abs_val (fvalue);
640 if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
643 if (flags & DP_F_SPACE)
649 * Sorry, we only support 16 digits past the decimal because of our
655 /* We "cheat" by converting the fractional part to integer by
656 * multiplying by a factor of 10
660 my_modf(temp, &intpart);
662 fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
664 if (fracpart >= POW10(max)) {
666 fracpart -= POW10(max);
670 /* Convert integer part */
673 my_modf(intpart*0.1, &intpart);
675 index = (int) ((temp -intpart +0.05)* 10.0);
676 /* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
677 /* printf ("%llf, %f, %x\n", temp, intpart, index); */
679 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
680 } while (intpart && (iplace < 311));
681 if (iplace == 311) iplace--;
682 iconvert[iplace] = 0;
684 /* Convert fractional part */
689 my_modf(fracpart*0.1, &fracpart);
691 index = (int) ((temp -fracpart +0.05)* 10.0);
692 /* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
693 /* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
695 (caps? "0123456789ABCDEF":"0123456789abcdef")[index];
696 } while(fracpart && (fplace < 311));
697 if (fplace == 311) fplace--;
699 fconvert[fplace] = 0;
701 /* -1 for decimal point, another -1 if we are printing a sign */
702 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
703 zpadlen = max - fplace;
704 if (zpadlen < 0) zpadlen = 0;
707 if (flags & DP_F_MINUS)
708 padlen = -padlen; /* Left Justifty */
710 if ((flags & DP_F_ZERO) && (padlen > 0)) {
712 dopr_outch (buffer, currlen, maxlen, signvalue);
717 dopr_outch (buffer, currlen, maxlen, '0');
722 dopr_outch (buffer, currlen, maxlen, ' ');
726 dopr_outch (buffer, currlen, maxlen, signvalue);
729 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
731 #ifdef DEBUG_SNPRINTF
732 printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
736 * Decimal point. This should probably use locale to find the correct
740 dopr_outch (buffer, currlen, maxlen, '.');
743 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
746 while (zpadlen > 0) {
747 dopr_outch (buffer, currlen, maxlen, '0');
752 dopr_outch (buffer, currlen, maxlen, ' ');
758 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)
768 vsnprintf (char *str, size_t count, const char *fmt, va_list args)
770 return dopr(str, count, fmt, args);
774 #if !defined(HAVE_SNPRINTF)
777 snprintf(char *str, size_t count, const char *fmt, ...)
780 snprintf(str, count, fmt, va_alist)
791 ret = vsnprintf(str, count, fmt, ap);