source: mainline/common/stdc/vsnprintf.c

Last change on this file was 163e34c, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Actually convert the printf outputs everywhere

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[3214a20]1/*
[df4ed85]2 * Copyright (c) 2006 Josef Cejka
[163e34c]3 * Copyright (c) 2025 Jiří Zárevúcky
[3214a20]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[fadd381]30/** @addtogroup libc
[b2951e2]31 * @{
32 */
33/** @file
34 */
35
[163e34c]36#include <errno.h>
37#include <macros.h>
38#include <printf_core.h>
[3214a20]39#include <stdarg.h>
40#include <stdio.h>
[163e34c]41#include <str.h>
[3214a20]42
[163e34c]43typedef struct {
44 char *dst; /* Destination */
45 size_t left;
46} vsnprintf_data_t;
47
48static int vsnprintf_str_write(const char *str, size_t size, void *data)
[3214a20]49{
[163e34c]50 vsnprintf_data_t *d = data;
51 size_t left = min(size, d->left);
52 if (left > 0) {
53 memcpy(d->dst, str, left);
54 d->dst += left;
55 d->left -= left;
56 }
57 return EOK;
58}
59
60int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
61{
62 vsnprintf_data_t data = {
63 .dst = str,
64 .left = size ? size - 1 : 0,
65 };
66
67 printf_spec_t ps = {
68 vsnprintf_str_write,
69 &data
70 };
[a35b458]71
[163e34c]72 int written = printf_core(fmt, &ps, ap);
73 if (written < 0)
74 return written;
[a35b458]75
[163e34c]76 /* Write the terminating NUL character. */
77 if (size > 0)
78 data.dst[0] = 0;
[a35b458]79
[163e34c]80 return written;
[3214a20]81}
[b2951e2]82
[fadd381]83/** @}
[b2951e2]84 */
Note: See TracBrowser for help on using the repository browser.