vsnprintf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #ifdef __UCLIBC_MJN3_ONLY__
  10. #warning WISHLIST: Implement vsnprintf for non-buffered and no custom stream case.
  11. #endif /* __UCLIBC_MJN3_ONLY__ */
  12. #ifdef __STDIO_BUFFERS
  13. /* NB: we can still have __USE_OLD_VFPRINTF__ defined in this case! */
  14. int vsnprintf(char *__restrict buf, size_t size,
  15. const char * __restrict format, va_list arg)
  16. {
  17. FILE f;
  18. int rv;
  19. f.__filedes = __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES;
  20. f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  21. #ifdef __UCLIBC_HAS_WCHAR__
  22. f.__ungot_width[0] = 0;
  23. #endif /* __UCLIBC_HAS_WCHAR__ */
  24. #ifdef __STDIO_MBSTATE
  25. __INIT_MBSTATE(&(f.__state));
  26. #endif /* __STDIO_MBSTATE */
  27. #if (defined(__STDIO_BUFFERS) || defined(__USE_OLD_VFPRINTF__)) && defined(__UCLIBC_HAS_THREADS__)
  28. f.__user_locking = 1; /* Set user locking. */
  29. STDIO_INIT_MUTEX(f.__lock);
  30. #endif
  31. f.__nextopen = NULL;
  32. if (size > SIZE_MAX - (size_t) buf) {
  33. size = SIZE_MAX - (size_t) buf;
  34. }
  35. /* TODO: this comment seems to be wrong */
  36. /* Set these last since __bufputc initialization depends on
  37. * __user_locking and only gets set if user locking is on. */
  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_ENABLE_PUTC(&f);
  43. #ifdef __USE_OLD_VFPRINTF__
  44. rv = vfprintf(&f, format, arg);
  45. #else
  46. rv = _vfprintf_internal(&f, format, arg);
  47. #endif
  48. if (size) {
  49. if (f.__bufpos == f.__bufend) {
  50. --f.__bufpos;
  51. }
  52. *f.__bufpos = 0;
  53. }
  54. return rv;
  55. }
  56. libc_hidden_def(vsnprintf)
  57. #elif defined(__USE_OLD_VFPRINTF__)
  58. typedef struct {
  59. FILE f;
  60. unsigned char *bufend; /* pointer to 1 past end of buffer */
  61. unsigned char *bufpos;
  62. } __FILE_vsnprintf;
  63. int vsnprintf(char *__restrict buf, size_t size,
  64. const char * __restrict format, va_list arg)
  65. {
  66. __FILE_vsnprintf f;
  67. int rv;
  68. f.bufpos = buf;
  69. if (size > SIZE_MAX - (size_t) buf) {
  70. size = SIZE_MAX - (size_t) buf;
  71. }
  72. f.bufend = buf + size;
  73. f.f.__filedes = __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB;
  74. f.f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  75. #ifdef __UCLIBC_HAS_WCHAR__
  76. f.f.__ungot_width[0] = 0;
  77. #endif /* __UCLIBC_HAS_WCHAR__ */
  78. #ifdef __STDIO_MBSTATE
  79. __INIT_MBSTATE(&(f.f.__state));
  80. #endif /* __STDIO_MBSTATE */
  81. #ifdef __UCLIBC_HAS_THREADS__
  82. f.f.__user_locking = 1; /* Set user locking. */
  83. STDIO_INIT_MUTEX(f.f.__lock);
  84. #endif
  85. f.f.__nextopen = NULL;
  86. rv = vfprintf((FILE *) &f, format, arg);
  87. if (size) {
  88. if (f.bufpos == f.bufend) {
  89. --f.bufpos;
  90. }
  91. *f.bufpos = 0;
  92. }
  93. return rv;
  94. }
  95. libc_hidden_def(vsnprintf)
  96. #elif defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
  97. typedef struct {
  98. size_t pos;
  99. size_t len;
  100. char *buf;
  101. FILE *fp;
  102. } __snpf_cookie;
  103. #define COOKIE ((__snpf_cookie *) cookie)
  104. static ssize_t snpf_write(register void *cookie, const char *buf,
  105. size_t bufsize)
  106. {
  107. size_t count;
  108. register char *p;
  109. /* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
  110. if (COOKIE->len > COOKIE->pos) {
  111. count = COOKIE->len - COOKIE->pos - 1; /* Leave space for nul. */
  112. if (count > bufsize) {
  113. count = bufsize;
  114. }
  115. p = COOKIE->buf + COOKIE->pos;
  116. while (count) {
  117. *p++ = *buf++;
  118. --count;
  119. }
  120. *p = 0;
  121. }
  122. COOKIE->pos += bufsize;
  123. return bufsize;
  124. }
  125. #undef COOKIE
  126. int vsnprintf(char *__restrict buf, size_t size,
  127. const char * __restrict format, va_list arg)
  128. {
  129. _IO_cookie_file_t cf;
  130. __snpf_cookie cookie;
  131. int rv;
  132. cookie.buf = buf;
  133. cookie.len = size;
  134. cookie.pos = 0;
  135. cookie.fp = &cf.__fp;
  136. cf.__cookie = &cookie;
  137. cf.__gcs.write = snpf_write;
  138. cf.__gcs.read = NULL;
  139. cf.__gcs.seek = NULL;
  140. cf.__gcs.close = NULL;
  141. cf.__fp.__filedes = __STDIO_STREAM_GLIBC_CUSTOM_FILEDES;
  142. cf.__fp.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  143. #ifdef __UCLIBC_HAS_WCHAR__
  144. cf.__fp.__ungot_width[0] = 0;
  145. #endif /* __UCLIBC_HAS_WCHAR__ */
  146. #ifdef __STDIO_MBSTATE
  147. __INIT_MBSTATE(&(cf.__fp.__state));
  148. #endif /* __STDIO_MBSTATE */
  149. cf.__fp.__nextopen = NULL;
  150. rv = _vfprintf_internal(&cf.__fp, format, arg);
  151. return rv;
  152. }
  153. libc_hidden_def(vsnprintf)
  154. #else
  155. #warning Skipping vsnprintf since no buffering, no custom streams, and not old vfprintf!
  156. #ifdef __STDIO_HAS_VSNPRINTF
  157. #error WHOA! __STDIO_HAS_VSNPRINTF is defined!
  158. #endif
  159. #endif