1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-01-08 12:44:23 +01:00

estream: Fix minor glitch in "%.*s" format.

* common/estream-printf.c (pr_string): Take care of non-nul terminated
strings.
--
Resolved conflicts:
	common/estream-printf.c - white spaces
This commit is contained in:
Werner Koch 2014-06-25 20:25:28 +02:00
parent 505f0a642f
commit 998f085297

View File

@ -52,7 +52,7 @@
#if defined(HAVE_INTMAX_T) || defined(HAVE_UINTMAX_T) #if defined(HAVE_INTMAX_T) || defined(HAVE_UINTMAX_T)
# ifdef HAVE_STDINT_H # ifdef HAVE_STDINT_H
# include <stdint.h> # include <stdint.h>
# endif # endif
#endif #endif
#ifdef HAVE_LANGINFO_THOUSANDS_SEP #ifdef HAVE_LANGINFO_THOUSANDS_SEP
#include <langinfo.h> #include <langinfo.h>
@ -67,12 +67,12 @@
/* Allow redefinition of asprintf used malloc functions. */ /* Allow redefinition of asprintf used malloc functions. */
#if defined(_ESTREAM_PRINTF_MALLOC) #if defined(_ESTREAM_PRINTF_MALLOC)
#define my_printf_malloc(a) _ESTREAM_PRINTF_MALLOC((a)) #define my_printf_malloc(a) _ESTREAM_PRINTF_MALLOC((a))
#else #else
#define my_printf_malloc(a) malloc((a)) #define my_printf_malloc(a) malloc((a))
#endif #endif
#if defined(_ESTREAM_PRINTF_FREE) #if defined(_ESTREAM_PRINTF_FREE)
#define my_printf_free(a) _ESTREAM_PRINTF_FREE((a)) #define my_printf_free(a) _ESTREAM_PRINTF_FREE((a))
#else #else
#define my_printf_free(a) free((a)) #define my_printf_free(a) free((a))
#endif #endif
@ -182,7 +182,7 @@ typedef enum
/* A union used to store the actual values. */ /* A union used to store the actual values. */
typedef union typedef union
{ {
char a_char; char a_char;
signed char a_schar; signed char a_schar;
@ -266,7 +266,7 @@ dump_argspecs (argspec_t arg, size_t argcount)
int idx; int idx;
for (idx=0; argcount; argcount--, arg++, idx++) for (idx=0; argcount; argcount--, arg++, idx++)
fprintf (stderr, fprintf (stderr,
"%2d: len=%u flags=%u width=%d prec=%d mod=%d " "%2d: len=%u flags=%u width=%d prec=%d mod=%d "
"con=%d vt=%d pos=%d-%d-%d\n", "con=%d vt=%d pos=%d-%d-%d\n",
idx, idx,
@ -290,8 +290,8 @@ compute_type (argspec_t arg)
{ {
switch (arg->conspec) switch (arg->conspec)
{ {
case CONSPEC_UNKNOWN: case CONSPEC_UNKNOWN:
arg->vt = VALTYPE_UNSUPPORTED; arg->vt = VALTYPE_UNSUPPORTED;
break; break;
case CONSPEC_DECIMAL: case CONSPEC_DECIMAL:
@ -302,7 +302,7 @@ compute_type (argspec_t arg)
case LENMOD_LONG: arg->vt = VALTYPE_LONG; break; case LENMOD_LONG: arg->vt = VALTYPE_LONG; break;
case LENMOD_LONGLONG: arg->vt = VALTYPE_LONGLONG; break; case LENMOD_LONGLONG: arg->vt = VALTYPE_LONGLONG; break;
case LENMOD_INTMAX: arg->vt = VALTYPE_INTMAX; break; case LENMOD_INTMAX: arg->vt = VALTYPE_INTMAX; break;
case LENMOD_SIZET: arg->vt = VALTYPE_SIZE; break; case LENMOD_SIZET: arg->vt = VALTYPE_SIZE; break;
case LENMOD_PTRDIFF: arg->vt = VALTYPE_PTRDIFF; break; case LENMOD_PTRDIFF: arg->vt = VALTYPE_PTRDIFF; break;
default: arg->vt = VALTYPE_INT; break; default: arg->vt = VALTYPE_INT; break;
} }
@ -319,12 +319,12 @@ compute_type (argspec_t arg)
case LENMOD_LONG: arg->vt = VALTYPE_ULONG; break; case LENMOD_LONG: arg->vt = VALTYPE_ULONG; break;
case LENMOD_LONGLONG: arg->vt = VALTYPE_ULONGLONG; break; case LENMOD_LONGLONG: arg->vt = VALTYPE_ULONGLONG; break;
case LENMOD_INTMAX: arg->vt = VALTYPE_UINTMAX; break; case LENMOD_INTMAX: arg->vt = VALTYPE_UINTMAX; break;
case LENMOD_SIZET: arg->vt = VALTYPE_SIZE; break; case LENMOD_SIZET: arg->vt = VALTYPE_SIZE; break;
case LENMOD_PTRDIFF: arg->vt = VALTYPE_PTRDIFF; break; case LENMOD_PTRDIFF: arg->vt = VALTYPE_PTRDIFF; break;
default: arg->vt = VALTYPE_UINT; break; default: arg->vt = VALTYPE_UINT; break;
} }
break; break;
case CONSPEC_FLOAT: case CONSPEC_FLOAT:
case CONSPEC_FLOAT_UP: case CONSPEC_FLOAT_UP:
case CONSPEC_EXP: case CONSPEC_EXP:
@ -340,9 +340,9 @@ compute_type (argspec_t arg)
default: arg->vt = VALTYPE_DOUBLE; break; default: arg->vt = VALTYPE_DOUBLE; break;
} }
break; break;
case CONSPEC_CHAR: case CONSPEC_CHAR:
arg->vt = VALTYPE_INT; arg->vt = VALTYPE_INT;
break; break;
case CONSPEC_STRING: case CONSPEC_STRING:
@ -365,12 +365,12 @@ compute_type (argspec_t arg)
case LENMOD_LONG: arg->vt = VALTYPE_LONG_PTR; break; case LENMOD_LONG: arg->vt = VALTYPE_LONG_PTR; break;
case LENMOD_LONGLONG: arg->vt = VALTYPE_LONGLONG_PTR; break; case LENMOD_LONGLONG: arg->vt = VALTYPE_LONGLONG_PTR; break;
case LENMOD_INTMAX: arg->vt = VALTYPE_INTMAX_PTR; break; case LENMOD_INTMAX: arg->vt = VALTYPE_INTMAX_PTR; break;
case LENMOD_SIZET: arg->vt = VALTYPE_SIZE_PTR; break; case LENMOD_SIZET: arg->vt = VALTYPE_SIZE_PTR; break;
case LENMOD_PTRDIFF: arg->vt = VALTYPE_PTRDIFF_PTR; break; case LENMOD_PTRDIFF: arg->vt = VALTYPE_PTRDIFF_PTR; break;
default: arg->vt = VALTYPE_INT_PTR; break; default: arg->vt = VALTYPE_INT_PTR; break;
} }
break; break;
} }
} }
@ -395,7 +395,7 @@ parse_format (const char *format,
if (!format) if (!format)
goto leave_einval; goto leave_einval;
for (; *format; format++) for (; *format; format++)
{ {
unsigned int flags; unsigned int flags;
@ -403,7 +403,7 @@ parse_format (const char *format,
lenmod_t lenmod; lenmod_t lenmod;
conspec_t conspec; conspec_t conspec;
int arg_pos, width_pos, precision_pos; int arg_pos, width_pos, precision_pos;
if (*format != '%') if (*format != '%')
continue; continue;
s = ++format; s = ++format;
@ -417,7 +417,7 @@ parse_format (const char *format,
if (*s >= '1' && *s <= '9') if (*s >= '1' && *s <= '9')
{ {
const char *save_s = s; const char *save_s = s;
arg_pos = (*s++ - '0'); arg_pos = (*s++ - '0');
for (; *s >= '0' && *s <= '9'; s++) for (; *s >= '0' && *s <= '9'; s++)
arg_pos = 10*arg_pos + (*s - '0'); arg_pos = 10*arg_pos + (*s - '0');
@ -431,7 +431,7 @@ parse_format (const char *format,
s = save_s; s = save_s;
} }
} }
/* Parse the flags. */ /* Parse the flags. */
flags = 0; flags = 0;
for ( ; *s; s++) for ( ; *s; s++)
@ -449,7 +449,7 @@ parse_format (const char *format,
} }
} }
flags_parsed: flags_parsed:
/* Parse the field width. */ /* Parse the field width. */
width_pos = 0; width_pos = 0;
if (*s == '*') if (*s == '*')
@ -532,11 +532,11 @@ parse_format (const char *format,
if (ignore_value) if (ignore_value)
precision = NO_FIELD_VALUE; precision = NO_FIELD_VALUE;
} }
/* Parse the length modifiers. */ /* Parse the length modifiers. */
switch (*s) switch (*s)
{ {
case 'h': case 'h':
if (s[1] == 'h') if (s[1] == 'h')
{ {
lenmod = LENMOD_CHAR; lenmod = LENMOD_CHAR;
@ -562,7 +562,7 @@ parse_format (const char *format,
case 'L': lenmod = LENMOD_LONGDBL; s++; break; case 'L': lenmod = LENMOD_LONGDBL; s++; break;
default: lenmod = LENMOD_NONE; break; default: lenmod = LENMOD_NONE; break;
} }
/* Parse the conversion specifier. */ /* Parse the conversion specifier. */
switch (*s) switch (*s)
{ {
@ -632,7 +632,7 @@ parse_format (const char *format,
*argspecs_addr = argspecs; *argspecs_addr = argspecs;
*r_argspecs_count = argcount; *r_argspecs_count = argcount;
return 0; /* Success. */ return 0; /* Success. */
leave_einval: leave_einval:
errno = EINVAL; errno = EINVAL;
leave: leave:
@ -663,14 +663,14 @@ read_values (valueitem_t valuetable, size_t valuetable_len, va_list vaargs)
value->a_char_ptr = va_arg (vaargs, char *); value->a_char_ptr = va_arg (vaargs, char *);
break; break;
case VALTYPE_SCHAR: value->a_schar = va_arg (vaargs, int); break; case VALTYPE_SCHAR: value->a_schar = va_arg (vaargs, int); break;
case VALTYPE_SCHAR_PTR: case VALTYPE_SCHAR_PTR:
value->a_schar_ptr = va_arg (vaargs, signed char *); value->a_schar_ptr = va_arg (vaargs, signed char *);
break; break;
case VALTYPE_UCHAR: value->a_uchar = va_arg (vaargs, int); break; case VALTYPE_UCHAR: value->a_uchar = va_arg (vaargs, int); break;
case VALTYPE_SHORT: value->a_short = va_arg (vaargs, int); break; case VALTYPE_SHORT: value->a_short = va_arg (vaargs, int); break;
case VALTYPE_USHORT: value->a_ushort = va_arg (vaargs, int); break; case VALTYPE_USHORT: value->a_ushort = va_arg (vaargs, int); break;
case VALTYPE_SHORT_PTR: case VALTYPE_SHORT_PTR:
value->a_short_ptr = va_arg (vaargs, short *); value->a_short_ptr = va_arg (vaargs, short *);
break; break;
case VALTYPE_INT: case VALTYPE_INT:
value->a_int = va_arg (vaargs, int); value->a_int = va_arg (vaargs, int);
@ -684,20 +684,20 @@ read_values (valueitem_t valuetable, size_t valuetable_len, va_list vaargs)
case VALTYPE_LONG: case VALTYPE_LONG:
value->a_long = va_arg (vaargs, long); value->a_long = va_arg (vaargs, long);
break; break;
case VALTYPE_ULONG: case VALTYPE_ULONG:
value->a_ulong = va_arg (vaargs, unsigned long); value->a_ulong = va_arg (vaargs, unsigned long);
break; break;
case VALTYPE_LONG_PTR: case VALTYPE_LONG_PTR:
value->a_long_ptr = va_arg (vaargs, long *); value->a_long_ptr = va_arg (vaargs, long *);
break; break;
#ifdef HAVE_LONG_LONG_INT #ifdef HAVE_LONG_LONG_INT
case VALTYPE_LONGLONG: case VALTYPE_LONGLONG:
value->a_longlong = va_arg (vaargs, long long int); value->a_longlong = va_arg (vaargs, long long int);
break; break;
case VALTYPE_ULONGLONG: case VALTYPE_ULONGLONG:
value->a_ulonglong = va_arg (vaargs, unsigned long long int); value->a_ulonglong = va_arg (vaargs, unsigned long long int);
break; break;
case VALTYPE_LONGLONG_PTR: case VALTYPE_LONGLONG_PTR:
value->a_longlong_ptr = va_arg (vaargs, long long *); value->a_longlong_ptr = va_arg (vaargs, long long *);
break; break;
#endif #endif
@ -712,31 +712,31 @@ read_values (valueitem_t valuetable, size_t valuetable_len, va_list vaargs)
case VALTYPE_STRING: case VALTYPE_STRING:
value->a_string = va_arg (vaargs, const char *); value->a_string = va_arg (vaargs, const char *);
break; break;
case VALTYPE_POINTER: case VALTYPE_POINTER:
value->a_void_ptr = va_arg (vaargs, void *); value->a_void_ptr = va_arg (vaargs, void *);
break; break;
#ifdef HAVE_INTMAX_T #ifdef HAVE_INTMAX_T
case VALTYPE_INTMAX: case VALTYPE_INTMAX:
value->a_intmax = va_arg (vaargs, intmax_t); value->a_intmax = va_arg (vaargs, intmax_t);
break; break;
case VALTYPE_INTMAX_PTR: case VALTYPE_INTMAX_PTR:
value->a_intmax_ptr = va_arg (vaargs, intmax_t *); value->a_intmax_ptr = va_arg (vaargs, intmax_t *);
break; break;
#endif #endif
#ifdef HAVE_UINTMAX_T #ifdef HAVE_UINTMAX_T
case VALTYPE_UINTMAX: case VALTYPE_UINTMAX:
value->a_uintmax = va_arg (vaargs, uintmax_t); value->a_uintmax = va_arg (vaargs, uintmax_t);
break; break;
#endif #endif
case VALTYPE_SIZE: case VALTYPE_SIZE:
value->a_size = va_arg (vaargs, size_t); value->a_size = va_arg (vaargs, size_t);
break; break;
case VALTYPE_SIZE_PTR: case VALTYPE_SIZE_PTR:
value->a_size_ptr = va_arg (vaargs, size_t *); value->a_size_ptr = va_arg (vaargs, size_t *);
break; break;
#ifdef HAVE_PTRDIFF_T #ifdef HAVE_PTRDIFF_T
case VALTYPE_PTRDIFF: case VALTYPE_PTRDIFF:
value->a_ptrdiff = va_arg (vaargs, ptrdiff_t); value->a_ptrdiff = va_arg (vaargs, ptrdiff_t);
break; break;
case VALTYPE_PTRDIFF_PTR: case VALTYPE_PTRDIFF_PTR:
value->a_ptrdiff_ptr = va_arg (vaargs, ptrdiff_t *); value->a_ptrdiff_ptr = va_arg (vaargs, ptrdiff_t *);
@ -771,7 +771,7 @@ pad_out (estream_printf_out_t outfnc, void *outfncarg,
*nbytes += n; *nbytes += n;
count -= n; count -= n;
} }
return 0; return 0;
} }
@ -808,18 +808,18 @@ pr_integer (estream_printf_out_t outfnc, void *outfncarg,
{ {
case VALTYPE_SHORT: along = value.a_short; break; case VALTYPE_SHORT: along = value.a_short; break;
case VALTYPE_INT: along = value.a_int; break; case VALTYPE_INT: along = value.a_int; break;
case VALTYPE_LONG: along = value.a_long; break; case VALTYPE_LONG: along = value.a_long; break;
#ifdef HAVE_LONG_LONG_INT #ifdef HAVE_LONG_LONG_INT
case VALTYPE_LONGLONG: along = value.a_longlong; break; case VALTYPE_LONGLONG: along = value.a_longlong; break;
case VALTYPE_SIZE: along = value.a_size; break; case VALTYPE_SIZE: along = value.a_size; break;
# ifdef HAVE_INTMAX_T # ifdef HAVE_INTMAX_T
case VALTYPE_INTMAX: along = value.a_intmax; break; case VALTYPE_INTMAX: along = value.a_intmax; break;
# endif # endif
# ifdef HAVE_PTRDIFF_T # ifdef HAVE_PTRDIFF_T
case VALTYPE_PTRDIFF: along = value.a_ptrdiff; break; case VALTYPE_PTRDIFF: along = value.a_ptrdiff; break;
# endif # endif
#endif /*HAVE_LONG_LONG_INT*/ #endif /*HAVE_LONG_LONG_INT*/
default: default:
return -1; return -1;
} }
if (along < 0) if (along < 0)
@ -836,18 +836,18 @@ pr_integer (estream_printf_out_t outfnc, void *outfncarg,
{ {
case VALTYPE_USHORT: aulong = value.a_ushort; break; case VALTYPE_USHORT: aulong = value.a_ushort; break;
case VALTYPE_UINT: aulong = value.a_uint; break; case VALTYPE_UINT: aulong = value.a_uint; break;
case VALTYPE_ULONG: aulong = value.a_ulong; break; case VALTYPE_ULONG: aulong = value.a_ulong; break;
#ifdef HAVE_LONG_LONG_INT #ifdef HAVE_LONG_LONG_INT
case VALTYPE_ULONGLONG: aulong = value.a_ulonglong; break; case VALTYPE_ULONGLONG: aulong = value.a_ulonglong; break;
case VALTYPE_SIZE: aulong = value.a_size; break; case VALTYPE_SIZE: aulong = value.a_size; break;
# ifdef HAVE_UINTMAX_T # ifdef HAVE_UINTMAX_T
case VALTYPE_UINTMAX: aulong = value.a_uintmax; break; case VALTYPE_UINTMAX: aulong = value.a_uintmax; break;
# endif # endif
# ifdef HAVE_PTRDIFF_T # ifdef HAVE_PTRDIFF_T
case VALTYPE_PTRDIFF: aulong = value.a_ptrdiff; break; case VALTYPE_PTRDIFF: aulong = value.a_ptrdiff; break;
# endif # endif
#endif /*HAVE_LONG_LONG_INT*/ #endif /*HAVE_LONG_LONG_INT*/
default: default:
return -1; return -1;
} }
} }
@ -878,7 +878,7 @@ pr_integer (estream_printf_out_t outfnc, void *outfncarg,
do do
{ {
if ((arg->flags & FLAG_GROUPING) if ((arg->flags & FLAG_GROUPING)
&& (++grouping == 3) && *grouping_string) && (++grouping == 3) && *grouping_string)
{ {
*--p = *grouping_string; *--p = *grouping_string;
@ -913,7 +913,7 @@ pr_integer (estream_printf_out_t outfnc, void *outfncarg,
if ((arg->flags & FLAG_ALT_CONV)) if ((arg->flags & FLAG_ALT_CONV))
n_extra += 2; n_extra += 2;
} }
n = pend - p; n = pend - p;
if ((arg->flags & FLAG_ZERO_PAD) if ((arg->flags & FLAG_ZERO_PAD)
@ -958,7 +958,7 @@ pr_integer (estream_printf_out_t outfnc, void *outfncarg,
if (rc) if (rc)
return rc; return rc;
} }
rc = outfnc (outfncarg, p, pend - p); rc = outfnc (outfncarg, p, pend - p);
if (rc) if (rc)
return rc; return rc;
@ -1011,7 +1011,7 @@ pr_float (estream_printf_out_t outfnc, void *outfncarg,
adblfloat = value.a_longdouble; adblfloat = value.a_longdouble;
use_dbl=1; break; use_dbl=1; break;
#endif #endif
default: default:
return -1; return -1;
} }
@ -1122,7 +1122,7 @@ pr_char (estream_printf_out_t outfnc, void *outfncarg,
if(rc) if(rc)
return rc; return rc;
*nbytes += 1; *nbytes += 1;
return 0; return 0;
} }
@ -1143,7 +1143,9 @@ pr_string (estream_printf_out_t outfnc, void *outfncarg,
string = "(null)"; string = "(null)";
if (arg->precision >= 0) if (arg->precision >= 0)
{ {
for (n=0,s=string; *s && n < arg->precision; s++) /* Test for nul after N so that we can pass a non-nul terminated
string. */
for (n=0,s=string; n < arg->precision && *s; s++)
n++; n++;
} }
else else
@ -1169,7 +1171,7 @@ pr_string (estream_printf_out_t outfnc, void *outfncarg,
if (rc) if (rc)
return rc; return rc;
} }
return 0; return 0;
} }
@ -1210,7 +1212,7 @@ pr_pointer (estream_printf_out_t outfnc, void *outfncarg,
*--p = '0'; *--p = '0';
*--p = 'x'; *--p = 'x';
*--p = '0'; *--p = '0';
rc = outfnc (outfncarg, p, pend - p); rc = outfnc (outfncarg, p, pend - p);
if (rc) if (rc)
return rc; return rc;
@ -1229,14 +1231,14 @@ pr_bytes_so_far (estream_printf_out_t outfnc, void *outfncarg,
switch (arg->vt) switch (arg->vt)
{ {
case VALTYPE_SCHAR_PTR: case VALTYPE_SCHAR_PTR:
*value.a_schar_ptr = (signed char)(unsigned int)(*nbytes); *value.a_schar_ptr = (signed char)(unsigned int)(*nbytes);
break; break;
case VALTYPE_SHORT_PTR: case VALTYPE_SHORT_PTR:
*value.a_short_ptr = (short)(unsigned int)(*nbytes); *value.a_short_ptr = (short)(unsigned int)(*nbytes);
break; break;
case VALTYPE_LONG_PTR: case VALTYPE_LONG_PTR:
*value.a_long_ptr = (long)(*nbytes); *value.a_long_ptr = (long)(*nbytes);
break; break;
#ifdef HAVE_LONG_LONG_INT #ifdef HAVE_LONG_LONG_INT
case VALTYPE_LONGLONG_PTR: case VALTYPE_LONGLONG_PTR:
@ -1244,12 +1246,12 @@ pr_bytes_so_far (estream_printf_out_t outfnc, void *outfncarg,
break; break;
#endif #endif
#ifdef HAVE_INTMAX_T #ifdef HAVE_INTMAX_T
case VALTYPE_INTMAX_PTR: case VALTYPE_INTMAX_PTR:
*value.a_intmax_ptr = (intmax_t)(*nbytes); *value.a_intmax_ptr = (intmax_t)(*nbytes);
break; break;
#endif #endif
case VALTYPE_SIZE_PTR: case VALTYPE_SIZE_PTR:
*value.a_size_ptr = (*nbytes); *value.a_size_ptr = (*nbytes);
break; break;
#ifdef HAVE_PTRDIFF_T #ifdef HAVE_PTRDIFF_T
case VALTYPE_PTRDIFF_PTR: case VALTYPE_PTRDIFF_PTR:
@ -1274,8 +1276,8 @@ pr_bytes_so_far (estream_printf_out_t outfnc, void *outfncarg,
holds the values and may be directly addressed using the position holds the values and may be directly addressed using the position
arguments given by ARGSPECS. MYERRNO is used for the "%m" arguments given by ARGSPECS. MYERRNO is used for the "%m"
conversion. NBYTES well be updated to reflect the number of bytes conversion. NBYTES well be updated to reflect the number of bytes
send to the output function. */ send to the output function. */
static int static int
do_format (estream_printf_out_t outfnc, void *outfncarg, do_format (estream_printf_out_t outfnc, void *outfncarg,
const char *format, argspec_t argspecs, size_t argspecs_len, const char *format, argspec_t argspecs, size_t argspecs_len,
valueitem_t valuetable, int myerrno, size_t *nbytes) valueitem_t valuetable, int myerrno, size_t *nbytes)
@ -1319,7 +1321,7 @@ do_format (estream_printf_out_t outfnc, void *outfncarg,
/* Save the next start. */ /* Save the next start. */
s += arg->length; s += arg->length;
format = s; format = s;
assert (argidx < argspecs_len); assert (argidx < argspecs_len);
argidx++; argidx++;
@ -1387,9 +1389,9 @@ do_format (estream_printf_out_t outfnc, void *outfncarg,
} }
if (rc) if (rc)
return rc; return rc;
arg++; arg++;
} }
/* Print out any trailing stuff. */ /* Print out any trailing stuff. */
n = s - format; n = s - format;
rc = n? outfnc (outfncarg, format, n) : 0; rc = n? outfnc (outfncarg, format, n) : 0;
@ -1407,7 +1409,7 @@ do_format (estream_printf_out_t outfnc, void *outfncarg,
output of the formatted stuff. FORMAT is the format specification output of the formatted stuff. FORMAT is the format specification
and VAARGS a variable argumemt list matching the arguments of and VAARGS a variable argumemt list matching the arguments of
FORMAT. */ FORMAT. */
int int
estream_format (estream_printf_out_t outfnc, estream_format (estream_printf_out_t outfnc,
void *outfncarg, void *outfncarg,
const char *format, va_list vaargs) const char *format, va_list vaargs)
@ -1442,7 +1444,7 @@ estream_format (estream_printf_out_t outfnc,
/* Check that all ARG_POS fields are set. */ /* Check that all ARG_POS fields are set. */
for (argidx=0,max_pos=0; argidx < argspecs_len; argidx++) for (argidx=0,max_pos=0; argidx < argspecs_len; argidx++)
{ {
if (argspecs[argidx].arg_pos != -1 if (argspecs[argidx].arg_pos != -1
&& argspecs[argidx].arg_pos > max_pos) && argspecs[argidx].arg_pos > max_pos)
max_pos = argspecs[argidx].arg_pos; max_pos = argspecs[argidx].arg_pos;
if (argspecs[argidx].width_pos > max_pos) if (argspecs[argidx].width_pos > max_pos)
@ -1523,13 +1525,13 @@ estream_format (estream_printf_out_t outfnc,
valuetable[validx].vt = VALTYPE_INT; valuetable[validx].vt = VALTYPE_INT;
} }
} }
/* Read all the arguments. This will error out for unsupported /* Read all the arguments. This will error out for unsupported
types and for not given positional arguments. */ types and for not given positional arguments. */
rc = read_values (valuetable, max_pos, vaargs); rc = read_values (valuetable, max_pos, vaargs);
if (rc) if (rc)
goto leave_einval; goto leave_einval;
/* for (validx=0; validx < max_pos; validx++) */ /* for (validx=0; validx < max_pos; validx++) */
/* fprintf (stderr, "%2d: vt=%d\n", validx, valuetable[validx].vt); */ /* fprintf (stderr, "%2d: vt=%d\n", validx, valuetable[validx].vt); */
@ -1538,7 +1540,7 @@ estream_format (estream_printf_out_t outfnc,
argspecs, argspecs_len, valuetable, myerrno, &nbytes); argspecs, argspecs_len, valuetable, myerrno, &nbytes);
goto leave; goto leave;
leave_einval: leave_einval:
errno = EINVAL; errno = EINVAL;
leave_error: leave_error:
@ -1572,11 +1574,11 @@ estream_printf (const char *format, ...)
{ {
int rc; int rc;
va_list arg_ptr; va_list arg_ptr;
va_start (arg_ptr, format); va_start (arg_ptr, format);
rc = estream_format (plain_stdio_out, stderr, format, arg_ptr); rc = estream_format (plain_stdio_out, stderr, format, arg_ptr);
va_end (arg_ptr); va_end (arg_ptr);
return rc; return rc;
} }
@ -1586,16 +1588,16 @@ estream_fprintf (FILE *fp, const char *format, ...)
{ {
int rc; int rc;
va_list arg_ptr; va_list arg_ptr;
va_start (arg_ptr, format); va_start (arg_ptr, format);
rc = estream_format (plain_stdio_out, fp, format, arg_ptr); rc = estream_format (plain_stdio_out, fp, format, arg_ptr);
va_end (arg_ptr); va_end (arg_ptr);
return rc; return rc;
} }
/* A replacement for vfprintf. */ /* A replacement for vfprintf. */
int int
estream_vfprintf (FILE *fp, const char *format, va_list arg_ptr) estream_vfprintf (FILE *fp, const char *format, va_list arg_ptr)
{ {
return estream_format (plain_stdio_out, fp, format, arg_ptr); return estream_format (plain_stdio_out, fp, format, arg_ptr);
@ -1642,7 +1644,7 @@ fixed_buffer_out (void *outfncarg, const char *buf, size_t buflen)
/* A replacement for vsnprintf. */ /* A replacement for vsnprintf. */
int int
estream_vsnprintf (char *buf, size_t bufsize, estream_vsnprintf (char *buf, size_t bufsize,
const char *format, va_list arg_ptr) const char *format, va_list arg_ptr)
{ {
@ -1667,7 +1669,7 @@ estream_vsnprintf (char *buf, size_t bufsize,
} }
/* A replacement for snprintf. */ /* A replacement for snprintf. */
int int
estream_snprintf (char *buf, size_t bufsize, const char *format, ...) estream_snprintf (char *buf, size_t bufsize, const char *format, ...)
{ {
int rc; int rc;
@ -1676,7 +1678,7 @@ estream_snprintf (char *buf, size_t bufsize, const char *format, ...)
va_start (arg_ptr, format); va_start (arg_ptr, format);
rc = estream_vsnprintf (buf, bufsize, format, arg_ptr); rc = estream_vsnprintf (buf, bufsize, format, arg_ptr);
va_end (arg_ptr); va_end (arg_ptr);
return rc; return rc;
} }
@ -1686,7 +1688,7 @@ estream_snprintf (char *buf, size_t bufsize, const char *format, ...)
dynamic_buffer_out. */ dynamic_buffer_out. */
struct dynamic_buffer_parm_s struct dynamic_buffer_parm_s
{ {
int error_flag; /* Internal helper. */ int error_flag; /* Internal helper. */
size_t alloced; /* Allocated size of the buffer. */ size_t alloced; /* Allocated size of the buffer. */
size_t used; /* Used size of the buffer. */ size_t used; /* Used size of the buffer. */
char *buffer; /* Malloced buffer. */ char *buffer; /* Malloced buffer. */
@ -1709,7 +1711,7 @@ dynamic_buffer_out (void *outfncarg, const char *buf, size_t buflen)
if (parm->used + buflen >= parm->alloced) if (parm->used + buflen >= parm->alloced)
{ {
char *p; char *p;
parm->alloced += buflen + 512; parm->alloced += buflen + 512;
p = realloc (parm->buffer, parm->alloced); p = realloc (parm->buffer, parm->alloced);
if (!p) if (!p)
@ -1732,7 +1734,7 @@ dynamic_buffer_out (void *outfncarg, const char *buf, size_t buflen)
/* A replacement for vasprintf. As with the BSD of vasprintf version -1 /* A replacement for vasprintf. As with the BSD of vasprintf version -1
will be returned on error and NULL stored at BUFP. On success the will be returned on error and NULL stored at BUFP. On success the
number of bytes printed will be returned. */ number of bytes printed will be returned. */
int int
estream_vasprintf (char **bufp, const char *format, va_list arg_ptr) estream_vasprintf (char **bufp, const char *format, va_list arg_ptr)
{ {
struct dynamic_buffer_parm_s parm; struct dynamic_buffer_parm_s parm;
@ -1747,7 +1749,7 @@ estream_vasprintf (char **bufp, const char *format, va_list arg_ptr)
*bufp = NULL; *bufp = NULL;
return -1; return -1;
} }
rc = estream_format (dynamic_buffer_out, &parm, format, arg_ptr); rc = estream_format (dynamic_buffer_out, &parm, format, arg_ptr);
if (!rc) if (!rc)
rc = dynamic_buffer_out (&parm, "", 1); /* Print terminating Nul. */ rc = dynamic_buffer_out (&parm, "", 1); /* Print terminating Nul. */
@ -1772,7 +1774,7 @@ estream_vasprintf (char **bufp, const char *format, va_list arg_ptr)
/* A replacement for asprintf. As with the BSD of asprintf version -1 /* A replacement for asprintf. As with the BSD of asprintf version -1
will be returned on error and NULL stored at BUFP. On success the will be returned on error and NULL stored at BUFP. On success the
number of bytes printed will be returned. */ number of bytes printed will be returned. */
int int
estream_asprintf (char **bufp, const char *format, ...) estream_asprintf (char **bufp, const char *format, ...)
{ {
int rc; int rc;
@ -1781,7 +1783,7 @@ estream_asprintf (char **bufp, const char *format, ...)
va_start (arg_ptr, format); va_start (arg_ptr, format);
rc = estream_vasprintf (bufp, format, arg_ptr); rc = estream_vasprintf (bufp, format, arg_ptr);
va_end (arg_ptr); va_end (arg_ptr);
return rc; return rc;
} }