vswprintf.c 1.7 KB

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