* original. Also, there is now a builtin-test, just compile with:
* gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
* and run snprintf for results.
- *
+ *
* Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
- * The PGP code was using unsigned hexadecimal formats.
+ * The PGP code was using unsigned hexadecimal formats.
* Unfortunately, unsigned formats simply didn't work.
*
* Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
*
* date: 2002/12/19 19:56:31; author: herb; state: Exp; lines: +2 -0
* actually print args for %g and %e
- *
+ *
* date: 2002/06/03 13:37:52; author: jmcd; state: Exp; lines: +8 -0
* Since includes.h isn't included here, VA_COPY has to be defined here. I don't
* see any include file that is guaranteed to be here, so I'm defining it
* locally. Fixes AIX and Solaris builds.
- *
+ *
* date: 2002/06/03 03:07:24; author: tridge; state: Exp; lines: +5 -13
* put the ifdef for HAVE_VA_COPY in one place rather than in lots of
* functions
- *
+ *
* date: 2002/05/17 14:51:22; author: jmcd; state: Exp; lines: +21 -4
* Fix usage of va_list passed as an arg. Use __va_copy before using it
* when it exists.
- *
+ *
* date: 2002/04/16 22:38:04; author: idra; state: Exp; lines: +20 -14
* Fix incorrect zpadlen handling in fmtfp.
* Thanks to Ollie Oldham <ollie.oldham@metro-optix.com> for spotting it.
* From OpenSSH. Also added test for same.
*
* Simo Sorce (idra@samba.org) Jan 2006
- *
- * Add support for position independent parameters
+ *
+ * Add support for position independent parameters
* fix fmtstr now it conforms to sprintf wrt min.max
*
**************************************************************/
#include <stdio.h>
/* make the compiler happy with an empty file */
void dummy_snprintf(void);
- void dummy_snprintf(void) {}
+ void dummy_snprintf(void) {}
#endif /* HAVE_SNPRINTF, etc */
/* yes this really must be a ||. Don't muck with this (tridge) */
#define DP_S_DONE 7
/* format flags - Bits */
-#define DP_F_MINUS (1 << 0)
-#define DP_F_PLUS (1 << 1)
-#define DP_F_SPACE (1 << 2)
-#define DP_F_NUM (1 << 3)
-#define DP_F_ZERO (1 << 4)
-#define DP_F_UP (1 << 5)
-#define DP_F_UNSIGNED (1 << 6)
+#define DP_F_MINUS (1 << 0)
+#define DP_F_PLUS (1 << 1)
+#define DP_F_SPACE (1 << 2)
+#define DP_F_NUM (1 << 3)
+#define DP_F_ZERO (1 << 4)
+#define DP_F_UP (1 << 5)
+#define DP_F_UNSIGNED (1 << 6)
/* Conversion Flags */
#define DP_C_CHAR 1
struct pr_chunk {
int type; /* chunk type */
int num; /* parameter number */
- int min;
+ int min;
int max;
int flags;
int cflags;
int num;
};
-static int dopr(char *buffer, size_t maxlen, const char *format,
+static int dopr(char *buffer, size_t maxlen, const char *format,
va_list args_in);
static void fmtstr(char *buffer, size_t *currlen, size_t maxlen,
char *value, int flags, int min, int max);
max_pos = 0;
base = format;
ch = *format++;
-
+
/* retrieve the string structure as chunks */
while (state != DP_S_DONE) {
- if (ch == '\0')
+ if (ch == '\0')
state = DP_S_DONE;
switch(state) {
case DP_S_DEFAULT:
-
+
if (cnk) {
cnk->next = new_chunk();
cnk = cnk->next;
}
if (!cnk) goto done;
if (!chunks) chunks = cnk;
-
+
if (ch == '%') {
state = DP_S_FLAGS;
ch = *format++;
if (ch == '.') {
state = DP_S_MAX;
ch = *format++;
- } else {
+ } else {
state = DP_S_MOD;
}
break;
max_pos = add_cnk_list_entry(&clist, max_pos, cnk);
if (max_pos == 0) /* out of memory :-( */
goto done;
-
+
switch (ch) {
case 'd':
case 'i':
cnk = clist[pnum].chunks[0];
switch (cnk->type) {
case CNK_INT:
- if (cnk->cflags == DP_C_SHORT)
+ if (cnk->cflags == DP_C_SHORT)
cnk->value = va_arg (args, int);
else if (cnk->cflags == DP_C_LONG)
cnk->value = va_arg (args, long int);
memcpy(&(buffer[currlen]), &(base[cnk->start]), len);
}
currlen += cnk->len;
-
+
break;
case CNK_INT:
cnk = cnk->next;
}
if (maxlen != 0) {
- if (currlen < maxlen - 1)
+ if (currlen < maxlen - 1)
buffer[currlen] = '\0';
- else if (maxlen > 0)
+ else if (maxlen > 0)
buffer[maxlen - 1] = '\0';
}
ret = currlen;
for (strln = 0; strln < max && value[strln]; ++strln); /* strlen */
padlen = min - strln;
- if (padlen < 0)
+ if (padlen < 0)
padlen = 0;
- if (flags & DP_F_MINUS)
+ if (flags & DP_F_MINUS)
padlen = -padlen; /* Left Justify */
-
+
while (padlen > 0) {
dopr_outch (buffer, currlen, maxlen, ' ');
--padlen;
int spadlen = 0; /* amount to space pad */
int zpadlen = 0; /* amount to zero pad */
int caps = 0;
-
+
if (max < 0)
max = 0;
-
+
uvalue = value;
-
+
if(!(flags & DP_F_UNSIGNED)) {
if( value < 0 ) {
signvalue = '-';
signvalue = ' ';
}
}
-
+
if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
do {
zpadlen = MAX(zpadlen, spadlen);
spadlen = 0;
}
- if (flags & DP_F_MINUS)
+ if (flags & DP_F_MINUS)
spadlen = -spadlen; /* Left Justifty */
#ifdef DEBUG_SNPRINTF
}
/* Sign */
- if (signvalue)
+ if (signvalue)
dopr_outch (buffer, currlen, maxlen, signvalue);
/* Zeros */
}
/* Digits */
- while (place > 0)
+ while (place > 0)
dopr_outch (buffer, currlen, maxlen, convert[--place]);
-
+
/* Left Justified spaces */
while (spadlen < 0) {
dopr_outch (buffer, currlen, maxlen, ' ');
if (value < 0)
result = -value;
-
+
return result;
}
static LDOUBLE POW10(int exp)
{
LDOUBLE result = 1;
-
+
while (exp) {
result *= 10;
exp--;
}
-
+
return result;
}
intpart = (LLONG)value;
value = value - intpart;
if (value >= 0.5) intpart++;
-
+
return intpart;
}
ret = my_modf(x0-l*f, &i2);
(*iptr) = l*f + i2;
return ret;
- }
+ }
(*iptr) = l;
return x - (*iptr);
int iplace = 0;
int fplace = 0;
int padlen = 0; /* amount to pad */
- int zpadlen = 0;
+ int zpadlen = 0;
int caps = 0;
int idx;
double intpart;
double fracpart;
double temp;
-
- /*
+
+ /*
* AIX manpage says the default is 0, but Solaris says the default
* is 6, and sprintf on AIX defaults to 6
*/
if (max == 0) ufvalue += 0.5; /* if max = 0 we must round */
#endif
- /*
- * Sorry, we only support 9 digits past the decimal because of our
+ /*
+ * Sorry, we only support 9 digits past the decimal because of our
* conversion method
*/
if (max > 9)
my_modf(temp, &intpart);
fracpart = ROUND((POW10(max)) * (ufvalue - intpart));
-
+
if (fracpart >= POW10(max)) {
intpart++;
fracpart -= POW10(max);
if (fplace == 311) fplace--;
}
fconvert[fplace] = 0;
-
+
/* -1 for decimal point, another -1 if we are printing a sign */
- padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
+ padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
zpadlen = max - fplace;
if (zpadlen < 0) zpadlen = 0;
- if (padlen < 0)
+ if (padlen < 0)
padlen = 0;
- if (flags & DP_F_MINUS)
+ if (flags & DP_F_MINUS)
padlen = -padlen; /* Left Justifty */
-
+
if ((flags & DP_F_ZERO) && (padlen > 0)) {
if (signvalue) {
dopr_outch (buffer, currlen, maxlen, signvalue);
dopr_outch (buffer, currlen, maxlen, ' ');
--padlen;
}
- if (signvalue)
+ if (signvalue)
dopr_outch (buffer, currlen, maxlen, signvalue);
-
- while (iplace > 0)
+
+ while (iplace > 0)
dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
#ifdef DEBUG_SNPRINTF
*/
if (max > 0) {
dopr_outch (buffer, currlen, maxlen, '.');
-
+
while (zpadlen > 0) {
dopr_outch (buffer, currlen, maxlen, '0');
--zpadlen;
}
- while (fplace > 0)
+ while (fplace > 0)
dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
}
if (chunk->num > max_num) {
max = chunk->num;
-
+
if (*list == NULL) {
l = (struct pr_chunk_x *)malloc(sizeof(struct pr_chunk_x) * max);
pos = 0;
i = chunk->num - 1;
cnum = l[i].num + 1;
if (l[i].chunks == NULL) {
- c = (struct pr_chunk **)malloc(sizeof(struct pr_chunk *) * cnum);
+ c = (struct pr_chunk **)malloc(sizeof(struct pr_chunk *) * cnum);
} else {
c = (struct pr_chunk **)realloc(l[i].chunks, sizeof(struct pr_chunk *) * cnum);
}
{
size_t ret;
va_list ap;
-
+
va_start(ap, fmt);
ret = vsnprintf(str, count, fmt, ap);
va_end(ap);
}
#endif
-#endif
+#endif
#if !defined(HAVE_VASPRINTF) || !defined(HAVE_C99_VSNPRINTF)
int rep_vasprintf(char **ptr, const char *format, va_list ap)
{
va_list ap;
int ret;
-
+
*ptr = NULL;
va_start(ap, format);
ret = vasprintf(ptr, format, ap);
"%-9.9f",
NULL
};
- double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996,
+ double fp_nums[] = { 6442452944.1234, -1.5, 134.21, 91340.2, 341.1234, 203.9, 0.96, 0.996,
0.9996, 1.996, 4.136, 5.030201, 0.00205,
/* END LIST */ 0};
char *int_fmt[] = {
l2 = sprintf (buf2, fp_fmt[x], fp_nums[y]);
buf1[1023] = buf2[1023] = '\0';
if (strcmp (buf1, buf2) || (l1 != l2)) {
- printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
+ printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
fp_fmt[x], l1, buf1, l2, buf2);
fail++;
}
l2 = sprintf (buf2, int_fmt[x], int_nums[y]);
buf1[1023] = buf2[1023] = '\0';
if (strcmp (buf1, buf2) || (l1 != l2)) {
- printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
+ printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
int_fmt[x], l1, buf1, l2, buf2);
fail++;
}
l2 = sprintf (buf2, str_fmt[x], str_vals[y]);
buf1[1023] = buf2[1023] = '\0';
if (strcmp (buf1, buf2) || (l1 != l2)) {
- printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
+ printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
str_fmt[x], l1, buf1, l2, buf2);
fail++;
}
l2 = sprintf (buf2, ll_fmt[x], ll_nums[y]);
buf1[1023] = buf2[1023] = '\0';
if (strcmp (buf1, buf2) || (l1 != l2)) {
- printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
+ printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
ll_fmt[x], l1, buf1, l2, buf2);
fail++;
}
l2 = sprintf (buf2, ss_fmt[x], ss_nums[y]);
buf1[1023] = buf2[1023] = '\0';
if (strcmp (buf1, buf2) || (l1 != l2)) {
- printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
+ printf("snprintf doesn't match Format: %s\n\tsnprintf(%d) = [%s]\n\t sprintf(%d) = [%s]\n",
ss_fmt[x], l1, buf1, l2, buf2);
fail++;
}
{
double v0 = 0.12345678901234567890123456789012345678901;
for (x=0; x<100; x++) {
- double p = pow(10, x);
+ double p = pow(10, x);
double r = v0*p;
snprintf(buf1, sizeof(buf1), "%1.1f", r);
sprintf(buf2, "%1.1f", r);