You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

vasprintf_compat.h 1.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef __vasprintf_compat_h
  2. #define __vasprintf_compat_h
  3. #if !defined(HAVE_VSNPRINTF) && defined(_MSC_VER)
  4. # define vsnprintf _vsnprintf
  5. #elif !defined(HAVE_VSNPRINTF) /* !HAVE_VSNPRINTF */
  6. # error Need vsnprintf!
  7. #endif /* !HAVE_VSNPRINTF && defined(WIN32) */
  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 */