Commit a6e632aa authored by Daniel Kurtz's avatar Daniel Kurtz Committed by Mike Gabriel

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: 's avatarDaniel Kurtz <djkurtz@chromium.org> Reviewed-by: 's avatarPeter Hutterer <peter.hutterer@who-t.net> Signed-off-by: 's avatarPeter Hutterer <peter.hutterer@who-t.net> Backported-to-NX-by: 's avatarMihai Moldovan <ionic@ionic.de>
parent c281c6e0
......@@ -66,4 +66,16 @@ _X_ATTRIBUTE_PRINTF(2, 0);
#define vasprintf Xvasprintf
#endif
/*
* These functions provide a portable implementation of the linux kernel
* scnprintf & vscnprintf routines that return the number of bytes actually
* copied during a snprintf, (excluding the final '\0').
*/
extern _X_EXPORT int
Xscnprintf(char *s, int n, const char * _X_RESTRICT_KYWD fmt, ...)
_X_ATTRIBUTE_PRINTF(3,4);
extern _X_EXPORT int
Xvscnprintf(char *s, int n, const char * _X_RESTRICT_KYWD fmt, va_list va)
_X_ATTRIBUTE_PRINTF(3,0);
#endif /* XPRINTF_H */
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment