asprintf.h 1.3 KB

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