vswprintf.c 1.9 KB

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