vswprintf.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "_stdio.h"
  8. #include <stdarg.h>
  9. #include <wchar.h>
  10. /* NB: this file is not used if __USE_OLD_VFPRINTF__ */
  11. #ifndef __STDIO_BUFFERS
  12. #warning Skipping vswprintf since no buffering!
  13. #else /* __STDIO_BUFFERS */
  14. int vswprintf(wchar_t *__restrict buf, size_t size,
  15. const wchar_t * __restrict format, va_list arg)
  16. {
  17. FILE f;
  18. int rv;
  19. f.__filedes = __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES;
  20. f.__modeflags = (__FLAG_WIDE|__FLAG_WRITEONLY|__FLAG_WRITING);
  21. f.__ungot_width[0] = 0;
  22. #ifdef __STDIO_MBSTATE
  23. __INIT_MBSTATE(&(f.__state));
  24. #endif /* __STDIO_MBSTATE */
  25. #ifdef __UCLIBC_HAS_THREADS__
  26. f.__user_locking = 1; /* Set user locking. */
  27. STDIO_INIT_MUTEX(f.__lock);
  28. #endif /* __UCLIBC_HAS_THREADS__ */
  29. f.__nextopen = NULL;
  30. if (size > ((SIZE_MAX - (size_t) buf)/sizeof(wchar_t))) {
  31. size = ((SIZE_MAX - (size_t) buf)/sizeof(wchar_t));
  32. }
  33. f.__bufstart = (unsigned char *) buf;
  34. f.__bufend = (unsigned char *) (buf + size);
  35. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
  36. __STDIO_STREAM_DISABLE_GETC(&f);
  37. __STDIO_STREAM_DISABLE_PUTC(&f);
  38. rv = _vfwprintf_internal(&f, format, arg);
  39. /* NOTE: Return behaviour differs from snprintf... */
  40. if (f.__bufpos == f.__bufend) {
  41. rv = -1;
  42. if (size) {
  43. f.__bufpos = (unsigned char *) (((wchar_t *) f.__bufpos) - 1);
  44. }
  45. }
  46. if (size) {
  47. *((wchar_t *) f.__bufpos) = 0;
  48. }
  49. return rv;
  50. }
  51. libc_hidden_def(vswprintf)
  52. #endif /* __STDIO_BUFFERS */