vswprintf.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /* NB: this file is not used if __USE_OLD_VFPRINTF__ */
  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. f.__nextopen = NULL;
  35. if (size > ((SIZE_MAX - (size_t) buf)/sizeof(wchar_t))) {
  36. size = ((SIZE_MAX - (size_t) buf)/sizeof(wchar_t));
  37. }
  38. f.__bufstart = (unsigned char *) buf;
  39. f.__bufend = (unsigned char *) (buf + size);
  40. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
  41. __STDIO_STREAM_DISABLE_GETC(&f);
  42. __STDIO_STREAM_DISABLE_PUTC(&f);
  43. rv = _vfwprintf_internal(&f, format, arg);
  44. /* NOTE: Return behaviour differs from snprintf... */
  45. if (f.__bufpos == f.__bufend) {
  46. rv = -1;
  47. if (size) {
  48. f.__bufpos = (unsigned char *) (((wchar_t *) f.__bufpos) - 1);
  49. }
  50. }
  51. if (size) {
  52. *((wchar_t *) f.__bufpos) = 0;
  53. }
  54. return rv;
  55. }
  56. libc_hidden_def(vswprintf)
  57. #endif /* __STDIO_BUFFERS */