Skip to content
Snippets Groups Projects
Commit 59846a8f authored by Gregory Nutt's avatar Gregory Nutt
Browse files

snprintf(): If size is zero, then snprintf() should return the size of the...

snprintf():  If size is zero, then snprintf() should return the size of the required buffer without writing anyting.  From Sami Pelkonen
parent 6f3cb90c
No related branches found
No related tags found
No related merge requests found
/****************************************************************************
* libc/stdio/lib_snprintf.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009, 2011, 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
......@@ -82,19 +82,41 @@
int snprintf(FAR char *buf, size_t size, const char *format, ...)
{
struct lib_memoutstream_s memoutstream;
union
{
struct lib_outstream_s nulloutstream;
struct lib_memoutstream_s memoutstream;
} u;
FAR struct lib_outstream_s *stream;
va_list ap;
int n;
int n;
/* "If the value of [size] is zero on a call to snprintf(), nothing shall
* be written, the number of bytes that would have been written had [size]
* been sufficiently large excluding the terminating null shall be returned,
* and [buf] may be a null pointer." -- opengroup.org
*/
if (size > 0)
{
/* Initialize a memory stream to write to the buffer */
/* Initialize a memory stream to write to the buffer */
lib_memoutstream(&u.memoutstream, buf, size);
stream = &u.memoutstream.public;
}
else
{
/* Use a null stream to get the size of the buffer */
lib_memoutstream((FAR struct lib_memoutstream_s *)&memoutstream, buf, size);
lib_nulloutstream(&u.nulloutstream);
stream = &u.nulloutstream;
}
/* Then let lib_vsprintf do the real work */
va_start(ap, format);
n = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public,
format, ap);
n = lib_vsprintf(stream, format, ap);
va_end(ap);
return n;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment