diff options
author | Daniel Kurtz <djkurtz@chromium.org> | 2012-04-18 09:51:51 +0000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2012-05-03 14:59:23 +1000 |
commit | 5c2e2a164d615ab06be28a663734e782614b5cc7 (patch) | |
tree | 7d3ef41b6ac9a091168eab1d96c51c25c640eb77 /os | |
parent | c30862879d2c766519780bb7f353f35edf0daa9b (diff) |
os/xprintf: add Xvscnprintf and Xscnprintf
Normal snprintf() usually returns the number of bytes that would have been
written into a buffer had the buffer been long enough.
The scnprintf() variants return the actual number of bytes written,
excluding the trailing '\0'.
Signed-off-by: Daniel Kurtz <djkurtz@chromium.org>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'os')
-rw-r--r-- | os/xprintf.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/os/xprintf.c b/os/xprintf.c index 17fea2ec7..80caa5790 100644 --- a/os/xprintf.c +++ b/os/xprintf.c @@ -186,6 +186,50 @@ XNFasprintf(char **ret, const char *_X_RESTRICT_KYWD format, ...) return size; } +/** + * Varargs snprintf that returns the actual number of bytes (excluding final + * '\0') that were copied into the buffer. + * This is opposed to the normal sprintf() usually returns the number of bytes + * that would have been written. + * + * @param s buffer to copy into + * @param n size of buffer s + * @param format printf style format string + * @param va variable argument list + * @return number of bytes actually copied, excluding final '\0' + */ +int +Xvscnprintf(char *s, int n, const char *format, va_list args) +{ + int x; + if (n == 0) + return 0; + x = vsnprintf(s, n , format, args); + return (x >= n) ? (n - 1) : x; +} + +/** + * snprintf that returns the actual number of bytes (excluding final '\0') that + * were copied into the buffer. + * This is opposed to the normal sprintf() usually returns the number of bytes + * that would have been written. + * + * @param s buffer to copy into + * @param n size of buffer s + * @param format printf style format string + * @param ... arguments for specified format + * @return number of bytes actually copied, excluding final '\0' + */ +int Xscnprintf(char *s, int n, const char *format, ...) +{ + int x; + va_list ap; + va_start(ap, format); + x = Xvscnprintf(s, n, format, ap); + va_end(ap); + return x; +} + /* Old api, now deprecated, may be removed in the future */ char * Xvprintf(const char *format, va_list va) |