asprintf.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef CCAN_ASPRINTF_H
  2. #define CCAN_ASPRINTF_H
  3. #include "config.h"
  4. #include <ccan/compiler/compiler.h>
  5. /**
  6. * afmt - allocate and populate a string with the given format.
  7. * @fmt: printf-style format.
  8. *
  9. * This is a simplified asprintf interface. Returns NULL on error.
  10. */
  11. char *PRINTF_FMT(1, 2) afmt(const char *fmt, ...);
  12. #if HAVE_ASPRINTF
  13. #include <stdio.h>
  14. #else
  15. #include <stdarg.h>
  16. /**
  17. * asprintf - printf to a dynamically-allocated string.
  18. * @strp: pointer to the string to allocate.
  19. * @fmt: printf-style format.
  20. *
  21. * Returns -1 (and leaves @strp undefined) on an error. Otherwise returns
  22. * number of bytes printed into @strp.
  23. *
  24. * Example:
  25. * static char *greeting(const char *name)
  26. * {
  27. * char *str;
  28. * int len = asprintf(&str, "Hello %s", name);
  29. * if (len < 0)
  30. * return NULL;
  31. * return str;
  32. * }
  33. */
  34. int PRINTF_FMT(2, 3) asprintf(char **strp, const char *fmt, ...);
  35. /**
  36. * vasprintf - vprintf to a dynamically-allocated string.
  37. * @strp: pointer to the string to allocate.
  38. * @fmt: printf-style format.
  39. *
  40. * Returns -1 (and leaves @strp undefined) on an error. Otherwise returns
  41. * number of bytes printed into @strp.
  42. */
  43. int vasprintf(char **strp, const char *fmt, va_list ap);
  44. #endif
  45. #endif /* CCAN_ASPRINTF_H */