vsnprintf.c 4.7 KB

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