kasprintf.c 704 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * linux/lib/kasprintf.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <stdarg.h>
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/types.h>
  10. #include <linux/string.h>
  11. /* Simplified asprintf. */
  12. char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
  13. {
  14. unsigned int len;
  15. char *p;
  16. va_list aq;
  17. va_copy(aq, ap);
  18. len = vsnprintf(NULL, 0, fmt, aq);
  19. va_end(aq);
  20. p = kmalloc(len+1, gfp);
  21. if (!p)
  22. return NULL;
  23. vsnprintf(p, len+1, fmt, ap);
  24. return p;
  25. }
  26. EXPORT_SYMBOL(kvasprintf);
  27. char *kasprintf(gfp_t gfp, const char *fmt, ...)
  28. {
  29. va_list ap;
  30. char *p;
  31. va_start(ap, fmt);
  32. p = kvasprintf(gfp, fmt, ap);
  33. va_end(ap);
  34. return p;
  35. }
  36. EXPORT_SYMBOL(kasprintf);