vsnprintf.c 4.8 KB

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