vasprintf.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <features.h>
  8. #ifdef __USE_GNU
  9. #include "_stdio.h"
  10. #include <stdarg.h>
  11. #include <bits/uClibc_va_copy.h>
  12. #ifdef __UCLIBC_MJN3_ONLY__
  13. /* Do the memstream stuff inline to avoid fclose and the openlist? */
  14. #warning CONSIDER: avoid open_memstream call?
  15. #endif
  16. #ifndef __STDIO_HAS_VSNPRINTF
  17. #warning Skipping vasprintf since no vsnprintf!
  18. #else
  19. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  20. libc_hidden_proto(open_memstream)
  21. libc_hidden_proto(fclose)
  22. libc_hidden_proto(vfprintf)
  23. #else
  24. libc_hidden_proto(vsnprintf)
  25. #endif
  26. libc_hidden_proto(vasprintf)
  27. int vasprintf(char **__restrict buf, const char * __restrict format,
  28. va_list arg)
  29. {
  30. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  31. FILE *f;
  32. size_t size;
  33. int rv = -1;
  34. *buf = NULL;
  35. if ((f = open_memstream(buf, &size)) != NULL) {
  36. rv = vfprintf(f, format, arg);
  37. fclose(f);
  38. if (rv < 0) {
  39. free(*buf);
  40. *buf = NULL;
  41. }
  42. }
  43. assert(rv >= -1);
  44. return rv;
  45. #else /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
  46. /* This implementation actually calls the printf machinery twice, but only
  47. * only does one malloc. This can be a problem though when custom printf
  48. * specs or the %m specifier are involved because the results of the
  49. * second call might be different from the first. */
  50. va_list arg2;
  51. int rv;
  52. va_copy(arg2, arg);
  53. rv = vsnprintf(NULL, 0, format, arg2);
  54. va_end(arg2);
  55. *buf = NULL;
  56. if (rv >= 0) {
  57. if ((*buf = malloc(++rv)) != NULL) {
  58. if ((rv = vsnprintf(*buf, rv, format, arg)) < 0) {
  59. free(*buf);
  60. *buf = NULL;
  61. }
  62. }
  63. }
  64. assert(rv >= -1);
  65. return rv;
  66. #endif /* __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__ */
  67. }
  68. libc_hidden_def(vasprintf)
  69. #endif
  70. #endif