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. /* 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. /* __STDIO_STREAM_RESET_GCS(&f); */
  20. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  21. f.__cookie = &(f.__filedes);
  22. f.__gcs.read = NULL;
  23. f.__gcs.write = NULL;
  24. f.__gcs.seek = NULL;
  25. f.__gcs.close = NULL;
  26. #endif
  27. f.__filedes = __STDIO_STREAM_FAKE_VSWPRINTF_FILEDES;
  28. f.__modeflags = (__FLAG_WIDE|__FLAG_WRITEONLY|__FLAG_WRITING);
  29. f.__ungot_width[0] = 0;
  30. #ifdef __STDIO_MBSTATE
  31. __INIT_MBSTATE(&(f.__state));
  32. #endif /* __STDIO_MBSTATE */
  33. f.__nextopen = NULL;
  34. if (size > ((SIZE_MAX - (size_t) buf)/sizeof(wchar_t))) {
  35. size = ((SIZE_MAX - (size_t) buf)/sizeof(wchar_t));
  36. }
  37. f.__bufstart = (unsigned char *) buf;
  38. f.__bufend = (unsigned char *) (buf + size);
  39. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
  40. __STDIO_STREAM_DISABLE_GETC(&f);
  41. __STDIO_STREAM_DISABLE_PUTC(&f);
  42. rv = _vfwprintf_internal(&f, format, arg);
  43. /* NOTE: Return behaviour differs from snprintf... */
  44. if (f.__bufpos == f.__bufend) {
  45. rv = -1;
  46. if (size) {
  47. f.__bufpos = (unsigned char *) (((wchar_t *) f.__bufpos) - 1);
  48. }
  49. }
  50. if (size) {
  51. *((wchar_t *) f.__bufpos) = 0;
  52. }
  53. return rv;
  54. }
  55. libc_hidden_def(vswprintf)
  56. #endif /* __STDIO_BUFFERS */