- Timestamp:
- 2025-04-13T19:33:48Z (3 months ago)
- 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)
- Location:
- common
- Files:
-
- 2 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
common/include/printf_core.h
r97f6b71 r163e34c 36 36 #define _LIBC_PRINTF_CORE_H_ 37 37 38 #include <errno.h> 39 #include <stdarg.h> 38 40 #include <stddef.h> 39 #include <stdarg.h>40 41 #include <uchar.h> 41 42 42 43 /** Structure for specifying output methods for different printf clones. */ 43 44 typedef 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 *); 49 51 50 52 /* User data - output stream specification, state, locks, etc. */ -
common/printf/printf_core.c
r97f6b71 r163e34c 201 201 size_t *written_bytes) 202 202 { 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 206 207 _saturating_add(written_bytes, n); 207 208 return EOK; 208 209 #if 0210 errno_t rc = ps->write(buf, &n, ps->data);211 _saturating_add(written_bytes, n);212 return rc;213 #endif214 209 } 215 210 … … 1466 1461 1467 1462 rc = _format_number(number, width, precision, base, flags, ps, &counter); 1468 if (rc != EOK)1469 continue;1470 1463 } 1471 1464 -
common/stdc/vsnprintf.c
r97f6b71 r163e34c 1 1 /* 2 2 * Copyright (c) 2006 Josef Cejka 3 * Copyright (c) 2025 Jiří Zárevúcky 3 4 * All rights reserved. 4 5 * … … 27 28 */ 28 29 29 /** @addtogroup kernel_generic30 /** @addtogroup libc 30 31 * @{ 31 32 */ … … 33 34 */ 34 35 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> 37 42 38 #include <stdarg.h> 39 #include <stddef.h> 40 #include <uchar.h> 43 typedef struct { 44 char *dst; /* Destination */ 45 size_t left; 46 } vsnprintf_data_t; 41 47 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 *); 48 static 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 } 46 59 47 /* Wide string output function, returns number of printed characters or EOF */ 48 int (*wstr_write)(const char32_t *, size_t, void *); 60 int 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 }; 49 66 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 }; 53 71 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; 55 75 56 #endif 76 /* Write the terminating NUL character. */ 77 if (size > 0) 78 data.dst[0] = 0; 79 80 return written; 81 } 57 82 58 83 /** @}
Note:
See TracChangeset
for help on using the changeset viewer.