vsnprintf.c 4.6 KB

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