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
**************************************************************/
58
#ifndef NO_CONFIG_H /* for some tests */
64
#ifdef TEST_SNPRINTF /* need math library headers for testing */
78
#ifdef HAVE_SYS_TYPES_H
79
#include <sys/types.h>
88
#if defined(HAVE_SNPRINTF) && defined(HAVE_VSNPRINTF) && defined(HAVE_C99_VSNPRINTF)
89
/* only include stdio.h if we are not re-defining snprintf or vsnprintf */
91
/* make the compiler happy with an empty file */
92
void dummy_snprintf(void) {}
95
#ifdef HAVE_LONG_DOUBLE
96
#define LDOUBLE long double
98
#define LDOUBLE double
101
#ifdef HAVE_LONG_LONG
102
#define LLONG long long
107
/* free memory if the pointer is valid and zero the pointer */
109
#define SAFE_FREE(x) do { if ((x) != NULL) {free((x)); (x)=NULL;} } while(0)
114
#define VA_COPY(dest, src) __va_copy(dest, src)
116
#define VA_COPY(dest, src) (dest) = (src)
120
static size_t dopr(char *buffer, size_t maxlen, const char *format,
122
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
123
char *value, int flags, int min, int max);
124
static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
125
long value, int base, int min, int max, int flags);
126
static void fmtfp(char *buffer, size_t *currlen, size_t maxlen,
127
LDOUBLE fvalue, int min, int max, int flags);
128
static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
131
* dopr(): poor man's version of doprintf
134
/* format read states */
135
#define DP_S_DEFAULT 0
144
/* format flags - Bits */
145
#define DP_F_MINUS (1 << 0)
146
#define DP_F_PLUS (1 << 1)
147
#define DP_F_SPACE (1 << 2)
148
#define DP_F_NUM (1 << 3)
149
#define DP_F_ZERO (1 << 4)
150
#define DP_F_UP (1 << 5)
151
#define DP_F_UNSIGNED (1 << 6)
153
/* Conversion Flags */
156
#define DP_C_LDOUBLE 3
159
#define char_to_int(p) ((p)- '0')
161
#define MAX(p,q) (((p) >= (q)) ? (p) : (q))
164
static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
178
VA_COPY(args, args_in);
180
state = DP_S_DEFAULT;
181
currlen = flags = cflags = min = 0;
185
while (state != DP_S_DONE) {
194
dopr_outch (buffer, &currlen, maxlen, ch);
225
if (isdigit((unsigned char)ch)) {
226
min = 10*min + char_to_int (ch);
228
} else if (ch == '*') {
229
min = va_arg (args, int);
245
if (isdigit((unsigned char)ch)) {
248
max = 10*max + char_to_int (ch);
250
} else if (ch == '*') {
251
max = va_arg (args, int);
267
if (ch == 'l') { /* It's a long long */
273
cflags = DP_C_LDOUBLE;
285
if (cflags == DP_C_SHORT)
286
value = va_arg (args, int);
287
else if (cflags == DP_C_LONG)
288
value = va_arg (args, long int);
289
else if (cflags == DP_C_LLONG)
290
value = va_arg (args, LLONG);
292
value = va_arg (args, int);
293
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
296
flags |= DP_F_UNSIGNED;
297
if (cflags == DP_C_SHORT)
298
value = va_arg (args, unsigned int);
299
else if (cflags == DP_C_LONG)
300
value = (long)va_arg (args, unsigned long int);
301
else if (cflags == DP_C_LLONG)
302
value = (long)va_arg (args, unsigned LLONG);
304
value = (long)va_arg (args, unsigned int);
305
fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
308
flags |= DP_F_UNSIGNED;
309
if (cflags == DP_C_SHORT)
310
value = va_arg (args, unsigned int);
311
else if (cflags == DP_C_LONG)
312
value = (long)va_arg (args, unsigned long int);
313
else if (cflags == DP_C_LLONG)
314
value = (LLONG)va_arg (args, unsigned LLONG);
316
value = (long)va_arg (args, unsigned int);
317
fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
322
flags |= DP_F_UNSIGNED;
323
if (cflags == DP_C_SHORT)
324
value = va_arg (args, unsigned int);
325
else if (cflags == DP_C_LONG)
326
value = (long)va_arg (args, unsigned long int);
327
else if (cflags == DP_C_LLONG)
328
value = (LLONG)va_arg (args, unsigned LLONG);
330
value = (long)va_arg (args, unsigned int);
331
fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
334
if (cflags == DP_C_LDOUBLE)
335
fvalue = va_arg (args, LDOUBLE);
337
fvalue = va_arg (args, double);
338
/* um, floating point? */
339
fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
344
if (cflags == DP_C_LDOUBLE)
345
fvalue = va_arg (args, LDOUBLE);
347
fvalue = va_arg (args, double);
352
if (cflags == DP_C_LDOUBLE)
353
fvalue = va_arg (args, LDOUBLE);
355
fvalue = va_arg (args, double);
358
dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
361
strvalue = va_arg (args, char *);
362
if (!strvalue) strvalue = "(NULL)";
364
max = strlen(strvalue);
366
if (min > 0 && max >= 0 && min > max) max = min;
367
fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
370
strvalue = va_arg (args, void *);
371
fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
374
if (cflags == DP_C_SHORT) {
376
num = va_arg (args, short int *);
378
} else if (cflags == DP_C_LONG) {
380
num = va_arg (args, long int *);
381
*num = (long int)currlen;
382
} else if (cflags == DP_C_LLONG) {
384
num = va_arg (args, LLONG *);
385
*num = (LLONG)currlen;
388
num = va_arg (args, int *);
393
dopr_outch (buffer, &currlen, maxlen, ch);
396
/* not supported yet, treat as next char */
404
state = DP_S_DEFAULT;
405
flags = cflags = min = 0;
412
break; /* some picky compilers need this */
416
if (currlen < maxlen - 1)
417
buffer[currlen] = '\0';
419
buffer[maxlen - 1] = '\0';
425
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
426
char *value, int flags, int min, int max)
428
int padlen, strln; /* amount to pad */
431
#ifdef DEBUG_SNPRINTF
432
printf("fmtstr min=%d max=%d s=[%s]\n", min, max, value);
438
for (strln = 0; value[strln]; ++strln); /* strlen */
439
padlen = min - strln;
442
if (flags & DP_F_MINUS)
443
padlen = -padlen; /* Left Justify */
445
while ((padlen > 0) && (cnt < max)) {
446
dopr_outch (buffer, currlen, maxlen, ' ');
450
while (*value && (cnt < max)) {
451
dopr_outch (buffer, currlen, maxlen, *value++);
454
while ((padlen < 0) && (cnt < max)) {
455
dopr_outch (buffer, currlen, maxlen, ' ');
461
/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
463
static void fmtint(char *buffer, size_t *currlen, size_t maxlen,
464
long value, int base, int min, int max, int flags)
467
unsigned long uvalue;
470
int spadlen = 0; /* amount to space pad */
471
int zpadlen = 0; /* amount to zero pad */
479
if(!(flags & DP_F_UNSIGNED)) {
484
if (flags & DP_F_PLUS) /* Do a sign (+/i) */
486
else if (flags & DP_F_SPACE)
491
if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
495
(caps? "0123456789ABCDEF":"0123456789abcdef")
496
[uvalue % (unsigned)base ];
497
uvalue = (uvalue / (unsigned)base );
498
} while(uvalue && (place < 20));
499
if (place == 20) place--;
502
zpadlen = max - place;
503
spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
504
if (zpadlen < 0) zpadlen = 0;
505
if (spadlen < 0) spadlen = 0;
506
if (flags & DP_F_ZERO) {
507
zpadlen = MAX(zpadlen, spadlen);
510
if (flags & DP_F_MINUS)
511
spadlen = -spadlen; /* Left Justifty */
513
#ifdef DEBUG_SNPRINTF
514
printf("zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
515
zpadlen, spadlen, min, max, place);
519
while (spadlen > 0) {
520
dopr_outch (buffer, currlen, maxlen, ' ');
526
dopr_outch (buffer, currlen, maxlen, signvalue);
530
while (zpadlen > 0) {
531
dopr_outch (buffer, currlen, maxlen, '0');
538
dopr_outch (buffer, currlen, maxlen, convert[--place]);
540
/* Left Justified spaces */
541
while (spadlen < 0) {
542
dopr_outch (buffer, currlen, maxlen, ' ');
547
static LDOUBLE abs_val(LDOUBLE value)
549
LDOUBLE result = value;
557
static LDOUBLE POW10(int exp)
569
static LLONG ROUND(LDOUBLE value)
573
intpart = (LLONG)value;
574
value = value - intpart;
575
if (value >= 0.5) intpart++;
580
/* a replacement for modf that doesn't need the math library. Should
581
be portable, but slow */
582
static double my_modf(double x0, double *iptr)
589
for (i=0;i<100;i++) {
591
if (l <= (x+1) && l >= (x-1)) break;
597
/* yikes! the number is beyond what we can handle. What do we do? */
606
ret = my_modf(x0-l*f, &i2);
616
static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
617
LDOUBLE fvalue, int min, int max, int flags)
625
int padlen = 0; /* amount to pad */
634
* AIX manpage says the default is 0, but Solaris says the default
635
* is 6, and sprintf on AIX defaults to 6
640
ufvalue = abs_val (fvalue);
645
if (flags & DP_F_PLUS) { /* Do a sign (+/i) */
648
if (flags & DP_F_SPACE)
654
if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
658
if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
662
* Sorry, we only support 16 digits past the decimal because of our
668
/* We "cheat" by converting the fractional part to integer by
669
* multiplying by a factor of 10
673
my_modf(temp, &intpart);
675
fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
677
if (fracpart >= POW10(max)) {
679
fracpart -= POW10(max);
683
/* Convert integer part */
686
my_modf(temp, &intpart);
687
index = (int) ((temp -intpart +0.05)* 10.0);
688
/* index = (int) (((double)(temp*0.1) -intpart +0.05) *10.0); */
689
/* printf ("%llf, %f, %x\n", temp, intpart, index); */
691
(caps? "0123456789ABCDEF":"0123456789abcdef")[index];
692
} while (intpart && (iplace < 311));
693
if (iplace == 311) iplace--;
694
iconvert[iplace] = 0;
696
/* Convert fractional part */
701
my_modf(temp, &fracpart);
702
index = (int) ((temp -fracpart +0.05)* 10.0);
703
/* index = (int) ((((temp/10) -fracpart) +0.05) *10); */
704
/* printf ("%lf, %lf, %ld\n", temp, fracpart, index); */
706
(caps? "0123456789ABCDEF":"0123456789abcdef")[index];
707
} while(fracpart && (fplace < 311));
708
if (fplace == 311) fplace--;
710
fconvert[fplace] = 0;
712
/* -1 for decimal point, another -1 if we are printing a sign */
713
padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
714
zpadlen = max - fplace;
715
if (zpadlen < 0) zpadlen = 0;
718
if (flags & DP_F_MINUS)
719
padlen = -padlen; /* Left Justifty */
721
if ((flags & DP_F_ZERO) && (padlen > 0)) {
723
dopr_outch (buffer, currlen, maxlen, signvalue);
728
dopr_outch (buffer, currlen, maxlen, '0');
733
dopr_outch (buffer, currlen, maxlen, ' ');
737
dopr_outch (buffer, currlen, maxlen, signvalue);
740
dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
742
#ifdef DEBUG_SNPRINTF
743
printf("fmtfp: fplace=%d zpadlen=%d\n", fplace, zpadlen);
747
* Decimal point. This should probably use locale to find the correct
751
dopr_outch (buffer, currlen, maxlen, '.');
753
while (zpadlen > 0) {
754
dopr_outch (buffer, currlen, maxlen, '0');
759
dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
763
dopr_outch (buffer, currlen, maxlen, ' ');
768
static void dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
770
if (*currlen < maxlen) {
771
buffer[(*currlen)] = c;
776
/* yes this really must be a ||. Don't muck with this (tridge) */
777
#if !defined(HAVE_VSNPRINTF) || !defined(HAVE_C99_VSNPRINTF)
778
int vsnprintf (char *str, size_t count, const char *fmt, va_list args)
780
return dopr(str, count, fmt, args);
784
/* yes this really must be a ||. Don't muck wiith this (tridge)
786
* The logic for these two is that we need our own definition if the
787
* OS *either* has no definition of *sprintf, or if it does have one
788
* that doesn't work properly according to the autoconf test. Perhaps
789
* these should really be smb_snprintf to avoid conflicts with buggy
792
#if !defined(HAVE_SNPRINTF) /* || !defined(HAVE_C99_SNPRINTF) */
793
int snprintf(char *str,size_t count,const char *fmt,...)
799
ret = vsnprintf(str, count, fmt, ap);
807
#ifndef HAVE_VASPRINTF
808
int vasprintf(char **ptr, const char *format, va_list ap)
815
ret = vsnprintf(NULL, 0, format, ap2);
816
if (ret <= 0) return ret;
818
(*ptr) = (char *)malloc(ret+1);
819
if (!*ptr) return -1;
823
ret = vsnprintf(*ptr, ret+1, format, ap2);
830
#ifndef HAVE_ASPRINTF
831
int asprintf(char **ptr, const char *format, ...)
837
va_start(ap, format);
838
ret = vasprintf(ptr, format, ap);
847
int sprintf(char *str,const char *fmt,...);
871
double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996,
872
0.9996, 1.996, 4.136, 5.030201, 0};
886
long int_nums[] = { -1, 134, 91340, 341, 0203, 0};
900
char *str_vals[] = {"hello", "a", "", "a longer string", NULL};
905
printf ("Testing snprintf format codes against system sprintf...\n");
907
for (x = 0; fp_fmt[x] ; x++) {
908
for (y = 0; fp_nums[y] != 0 ; y++) {
909
int l1 = snprintf(NULL, 0, fp_fmt[x], fp_nums[y]);
910
int l2 = snprintf(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
911
sprintf (buf2, fp_fmt[x], fp_nums[y]);
912
if (strcmp (buf1, buf2)) {
913
printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
914
fp_fmt[x], buf1, buf2);
918
printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, fp_fmt[x]);
925
for (x = 0; int_fmt[x] ; x++) {
926
for (y = 0; int_nums[y] != 0 ; y++) {
927
int l1 = snprintf(NULL, 0, int_fmt[x], int_nums[y]);
928
int l2 = snprintf(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
929
sprintf (buf2, int_fmt[x], int_nums[y]);
930
if (strcmp (buf1, buf2)) {
931
printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
932
int_fmt[x], buf1, buf2);
936
printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, int_fmt[x]);
943
for (x = 0; str_fmt[x] ; x++) {
944
for (y = 0; str_vals[y] != 0 ; y++) {
945
int l1 = snprintf(NULL, 0, str_fmt[x], str_vals[y]);
946
int l2 = snprintf(buf1, sizeof(buf1), str_fmt[x], str_vals[y]);
947
sprintf (buf2, str_fmt[x], str_vals[y]);
948
if (strcmp (buf1, buf2)) {
949
printf("snprintf doesn't match Format: %s\n\tsnprintf = [%s]\n\t sprintf = [%s]\n",
950
str_fmt[x], buf1, buf2);
954
printf("snprintf l1 != l2 (%d %d) %s\n", l1, l2, str_fmt[x]);
961
printf ("%d tests failed out of %d.\n", fail, num);
963
printf("seeing how many digits we support\n");
965
double v0 = 0.12345678901234567890123456789012345678901;
966
for (x=0; x<100; x++) {
967
double p = pow(10, x);
969
snprintf(buf1, sizeof(buf1), "%1.1f", r);
970
sprintf(buf2, "%1.1f", r);
971
if (strcmp(buf1, buf2)) {
972
printf("we seem to support %d digits\n", x-1);
980
#endif /* SNPRINTF_TEST */