| 35 | ****************************************************************************/ |
| 36 | |
| 37 | int snprintf(FAR char *buf, size_t size, FAR const IPTR char *format, ...) |
| 38 | { |
| 39 | union |
| 40 | { |
| 41 | struct lib_outstream_s nulloutstream; |
| 42 | struct lib_memoutstream_s memoutstream; |
| 43 | } u; |
| 44 | |
| 45 | FAR struct lib_outstream_s *stream; |
| 46 | va_list ap; |
| 47 | int n; |
| 48 | |
| 49 | /* "If the value of [size] is zero on a call to snprintf(), nothing shall |
| 50 | * be written, the number of bytes that would have been written had [size] |
| 51 | * been sufficiently large excluding the terminating null shall be |
| 52 | * returned, and [buf] may be a null pointer." -- opengroup.org |
| 53 | */ |
| 54 | |
| 55 | if (size > 0) |
| 56 | { |
| 57 | /* Initialize a memory stream to write to the buffer */ |
| 58 | |
| 59 | lib_memoutstream(&u.memoutstream, buf, size); |
| 60 | stream = &u.memoutstream.common; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | /* Use a null stream to get the size of the buffer */ |
| 65 | |
| 66 | lib_nulloutstream(&u.nulloutstream); |
| 67 | stream = &u.nulloutstream; |
| 68 | } |
| 69 | |
| 70 | /* Then let lib_vsprintf do the real work */ |
| 71 | |
| 72 | va_start(ap, format); |
| 73 | n = lib_vsprintf(stream, format, ap); |
| 74 | va_end(ap); |
| 75 | return n; |
| 76 | } |
no test coverage detected