vsnprintf.c 4.7 KB

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