vasprintf_compat.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef __vasprintf_compat_h
  2. #define __vasprintf_compat_h
  3. /**
  4. * @file
  5. * @brief Do not use, json-c internal, may be changed or removed at any time.
  6. */
  7. #include "snprintf_compat.h"
  8. #if !defined(HAVE_VASPRINTF)
  9. /* CAW: compliant version of vasprintf */
  10. static int vasprintf(char **buf, const char *fmt, va_list ap)
  11. {
  12. #ifndef WIN32
  13. static char _T_emptybuffer = '\0';
  14. #endif /* !defined(WIN32) */
  15. int chars;
  16. char *b;
  17. if(!buf) { return -1; }
  18. #ifdef WIN32
  19. chars = _vscprintf(fmt, ap)+1;
  20. #else /* !defined(WIN32) */
  21. /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
  22. our buffer like on some 64bit sun systems.... but hey, its time to move on */
  23. chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
  24. if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
  25. #endif /* defined(WIN32) */
  26. b = (char*)malloc(sizeof(char)*chars);
  27. if(!b) { return -1; }
  28. if((chars = vsprintf(b, fmt, ap)) < 0)
  29. {
  30. free(b);
  31. } else {
  32. *buf = b;
  33. }
  34. return chars;
  35. }
  36. #endif /* !HAVE_VASPRINTF */
  37. #endif /* __vasprintf_compat_h */