Changeset c7c6afd in mainline for common/stdc/vsnprintf.c


Ignore:
Timestamp:
2025-04-13T23:27:44Z (4 weeks ago)
Author:
GitHub <noreply@…>
Children:
b6061f8c
Parents:
240b2e4 (diff), f5e1692 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Wayne Thornton <wmthornton-dev@…> (2025-04-13 23:27:44)
git-committer:
GitHub <noreply@…> (2025-04-13 23:27:44)
Message:

Merge branch 'HelenOS:master' into master

File:
1 moved

Legend:

Unmodified
Added
Removed
  • common/stdc/vsnprintf.c

    r240b2e4 rc7c6afd  
    11/*
    22 * Copyright (c) 2006 Josef Cejka
     3 * Copyright (c) 2025 Jiří Zárevúcky
    34 * All rights reserved.
    45 *
     
    2728 */
    2829
    29 /** @addtogroup kernel_generic
     30/** @addtogroup libc
    3031 * @{
    3132 */
     
    3334 */
    3435
    35 #ifndef KERN_PRINTF_CORE_H_
    36 #define KERN_PRINTF_CORE_H_
     36#include <errno.h>
     37#include <macros.h>
     38#include <printf_core.h>
     39#include <stdarg.h>
     40#include <stdio.h>
     41#include <str.h>
    3742
    38 #include <stdarg.h>
    39 #include <stddef.h>
    40 #include <uchar.h>
     43typedef struct {
     44        char *dst;      /* Destination */
     45        size_t left;
     46} vsnprintf_data_t;
    4147
    42 /** Structure for specifying output methods for different printf clones. */
    43 typedef struct {
    44         /* String output function, returns number of printed characters or EOF */
    45         int (*str_write)(const char *, size_t, void *);
     48static int vsnprintf_str_write(const char *str, size_t size, void *data)
     49{
     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}
    4659
    47         /* Wide string output function, returns number of printed characters or EOF */
    48         int (*wstr_write)(const char32_t *, size_t, void *);
     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        };
    4966
    50         /* User data - output stream specification, state, locks, etc. */
    51         void *data;
    52 } printf_spec_t;
     67        printf_spec_t ps = {
     68                vsnprintf_str_write,
     69                &data
     70        };
    5371
    54 extern int printf_core(const char *fmt, printf_spec_t *ps, va_list ap);
     72        int written = printf_core(fmt, &ps, ap);
     73        if (written < 0)
     74                return written;
    5575
    56 #endif
     76        /* Write the terminating NUL character. */
     77        if (size > 0)
     78                data.dst[0] = 0;
     79
     80        return written;
     81}
    5782
    5883/** @}
Note: See TracChangeset for help on using the changeset viewer.