lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org> |
| 2 | * |
| 3 | * GNU Library General Public License (LGPL) version 2 or later. |
| 4 | * |
| 5 | * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details. |
| 6 | */ |
| 7 | |
| 8 | #include "_stdio.h" |
| 9 | #include <stdarg.h> |
| 10 | #include <wchar.h> |
| 11 | |
| 12 | |
| 13 | /* NB: this file is not used if __USE_OLD_VFPRINTF__ */ |
| 14 | |
| 15 | #ifndef __STDIO_BUFFERS |
| 16 | #warning Skipping vswprintf since no buffering! |
| 17 | #else /* __STDIO_BUFFERS */ |
| 18 | |
| 19 | int vswprintf(wchar_t *__restrict buf, size_t size, |
| 20 | const wchar_t * __restrict format, va_list arg) |
| 21 | { |
| 22 | FILE f; |
| 23 | int rv; |
| 24 | |
| 25 | /* __STDIO_STREAM_RESET_GCS(&f); */ |
| 26 | #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ |
| 27 | f.__cookie = &(f.__filedes); |
| 28 | f.__gcs.read = NULL; |
| 29 | f.__gcs.write = NULL; |
| 30 | f.__gcs.seek = NULL; |
| 31 | f.__gcs.close = NULL; |
| 32 | #endif |
| 33 | |
| 34 | f.__filedes = __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES; |
| 35 | f.__modeflags = (__FLAG_WIDE|__FLAG_WRITEONLY|__FLAG_WRITING); |
| 36 | |
| 37 | f.__ungot_width[0] = 0; |
| 38 | #ifdef __STDIO_MBSTATE |
| 39 | __INIT_MBSTATE(&(f.__state)); |
| 40 | #endif /* __STDIO_MBSTATE */ |
| 41 | |
| 42 | #ifdef __UCLIBC_HAS_THREADS__ |
| 43 | f.__user_locking = 1; /* Set user locking. */ |
| 44 | STDIO_INIT_MUTEX(f.__lock); |
| 45 | #endif /* __UCLIBC_HAS_THREADS__ */ |
| 46 | |
| 47 | f.__nextopen = NULL; |
| 48 | |
| 49 | if (size > ((SIZE_MAX - (size_t) buf)/sizeof(wchar_t))) { |
| 50 | size = ((SIZE_MAX - (size_t) buf)/sizeof(wchar_t)); |
| 51 | } |
| 52 | |
| 53 | f.__bufstart = (unsigned char *) buf; |
| 54 | f.__bufend = (unsigned char *) (buf + size); |
| 55 | __STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f); |
| 56 | __STDIO_STREAM_DISABLE_GETC(&f); |
| 57 | __STDIO_STREAM_DISABLE_PUTC(&f); |
| 58 | |
| 59 | rv = _vfwprintf_internal(&f, format, arg); |
| 60 | |
| 61 | /* NOTE: Return behaviour differs from snprintf... */ |
| 62 | if (f.__bufpos == f.__bufend) { |
| 63 | rv = -1; |
| 64 | if (size) { |
| 65 | f.__bufpos = (unsigned char *) (((wchar_t *) f.__bufpos) - 1); |
| 66 | } |
| 67 | } |
| 68 | if (size) { |
| 69 | *((wchar_t *) f.__bufpos) = 0; |
| 70 | } |
| 71 | return rv; |
| 72 | } |
| 73 | libc_hidden_def(vswprintf) |
| 74 | |
| 75 | #endif /* __STDIO_BUFFERS */ |