2
* Copyright Patrick Powell 1995
3
* This code is based on code written by Patrick Powell (papowell@astart.com)
4
* It may be used for any purpose as long as this notice remains intact
5
* on all source code distributions
8
/**************************************************************
10
* Patrick Powell Tue Apr 11 09:48:21 PDT 1995
11
* A bombproof version of doprnt (dopr) included.
12
* Sigh. This sort of thing is always nasty do deal with. Note that
13
* the version here does not include floating point...
15
* snprintf() is used instead of sprintf() as it does limit checks
16
* for string length. This covers a nasty loophole.
18
* The other functions are there to prevent NULL pointers from
19
* causing nast effects.
22
* Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
23
* This was ugly. It is still ugly. I opted out of floating point
24
* numbers, but the formatter understands just about everything
25
* from the normal C string format, at least as far as I can tell from
26
* the Solaris 2.5 printf(3S) man page.
28
* Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
29
* Ok, added some minimal floating point support, which means this
30
* probably requires libm on most operating systems. Don't yet
31
* support the exponent (e,E) and sigfig (g,G). Also, fmtint()
32
* was pretty badly broken, it just wasn't being exercised in ways
33
* which showed it, so that's been fixed. Also, formated the code
34
* to mutt conventions, and removed dead code left over from the
35
* original. Also, there is now a builtin-test, just compile with:
36
* gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
37
* and run snprintf for results.
39
* Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
40
* The PGP code was using unsigned hexadecimal formats.
41
* Unfortunately, unsigned formats simply didn't work.
43
* Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
44
* The original code assumed that both snprintf() and vsnprintf() were
45
* missing. Some systems only have snprintf() but not vsnprintf(), so
46
* the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
48
* Andrew Tridgell (tridge@samba.org) Oct 1998
49
* fixed handling of %.0f
50
* added test for HAVE_LONG_DOUBLE
52
* tridge@samba.org, idra@samba.org, April 2001
53
* got rid of fcvt code (twas buggy and made testing harder)
56
* date: 2002/12/19 19:56:31; author: herb; state: Exp; lines: +2 -0
57
* actually print args for %g and %e
59
* date: 2002/06/03 13:37:52; author: jmcd; state: Exp; lines: +8 -0
60
* Since includes.h isn't included here, VA_COPY has to be defined here. I don't
61
* see any include file that is guaranteed to be here, so I'm defining it
62
* locally. Fixes AIX and Solaris builds.
64
* date: 2002/06/03 03:07:24; author: tridge; state: Exp; lines: +5 -13
65
* put the ifdef for HAVE_VA_COPY in one place rather than in lots of
68
* date: 2002/05/17 14:51:22; author: jmcd; state: Exp; lines: +21 -4
69
* Fix usage of va_list passed as an arg. Use __va_copy before using it
72
* date: 2002/04/16 22:38:04; author: idra; state: Exp; lines: +20 -14
73
* Fix incorrect zpadlen handling in fmtfp.
74
* Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it.
75
* few mods to make it easier to compile the tests.
76
* addedd the "Ollie" test to the floating point ones.
78
* Martin Pool (mbp@samba.org) April 2003
79
* Remove NO_CONFIG_H so that the test case can be built within a source
80
* tree with less trouble.
81
* Remove unnecessary SAFE_FREE() definition.
83
* Martin Pool (mbp@samba.org) May 2003
84
* Put in a prototype for dummy_snprintf() to quiet compiler warnings.
86
* Move #endif to make sure VA_COPY, LDOUBLE, etc are defined even
87
* if the C library has some snprintf functions already.
88
**************************************************************/
92
#if defined(BROKEN_SNPRINTF) /* For those with broken snprintf() */
94
# undef HAVE_VSNPRINTF
99
# define VA_COPY(dest, src) va_copy(dest, src)
101
# ifdef HAVE___VA_COPY
102
# define VA_COPY(dest, src) __va_copy(dest, src)
104
# define VA_COPY(dest, src) (dest) = (src)
109
#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
116
#ifdef HAVE_LONG_DOUBLE
117
# define LDOUBLE long double
119
# define LDOUBLE double
122
#ifdef HAVE_LONG_LONG
123
# define LLONG long long
129
* dopr(): poor man's version of doprintf
132
/* format read states */
133
#define DP_S_DEFAULT 0
142
/* format flags - Bits */
143
#define DP_F_MINUS (1 << 0)
144
#define DP_F_PLUS (1 << 1)
145
#define DP_F_SPACE (1 << 2)
146
#define DP_F_NUM (1 << 3)
147
#define DP_F_ZERO (1 << 4)
148
#define DP_F_UP (1 << 5)
149
#define DP_F_UNSIGNED (1 << 6)
151
/* Conversion Flags */
154
#define DP_C_LDOUBLE 3
157
#define char_to_int(p) ((p)- '0')
159
# define MAX(p,q) (((p) >= (q)) ? (p) : (q))
162
static size_t dopr(char *buffer, size_t maxlen, const char *format,
164
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
165
char *value, int flags, int min, int max);
166
static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
167
LLONG value, int base, int min, int max, int flags);
168
static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
169
LDOUBLE fvalue, int min, int max, int flags);
170
static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
172
static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
186
VA_COPY(args, args_in);
188
state = DP_S_DEFAULT;
189
currlen = flags = cflags = min = 0;
193
while (state != DP_S_DONE) {
202
dopr_outch (buffer, &currlen, maxlen, ch);
233
if (isdigit((unsigned char)ch)) {
234
min = 10*min + char_to_int (ch);
236
} else if (ch == '*') {
237
min = va_arg (args, int);
253
if (isdigit((unsigned char)ch)) {
256
max = 10*max + char_to_int (ch);
258
} else if (ch == '*') {
259
max = va_arg (args, int);
275
if (ch == 'l') { /* It's a long long */
281
cflags = DP_C_LDOUBLE;
293
if (cflags == DP_C_SHORT)
294
value = va_arg (args, int);
295
else if (cflags == DP_C_LONG)
296
value = va_arg (args, long int);
297
else if (cflags == DP_C_LLONG)
298
value = va_arg (args, LLONG);
300
value = va_arg (args, int);
301
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
304
flags |= DP_F_UNSIGNED;
305
if (cflags == DP_C_SHORT)
306
value = va_arg (args, unsigned int);
307
else if (cflags == DP_C_LONG)
308
value = (long)va_arg (args, unsigned long int);
309
else if (cflags == DP_C_LLONG)
310
value = (long)va_arg (args, unsigned LLONG);
312
value = (long)va_arg (args, unsigned int);
313
fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
316
flags |= DP_F_UNSIGNED;
317
if (cflags == DP_C_SHORT)
318
value = va_arg (args, unsigned int);
319
else if (cflags == DP_C_LONG)
320
value = (long)va_arg (args, unsigned long int);
321
else if (cflags == DP_C_LLONG)
322
value = (LLONG)va_arg (args, unsigned LLONG);
324
value = (long)va_arg (args, unsigned int);
325
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
330
flags |= DP_F_UNSIGNED;
331
if (cflags == DP_C_SHORT)
332
value = va_arg (args, unsigned int);
333
else if (cflags == DP_C_LONG)
334
value = (long)va_arg (args, unsigned long int);
335
else if (cflags == DP_C_LLONG)
336
value = (LLONG)va_arg (args, unsigned LLONG);
338
value = (long)va_arg (args, unsigned int);
339
fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
342
if (cflags == DP_C_LDOUBLE)
343
fvalue = va_arg (args, LDOUBLE);
345
fvalue = va_arg (args, double);
346
/* um, floating point? */
347
fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
352
if (cflags == DP_C_LDOUBLE)
353
fvalue = va_arg (args, LDOUBLE);
355
fvalue = va_arg (args, double);
356
fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
361
if (cflags == DP_C_LDOUBLE)
362
fvalue = va_arg (args, LDOUBLE);
364
fvalue = va_arg (args, double);
365
fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
368
dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
371
strvalue = va_arg (args, char *);
372
if (!strvalue) strvalue = "(NULL)";
374
max = strlen(strvalue);
376
if (min > 0 && max >= 0 && min > max) max = min;
377
fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
380
strvalue = va_arg (args, void *);
381
fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
384
if (cflags == DP_C_SHORT) {
386
num = va_arg (args, short int *);
388
} else if (cflags == DP_C_LONG) {
390
num = va_arg (args, long int *);
391
*num = (long int)currlen;
392
} else if (cflags == DP_C_LLONG) {
394
num = va_arg (args, LLONG *);
395
*num = (LLONG)currlen;
398
num = va_arg (args, int *);
403
dopr_outch (buffer, &currlen, maxlen, ch);
406
/* not supported yet, treat as next char */
414
state = DP_S_DEFAULT;
415
flags = cflags = min = 0;
422
break; /* some picky compilers need this */
426
if (currlen < maxlen - 1)
427
buffer[currlen] = '\0';
429
buffer[maxlen - 1] = '\0';
435
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
436
char *value, int flags, int min, int max)
438
int padlen, strln; /* amount to pad */
441
#ifdef DEBUG_SNPRINTF
442
printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
448
for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
449
padlen = min - strln;
452
if (flags & DP_F_MINUS)
453
padlen = -padlen; /* Left Justify */
455
while ((padlen > 0) && (cnt < max)) {
456
dopr_outch (buffer, currlen, maxlen, ' ');
460
while (*value && (cnt < max)) {
461
dopr_outch (buffer, currlen, maxlen, *value++);
464
while ((padlen < 0) && (cnt < max)) {
465
dopr_outch (buffer, currlen, maxlen, ' ');
471
/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
473
static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
474
LLONG value, int base, int min, int max, int flags)
477
unsigned LLONG uvalue;
480
int spadlen = 0; /* amount to space pad */
481
int zpadlen = 0; /* amount to zero pad */
489
if(!(flags & DP_F_UNSIGNED)) {
494
if (flags & DP_F_PLUS) /* Do a sign (+/i) */
496
else if (flags & DP_F_SPACE)
501
if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
505
(caps? "0123456789ABCDEF":"0123456789abcdef")
506
[uvalue % (unsigned)base ];
507
uvalue = (uvalue / (unsigned)base );
508
} while(uvalue && (place < 20));
509
if (place == 20) place--;
512
zpadlen = max - place;
513
spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
514
if (zpadlen < 0) zpadlen = 0;
515
if (spadlen < 0) spadlen = 0;
516
if (flags & DP_F_ZERO) {
517
zpadlen = MAX(zpadlen, spadlen);
520
if (flags & DP_F_MINUS)
521
spadlen = -spadlen; /* Left Justifty */
523
#ifdef DEBUG_SNPRINTF
524
printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
525
zpadlen, spadlen, min, max, place);
529
while (spadlen > 0) {
530
dopr_outch (buffer, currlen, maxlen, ' ');
536
dopr_outch (buffer, currlen, maxlen, signvalue);
540
while (zpadlen > 0) {
541
dopr_outch (buffer, currlen, maxlen, '0');
548
dopr_outch (buffer, currlen, maxlen, convert[--place]);
550
/* Left Justified spaces */
551
while (spadlen < 0) {
552
dopr_outch (buffer, currlen, maxlen, ' ');
557
static LDOUBLE abs_val(LDOUBLE value)
559
LDOUBLE result = value;
567
static LDOUBLE POW10(int exp)
579
static LLONG ROUND(LDOUBLE value)
583
intpart = (LLONG)value;
584
value = value - intpart;
585
if (value >= 0.5) intpart++;
590
/* a replacement for modf that doesn't need the math library. Should
591
be portable, but slow */
592
static double my_modf(double x0, double *iptr)
599
for (i=0;i<100;i++) {
601
if (l <= (x+1) && l >= (x-1)) break;
607
/* yikes! the number is beyond what we can handle. What do we do? */
616
ret = my_modf(x0-l*f, &i2);
626
static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
627
LDOUBLE fvalue, int min, int max, int flags)
635
int padlen = 0; /* amount to pad */
644
* AIX manpage says the default is 0, but Solaris says the default
645
* is 6, and sprintf on AIX defaults to 6
650
ufvalue = abs_val (fvalue);
655
if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
658
if (flags & DP_F_SPACE)
664
if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
668
if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
672
* Sorry, we only support 16 digits past the decimal because of our
678
/* We "cheat" by converting the fractional part to integer by
679
* multiplying by a factor of 10
683
my_modf(temp, &intpart);
685
fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
687
if (fracpart >= POW10(max)) {
689
fracpart -= POW10(max);
692
/* Convert integer part */
695
my_modf(temp, &intpart);
696
idx = (int) ((temp -intpart +0.05)* 10.0);
697
/* idx = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
698
/* printf ("%llf, %f, %x\n", temp, intpart, idx); */
700
(caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
701
} while (intpart && (iplace < 311));
702
if (iplace == 311) iplace--;
703
iconvert[iplace] = 0;
705
/* Convert fractional part */
710
my_modf(temp, &fracpart);
711
idx = (int) ((temp -fracpart +0.05)* 10.0);
712
/* idx = (int) ((((temp/10) -fracpart) +0.05) *10); */
713
/* printf ("%lf, %lf, %ld\n", temp, fracpart, idx ); */
715
(caps? "0123456789ABCDEF":"0123456789abcdef")[idx];
716
} while(fracpart && (fplace < 311));
717
if (fplace == 311) fplace--;
719
fconvert[fplace] = 0;
721
/* -1 for decimal point, another -1 if we are printing a sign */
722
padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
723
zpadlen = max - fplace;
724
if (zpadlen < 0) zpadlen = 0;
727
if (flags & DP_F_MINUS)
728
padlen = -padlen; /* Left Justifty */
730
if ((flags & DP_F_ZERO) && (padlen > 0)) {
732
dopr_outch (buffer, currlen, maxlen, signvalue);
737
dopr_outch (buffer, currlen, maxlen, '0');
742
dopr_outch (buffer, currlen, maxlen, ' ');
746
dopr_outch (buffer, currlen, maxlen, signvalue);
749
dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
751
#ifdef DEBUG_SNPRINTF
752
printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
756
* Decimal point. This should probably use locale to find the correct
760
dopr_outch (buffer, currlen, maxlen, '.');
762
while (zpadlen > 0) {
763
dopr_outch (buffer, currlen, maxlen, '0');
768
dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
772
dopr_outch (buffer, currlen, maxlen, ' ');
777
static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
779
if (*currlen < maxlen) {
780
buffer[(*currlen)] = c;
784
#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
786
#if !defined(HAVE_VSNPRINTF)
787
int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
789
return dopr(str, count, fmt, args);
793
#if !defined(HAVE_SNPRINTF)
794
int snprintf(char *str, size_t count, SNPRINTF_CONST char *fmt, ...)
800
ret = vsnprintf(str, count, fmt, ap);