vsnprintf.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. libc_hidden_proto(vfprintf)
  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. int vsnprintf(char *__restrict buf, size_t size,
  16. const char * __restrict format, va_list arg)
  17. {
  18. FILE f;
  19. int rv;
  20. /* __STDIO_STREAM_RESET_GCS(&f); */
  21. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  22. f.__cookie = &(f.__filedes);
  23. f.__gcs.read = NULL;
  24. f.__gcs.write = NULL;
  25. f.__gcs.seek = NULL;
  26. f.__gcs.close = NULL;
  27. #endif
  28. f.__filedes = __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES;
  29. f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  30. #ifdef __UCLIBC_HAS_WCHAR__
  31. f.__ungot_width[0] = 0;
  32. #endif /* __UCLIBC_HAS_WCHAR__ */
  33. #ifdef __STDIO_MBSTATE
  34. __INIT_MBSTATE(&(f.__state));
  35. #endif /* __STDIO_MBSTATE */
  36. #ifdef __UCLIBC_HAS_THREADS__
  37. f.__user_locking = 1; /* Set user locking. */
  38. __stdio_init_mutex(&f.__lock);
  39. #endif
  40. f.__nextopen = NULL;
  41. if (size > SIZE_MAX - (size_t) buf) {
  42. size = SIZE_MAX - (size_t) buf;
  43. }
  44. /* Set these last since __bufputc initialization depends on
  45. * __user_locking and only gets set if user locking is on. */
  46. f.__bufstart = buf;
  47. f.__bufend = buf + size;
  48. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
  49. __STDIO_STREAM_DISABLE_GETC(&f);
  50. __STDIO_STREAM_ENABLE_PUTC(&f);
  51. rv = vfprintf(&f, format, arg);
  52. if (size) {
  53. if (f.__bufpos == f.__bufend) {
  54. --f.__bufpos;
  55. }
  56. *f.__bufpos = 0;
  57. }
  58. return rv;
  59. }
  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_def(vsnprintf)
  108. #elif defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
  109. typedef struct {
  110. size_t pos;
  111. size_t len;
  112. unsigned char *buf;
  113. FILE *fp;
  114. } __snpf_cookie;
  115. #define COOKIE ((__snpf_cookie *) cookie)
  116. static ssize_t snpf_write(register void *cookie, const char *buf,
  117. size_t bufsize)
  118. {
  119. size_t count;
  120. register char *p;
  121. /* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
  122. if (COOKIE->len > COOKIE->pos) {
  123. count = COOKIE->len - COOKIE->pos - 1; /* Leave space for nul. */
  124. if (count > bufsize) {
  125. count = bufsize;
  126. }
  127. p = COOKIE->buf + COOKIE->pos;
  128. while (count) {
  129. *p++ = *buf++;
  130. --count;
  131. }
  132. *p = 0;
  133. }
  134. COOKIE->pos += bufsize;
  135. return bufsize;
  136. }
  137. #undef COOKIE
  138. int vsnprintf(char *__restrict buf, size_t size,
  139. const char * __restrict format, va_list arg)
  140. {
  141. FILE f;
  142. __snpf_cookie cookie;
  143. int rv;
  144. cookie.buf = buf;
  145. cookie.len = size;
  146. cookie.pos = 0;
  147. cookie.fp = &f;
  148. f.__cookie = &cookie;
  149. f.__gcs.write = snpf_write;
  150. f.__gcs.read = NULL;
  151. f.__gcs.seek = NULL;
  152. f.__gcs.close = NULL;
  153. f.__filedes = -1; /* For debugging. */
  154. f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  155. #ifdef __UCLIBC_HAS_WCHAR__
  156. f.__ungot_width[0] = 0;
  157. #endif /* __UCLIBC_HAS_WCHAR__ */
  158. #ifdef __STDIO_MBSTATE
  159. __INIT_MBSTATE(&(f.__state));
  160. #endif /* __STDIO_MBSTATE */
  161. #ifdef __UCLIBC_HAS_THREADS__
  162. f.__user_locking = 1; /* Set user locking. */
  163. __stdio_init_mutex(&f.__lock);
  164. #endif
  165. f.__nextopen = NULL;
  166. rv = vfprintf(&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