summaryrefslogtreecommitdiffhomepage
path: root/ir/libcore
diff options
context:
space:
mode:
authorMatthias Braun <matze@braunis.de>2013-09-13 08:55:58 +0200
committerMatthias Braun <matze@braunis.de>2013-09-13 08:57:06 +0200
commitbeb29b8109eeb9a544aa673ec0bab7217fef5ccf (patch)
treebb267f83fc7c824f59432f45aa412f0d967014a2 /ir/libcore
parent0070a44dbde8da9d6295b98cc390107f949c9fc3 (diff)
make printfs return the number of written bytes
This also fixes the return value of snprintf variants in case of full buffer: The number of bytes that would have been written with an infinite buffer is returned.
Diffstat (limited to 'ir/libcore')
-rw-r--r--ir/libcore/lc_printf.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/ir/libcore/lc_printf.c b/ir/libcore/lc_printf.c
index 8ebcf17..3c6db96 100644
--- a/ir/libcore/lc_printf.c
+++ b/ir/libcore/lc_printf.c
@@ -270,7 +270,9 @@ static int std_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_
* buffer of 128 letters for all other types should be enough. */
case 's': {
const char *str = (const char*)val->v_ptr;
- res = lc_arg_append(app, occ, str, strlen(str));
+ size_t size = strlen(str);
+ lc_arg_append(app, occ, str, size);
+ res = size;
break;
}
@@ -278,7 +280,8 @@ static int std_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_
int len = MAX(128, occ->width + 1);
char *buf = XMALLOCN(char, len);
res = dispatch_snprintf(buf, len, fmt, occ->lc_arg_type, val);
- res = lc_appendable_snadd(app, buf, res);
+ assert(res < len);
+ lc_appendable_snadd(app, buf, res);
free(buf);
}
}
@@ -335,7 +338,9 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
const char *s = strchr(fmt, '%');
/* Emit the text before the first % was found */
- res += lc_appendable_snadd(app, fmt, (s ? s : last) - fmt);
+ size_t addlen = (s ? s : last) - fmt;
+ lc_appendable_snadd(app, fmt, addlen);
+ res += addlen;
while (s != NULL) {
lc_arg_value_t val;
@@ -394,7 +399,8 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
switch (ch) {
case '%':
s++;
- res += lc_appendable_chadd(app, '%');
+ lc_appendable_chadd(app, '%');
+ ++res;
break;
case '{': {
const char *named = ++s;
@@ -481,7 +487,9 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
const char *old = s;
s = strchr(s, '%');
- res += lc_appendable_snadd(app, old, (s ? s : last) - old);
+ size_t addlen = (s ? s : last) - old;
+ lc_appendable_snadd(app, old, addlen);
+ res += addlen;
}
return res;