vsnprintf.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * vsnprintf()
  3. *
  4. * Poor substitute for a real vsnprintf() function for systems
  5. * that don't have them...
  6. */
  7. #include "compiler.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include "nasmlib.h"
  13. #include "error.h"
  14. #if !defined(HAVE_VSNPRINTF) && !defined(HAVE__VSNPRINTF)
  15. #define BUFFER_SIZE 65536 /* Bigger than any string we might print... */
  16. static char snprintf_buffer[BUFFER_SIZE];
  17. int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  18. {
  19. int rv, bytes;
  20. if (size > BUFFER_SIZE) {
  21. nasm_panic(ERR_NOFILE,
  22. "vsnprintf: size (%d) > BUFFER_SIZE (%d)",
  23. size, BUFFER_SIZE);
  24. size = BUFFER_SIZE;
  25. }
  26. rv = vsprintf(snprintf_buffer, format, ap);
  27. if (rv >= BUFFER_SIZE)
  28. nasm_panic(ERR_NOFILE, "vsnprintf buffer overflow");
  29. if (size > 0) {
  30. if ((size_t)rv < size-1)
  31. bytes = rv;
  32. else
  33. bytes = size-1;
  34. memcpy(str, snprintf_buffer, bytes);
  35. str[bytes] = '\0';
  36. }
  37. return rv;
  38. }
  39. #endif