diff options
author | Daniel Kurtz <djkurtz@chromium.org> | 2017-12-07 12:11:53 +0100 |
---|---|---|
committer | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2017-12-10 12:17:10 +0100 |
commit | a6e632aac9a013fbae6e8b6a17688e8b1a81e12d (patch) | |
tree | f8e2d29d74d45e5cc2bad958d223feb45d16f292 /nx-X11/programs/Xserver/os | |
parent | c281c6e06a1c9ff65e5ea00d172ea42806fb677f (diff) | |
download | nx-libs-a6e632aac9a013fbae6e8b6a17688e8b1a81e12d.tar.gz nx-libs-a6e632aac9a013fbae6e8b6a17688e8b1a81e12d.tar.bz2 nx-libs-a6e632aac9a013fbae6e8b6a17688e8b1a81e12d.zip |
os/xprintf: add Xvscnprintf and Xscnprintf
Backported from X.org:
commit 5c2e2a164d615ab06be28a663734e782614b5cc7
Author: Daniel Kurtz <djkurtz@chromium.org>
Date: Wed Apr 18 09:51:51 2012 +0000
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>
Backported-to-NX-by: Mihai Moldovan <ionic@ionic.de>
Diffstat (limited to 'nx-X11/programs/Xserver/os')
-rw-r--r-- | nx-X11/programs/Xserver/os/xprintf.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/nx-X11/programs/Xserver/os/xprintf.c b/nx-X11/programs/Xserver/os/xprintf.c index 58e0d507d..674b33581 100644 --- a/nx-X11/programs/Xserver/os/xprintf.c +++ b/nx-X11/programs/Xserver/os/xprintf.c @@ -264,6 +264,50 @@ int Xscnprintf(char *s, int n, const char *format, ...) return x; } +/** + * 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) |