vsnprintf.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. int vsnprintf(char *__restrict buf, size_t size,
  14. const char * __restrict format, va_list arg)
  15. {
  16. FILE f;
  17. int rv;
  18. /* __STDIO_STREAM_RESET_GCS(&f); */
  19. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  20. f.__cookie = &(f.__filedes);
  21. f.__gcs.read = NULL;
  22. f.__gcs.write = NULL;
  23. f.__gcs.seek = NULL;
  24. f.__gcs.close = NULL;
  25. #endif
  26. f.__filedes = __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES;
  27. f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  28. #ifdef __UCLIBC_HAS_WCHAR__
  29. f.__ungot_width[0] = 0;
  30. #endif /* __UCLIBC_HAS_WCHAR__ */
  31. #ifdef __STDIO_MBSTATE
  32. __INIT_MBSTATE(&(f.__state));
  33. #endif /* __STDIO_MBSTATE */
  34. #ifdef __UCLIBC_HAS_THREADS__
  35. f.__user_locking = 1; /* Set user locking. */
  36. __stdio_init_mutex(&f.__lock);
  37. #endif
  38. f.__nextopen = NULL;
  39. if (size > SIZE_MAX - (size_t) buf) {
  40. size = SIZE_MAX - (size_t) buf;
  41. }
  42. /* Set these last since __bufputc initialization depends on
  43. * __user_locking and only gets set if user locking is on. */
  44. f.__bufstart = buf;
  45. f.__bufend = buf + size;
  46. __STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
  47. __STDIO_STREAM_DISABLE_GETC(&f);
  48. __STDIO_STREAM_ENABLE_PUTC(&f);
  49. rv = vfprintf(&f, format, arg);
  50. if (size) {
  51. if (f.__bufpos == f.__bufend) {
  52. --f.__bufpos;
  53. }
  54. *f.__bufpos = 0;
  55. }
  56. return rv;
  57. }
  58. #elif defined(__USE_OLD_VFPRINTF__)
  59. typedef struct {
  60. FILE f;
  61. unsigned char *bufend; /* pointer to 1 past end of buffer */
  62. unsigned char *bufpos;
  63. } __FILE_vsnprintf;
  64. int vsnprintf(char *__restrict buf, size_t size,
  65. const char * __restrict format, va_list arg)
  66. {
  67. __FILE_vsnprintf f;
  68. int rv;
  69. f.bufpos = buf;
  70. if (size > SIZE_MAX - (size_t) buf) {
  71. size = SIZE_MAX - (size_t) buf;
  72. }
  73. f.bufend = buf + size;
  74. /* __STDIO_STREAM_RESET_GCS(&f.f); */
  75. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  76. f.f.__cookie = &(f.f.__filedes);
  77. f.f.__gcs.read = NULL;
  78. f.f.__gcs.write = NULL;
  79. f.f.__gcs.seek = NULL;
  80. f.f.__gcs.close = NULL;
  81. #endif
  82. f.f.__filedes = __STDIO_STREAM_FAKE_VSNPRINTF_FILEDES_NB;
  83. f.f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
  84. #ifdef __UCLIBC_HAS_WCHAR__
  85. f.f.__ungot_width[0] = 0;
  86. #endif /* __UCLIBC_HAS_WCHAR__ */
  87. #ifdef __STDIO_MBSTATE
  88. __INIT_MBSTATE(&(f.f.__state));
  89. #endif /* __STDIO_MBSTATE */
  90. #ifdef __UCLIBC_HAS_THREADS__
  91. f.f.__user_locking = 1; /* Set user locking. */
  92. __stdio_init_mutex(&f.f.__lock);
  93. #endif
  94. f.f.__nextopen = NULL;
  95. rv = vfprintf((FILE *) &f, format, arg);
  96. if (size) {
  97. if (f.bufpos == f.bufend) {
  98. --f.bufpos;
  99. }
  100. *f.bufpos = 0;
  101. }
  102. return rv;
  103. }
  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. #ifdef __UCLIBC_HAS_THREADS__
  158. f.__user_locking = 1; /* Set user locking. */
  159. __stdio_init_mutex(&f.__lock);
  160. #endif
  161. f.__nextopen = NULL;
  162. rv = vfprintf(&f, format, arg);
  163. return rv;
  164. }
  165. #else
  166. #warning Skipping vsnprintf since no buffering, no custom streams, and not old vfprintf!
  167. #ifdef __STDIO_HAS_VSNPRINTF
  168. #error WHOA! __STDIO_HAS_VSNPRINTF is defined!
  169. #endif
  170. #endif