vswprintf.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #ifndef __STDIO_BUFFERS
  11. #warning Skipping vswprintf since no buffering!
  12. #else /* __STDIO_BUFFERS */
  13. int vswprintf(wchar_t *__restrict buf, size_t size,
  14. const wchar_t * __restrict format, va_list arg)
  15. {
  16. FILE f;
  17. int rv;
  18. /* __STDIO_STREAM_RESET_GCS(&f); */
  19. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  20. f.__cookie = &(f.__filedes);
  21. f.__gcs.read = NULL;
  22. f.__gcs.write = NULL;
  23. f.__gcs.seek = NULL;
  24. f.__gcs.close = NULL;
  25. #endif
  26. f.__filedes = __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES;
  27. f.__modeflags = (__FLAG_WIDE|__FLAG_WRITEONLY|__FLAG_WRITING);
  28. f.__ungot_width[0] = 0;
  29. #ifdef __STDIO_MBSTATE
  30. __INIT_MBSTATE(&(f.__state));
  31. #endif /* __STDIO_MBSTATE */
  32. #ifdef __UCLIBC_HAS_THREADS__
  33. f.__user_locking = 1; /* Set user locking. */
  34. __stdio_init_mutex(&f.__lock);
  35. #endif
  36. f.__nextopen = NULL;
  37. if (size > ((SIZE_MAX - (size_t) buf)/sizeof(wchar_t))) {
  38. size = ((SIZE_MAX - (size_t) buf)/sizeof(wchar_t));
  39. }
  40. f.__bufstart = (char *) buf;
  41. f.__bufend = (char *)(buf + size);
  42. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
  43. __STDIO_STREAM_DISABLE_GETC(&f);
  44. __STDIO_STREAM_DISABLE_PUTC(&f);
  45. rv = vfwprintf(&f, format, arg);
  46. /* NOTE: Return behaviour differs from snprintf... */
  47. if (f.__bufpos == f.__bufend) {
  48. rv = -1;
  49. if (size) {
  50. f.__bufpos = (char *)(((wchar_t *) f.__bufpos) - 1);
  51. }
  52. }
  53. if (size) {
  54. *((wchar_t *) f.__bufpos) = 0;
  55. }
  56. return rv;
  57. }
  58. #endif /* __STDIO_BUFFERS */