Changeset 163e34c in mainline
- Timestamp:
- 2025-04-13T19:33:48Z (4 days 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)
- Files:
-
- 3 deleted
- 11 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 /** @} -
kernel/generic/meson.build
r97f6b71 r163e34c 40 40 'common/adt/list.c', 41 41 'common/adt/odict.c', 42 'common/gsort.c', 42 43 'common/printf/printf_core.c', 43 44 'common/stdc/calloc.c', 44 45 'common/stdc/ctype.c', 45 46 'common/stdc/mem.c', 46 'common/gsort.c', 47 'common/stdc/snprintf.c', 48 'common/stdc/vsnprintf.c', 47 49 'common/str.c', 48 50 'common/str_error.c', … … 96 98 'src/mm/reserve.c', 97 99 'src/printf/printf.c', 98 'src/printf/snprintf.c',99 100 'src/printf/vprintf.c', 100 'src/printf/vsnprintf.c',101 101 'src/proc/program.c', 102 102 'src/proc/scheduler.c', -
kernel/generic/src/log/log.c
r97f6b71 r163e34c 33 33 */ 34 34 35 #include <sysinfo/sysinfo.h> 36 #include <synch/spinlock.h> 37 #include <typedefs.h> 35 #include <abi/log.h> 36 #include <arch.h> 37 #include <atomic.h> 38 #include <console/console.h> 39 #include <ddi/ddi.h> 38 40 #include <ddi/irq.h> 39 #include < ddi/ddi.h>41 #include <errno.h> 40 42 #include <ipc/event.h> 41 43 #include <ipc/irq.h> 42 #include < arch.h>44 #include <log.h> 43 45 #include <panic.h> 46 #include <print.h> 47 #include <printf_core.h> 44 48 #include <putchar.h> 45 #include <atomic.h> 49 #include <stdarg.h> 50 #include <stdlib.h> 51 #include <str.h> 52 #include <synch/spinlock.h> 46 53 #include <syscall/copy.h> 47 #include <errno.h> 48 #include <str.h> 49 #include <print.h> 50 #include <printf/printf_core.h> 51 #include <stdarg.h> 52 #include <log.h> 53 #include <console/console.h> 54 #include <abi/log.h> 55 #include <stdlib.h> 54 #include <sysinfo/sysinfo.h> 55 #include <typedefs.h> 56 56 57 57 #define LOG_PAGES 8 … … 204 204 { 205 205 size_t offset = 0; 206 size_t chars = 0; 207 208 while (offset < size) { 206 207 while (offset < size) 209 208 kio_push_char(str_decode(str, &offset, size)); 210 chars++;211 }212 209 213 210 log_append((const uint8_t *)str, size); 214 211 215 return chars; 216 } 217 218 static int log_printf_wstr_write(const char32_t *wstr, size_t size, void *data) 219 { 220 char buffer[16]; 221 size_t offset = 0; 222 size_t chars = 0; 223 224 for (offset = 0; offset < size; offset += sizeof(char32_t), chars++) { 225 kio_push_char(wstr[chars]); 226 227 size_t buffer_offset = 0; 228 errno_t rc = chr_encode(wstr[chars], buffer, &buffer_offset, 16); 229 if (rc != EOK) { 230 return EOF; 231 } 232 233 log_append((const uint8_t *)buffer, buffer_offset); 234 } 235 236 return chars; 212 return EOK; 237 213 } 238 214 … … 243 219 int log_vprintf(const char *fmt, va_list args) 244 220 { 245 int ret;246 247 221 printf_spec_t ps = { 248 222 log_printf_str_write, 249 log_printf_wstr_write,250 223 NULL 251 224 }; 252 225 253 ret = printf_core(fmt, &ps, args); 254 255 return ret; 226 return printf_core(fmt, &ps, args); 256 227 } 257 228 -
kernel/generic/src/printf/vprintf.c
r97f6b71 r163e34c 36 36 #include <console/console.h> 37 37 #include <print.h> 38 #include <printf /printf_core.h>38 #include <printf_core.h> 39 39 #include <putchar.h> 40 40 #include <str.h> … … 42 42 #include <typedefs.h> 43 43 44 static int vprintf_str_write(const char *str, size_t size, void *data)44 static errno_t vprintf_str_write(const char *str, size_t size, void *data) 45 45 { 46 46 size_t offset = 0; 47 size_t chars = 0;48 47 49 while (offset < size) {48 while (offset < size) 50 49 putuchar(str_decode(str, &offset, size)); 51 chars++;52 }53 50 54 return chars; 55 } 56 57 static int vprintf_wstr_write(const char32_t *str, size_t size, void *data) 58 { 59 size_t offset = 0; 60 size_t chars = 0; 61 62 while (offset < size) { 63 putuchar(str[chars]); 64 chars++; 65 offset += sizeof(char32_t); 66 } 67 68 return chars; 51 return EOK; 69 52 } 70 53 … … 92 75 printf_spec_t ps = { 93 76 vprintf_str_write, 94 vprintf_wstr_write,95 77 NULL 96 78 }; -
uspace/lib/c/generic/io/asprintf.c
r97f6b71 r163e34c 40 40 #include <str.h> 41 41 #include <printf_core.h> 42 43 static int asprintf_str_write(const char *str, size_t count, void *unused)44 {45 return str_nlength(str, count);46 }47 48 static int asprintf_wstr_write(const char32_t *str, size_t count, void *unused)49 {50 return wstr_nlength(str, count);51 }52 53 int vprintf_length(const char *fmt, va_list args)54 {55 printf_spec_t ps = {56 asprintf_str_write,57 asprintf_wstr_write,58 NULL59 };60 61 return printf_core(fmt, &ps, args);62 }63 64 int printf_length(const char *fmt, ...)65 {66 va_list args;67 va_start(args, fmt);68 int ret = vprintf_length(fmt, args);69 va_end(args);70 71 return ret;72 }73 42 74 43 /** Allocate and print to string. … … 115 84 int ret = vasprintf(strp, fmt, args); 116 85 va_end(args); 117 118 86 return ret; 119 87 } -
uspace/lib/c/generic/io/kio.c
r97f6b71 r163e34c 131 131 } 132 132 133 static int kio_vprintf_str_write(const char *str, size_t size, void *data)133 static errno_t kio_vprintf_str_write(const char *str, size_t size, void *data) 134 134 { 135 size_t wr; 136 137 wr = 0; 138 (void) kio_write(str, size, &wr); 139 return str_nlength(str, wr); 140 } 141 142 static int kio_vprintf_wstr_write(const char32_t *str, size_t size, void *data) 143 { 144 size_t offset = 0; 145 size_t chars = 0; 146 size_t wr; 147 148 while (offset < size) { 149 char buf[STR_BOUNDS(1)]; 150 size_t sz = 0; 151 152 if (chr_encode(str[chars], buf, &sz, STR_BOUNDS(1)) == EOK) 153 kio_write(buf, sz, &wr); 154 155 chars++; 156 offset += sizeof(char32_t); 157 } 158 159 return chars; 135 size_t wr = 0; 136 return kio_write(str, size, &wr); 160 137 } 161 138 … … 172 149 printf_spec_t ps = { 173 150 kio_vprintf_str_write, 174 kio_vprintf_wstr_write,175 151 NULL 176 152 }; -
uspace/lib/c/generic/io/vprintf.c
r97f6b71 r163e34c 42 42 static FIBRIL_MUTEX_INITIALIZE(printf_mutex); 43 43 44 static int vprintf_str_write(const char *str, size_t size, void *stream)44 static errno_t vprintf_str_write(const char *str, size_t size, void *stream) 45 45 { 46 size_t wr = fwrite(str, 1, size, (FILE *) stream); 47 return str_nlength(str, wr); 48 } 46 errno_t old_errno = errno; 49 47 50 static int vprintf_wstr_write(const char32_t *str, size_t size, void *stream) 51 { 52 size_t offset = 0; 53 size_t chars = 0; 48 errno = EOK; 49 size_t written = fwrite(str, 1, size, (FILE *) stream); 54 50 55 while (offset < size) { 56 if (fputuc(str[chars], (FILE *) stream) <= 0) 57 break; 51 if (errno == EOK && written != size) 52 errno = EIO; 58 53 59 chars++; 60 offset += sizeof(char32_t); 61 } 54 if (errno != EOK) 55 return errno; 62 56 63 return chars; 57 errno = old_errno; 58 return EOK; 64 59 } 65 60 … … 77 72 printf_spec_t ps = { 78 73 vprintf_str_write, 79 vprintf_wstr_write,80 74 stream 81 75 }; -
uspace/lib/c/include/stdio.h
r97f6b71 r163e34c 209 209 }; 210 210 211 extern int vprintf_length(const char *, va_list);212 extern int printf_length(const char *, ...)213 _HELENOS_PRINTF_ATTRIBUTE(1, 2);214 211 extern FILE *fdopen(int, const char *); 215 212 extern int fileno(FILE *); -
uspace/lib/c/meson.build
r97f6b71 r163e34c 72 72 'common/stdc/mem.c', 73 73 'common/stdc/qsort.c', 74 'common/stdc/snprintf.c', 74 75 'common/stdc/uchar.c', 76 'common/stdc/vsnprintf.c', 75 77 'common/stdc/wchar.c', 76 78 'common/str.c', … … 113 115 'generic/io/logctl.c', 114 116 'generic/io/printf.c', 115 'generic/io/snprintf.c',116 117 'generic/io/table.c', 117 118 'generic/io/vprintf.c', 118 'generic/io/vsnprintf.c',119 119 'generic/ipc.c', 120 120 'generic/irq.c', -
uspace/lib/posix/src/stdio.c
r97f6b71 r163e34c 232 232 * @return The number of written characters. 233 233 */ 234 static int _dprintf_str_write(const char *str, size_t size, void *fd)234 static errno_t _dprintf_str_write(const char *str, size_t size, void *fd) 235 235 { 236 236 const int fildes = *(int *) fd; 237 237 size_t wr; 238 if (failed(vfs_write(fildes, &posix_pos[fildes], str, size, &wr))) 239 return -1; 240 return str_nlength(str, wr); 241 } 242 243 /** 244 * Write wide string to the opened file. 245 * 246 * @param str String to be written. 247 * @param size Size of the string (in bytes). 248 * @param fd File descriptor of the opened file. 249 * @return The number of written characters. 250 */ 251 static int _dprintf_wstr_write(const char32_t *str, size_t size, void *fd) 252 { 253 size_t offset = 0; 254 size_t chars = 0; 255 size_t sz; 256 char buf[4]; 257 258 while (offset < size) { 259 sz = 0; 260 if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) { 261 break; 262 } 263 264 const int fildes = *(int *) fd; 265 size_t nwr; 266 if (vfs_write(fildes, &posix_pos[fildes], buf, sz, &nwr) != EOK) 267 break; 268 269 chars++; 270 offset += sizeof(char32_t); 271 } 272 273 return chars; 238 return vfs_write(fildes, &posix_pos[fildes], str, size, &wr); 274 239 } 275 240 … … 285 250 { 286 251 printf_spec_t spec = { 287 .str_write = _dprintf_str_write, 288 .wstr_write = _dprintf_wstr_write, 252 .write = _dprintf_str_write, 289 253 .data = &fildes 290 254 };
Note:
See TracChangeset
for help on using the changeset viewer.