sprintf.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. static const char version[] = "$Id$";
  2. /*
  3. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  4. *
  5. * See the COPYING file for the terms of usage and distribution.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdarg.h>
  10. #include <string.h>
  11. #include <sd/sprintf.h>
  12. #include <sd/malloc.h>
  13. #include <sd/sd_xplatform.h>
  14. /******************************************************************************/
  15. extern char* sd_sprintf(const char* a_fmt, ...)
  16. {
  17. char* buffer;
  18. va_list args;
  19. va_start(args, a_fmt);
  20. buffer = sd_vsprintf(a_fmt, args);
  21. va_end(args);
  22. return buffer;
  23. }
  24. /******************************************************************************/
  25. extern char* sd_vsprintf(const char* a_fmt, va_list a_args)
  26. {
  27. int size = 10240;
  28. char* buffer = (char*)sd_calloc(size, sizeof(char));
  29. while (1) {
  30. int n = vsnprintf(buffer, size, a_fmt, a_args);
  31. /* If that worked, return */
  32. if (n > -1 && n < size)
  33. {
  34. return buffer;
  35. }
  36. /* Else try again with more space. */
  37. if (n > -1) /* ISO/IEC 9899:1999 */
  38. size = n + 1;
  39. else /* twice the old size */
  40. size *= 2;
  41. buffer = (char*)sd_realloc(buffer, size);
  42. }
  43. return buffer;
  44. }
  45. #if defined(__osf__)
  46. # ifndef snprintf
  47. # include "sprintf.osf.c"
  48. # endif
  49. #endif