Changeset 163e34c in mainline for common


Ignore:
Timestamp:
2025-04-13T19:33:48Z (3 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
f5e1692
Parents:
97f6b71
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-13 18:56:51)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-13 19:33:48)
Message:

Actually convert the printf outputs everywhere

Location:
common
Files:
2 edited
2 moved

Legend:

Unmodified
Added
Removed
  • common/include/printf_core.h

    r97f6b71 r163e34c  
    3636#define _LIBC_PRINTF_CORE_H_
    3737
     38#include <errno.h>
     39#include <stdarg.h>
    3840#include <stddef.h>
    39 #include <stdarg.h>
    4041#include <uchar.h>
    4142
    4243/** Structure for specifying output methods for different printf clones. */
    4344typedef struct {
    44         /* String output function, returns number of printed characters or EOF */
    45         int (*str_write)(const char *, size_t, void *);
    46 
    47         /* Wide string output function, returns number of printed characters or EOF */
    48         int (*wstr_write)(const char32_t *, size_t, void *);
     45        /*
     46         * String output function, returns EOK on success.
     47         * Only returns an error when an irrecoverable failure occurs and
     48         * the string cannot be fully output.
     49         */
     50        errno_t (*write)(const char *, size_t, void *);
    4951
    5052        /* User data - output stream specification, state, locks, etc. */
  • common/printf/printf_core.c

    r97f6b71 r163e34c  
    201201    size_t *written_bytes)
    202202{
    203         int written = ps->str_write(buf, n, ps->data);
    204         if (written < 0)
    205                 return EIO;
     203        errno_t rc = ps->write(buf, n, ps->data);
     204        if (rc != EOK)
     205                return rc;
     206
    206207        _saturating_add(written_bytes, n);
    207208        return EOK;
    208 
    209         #if 0
    210         errno_t rc = ps->write(buf, &n, ps->data);
    211         _saturating_add(written_bytes, n);
    212         return rc;
    213         #endif
    214209}
    215210
     
    14661461
    14671462                rc = _format_number(number, width, precision, base, flags, ps, &counter);
    1468                 if (rc != EOK)
    1469                         continue;
    14701463        }
    14711464
  • common/stdc/vsnprintf.c

    r97f6b71 r163e34c  
    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.