vsnprintf.c 4.5 KB

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