kasprintf.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/lib/kasprintf.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include <stdarg.h>
  8. #include <linux/export.h>
  9. #include <linux/slab.h>
  10. #include <linux/types.h>
  11. #include <linux/string.h>
  12. /* Simplified asprintf. */
  13. char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
  14. {
  15. unsigned int first, second;
  16. char *p;
  17. va_list aq;
  18. va_copy(aq, ap);
  19. first = vsnprintf(NULL, 0, fmt, aq);
  20. va_end(aq);
  21. p = kmalloc_track_caller(first+1, gfp);
  22. if (!p)
  23. return NULL;
  24. second = vsnprintf(p, first+1, fmt, ap);
  25. WARN(first != second, "different return values (%u and %u) from vsnprintf(\"%s\", ...)",
  26. first, second, fmt);
  27. return p;
  28. }
  29. EXPORT_SYMBOL(kvasprintf);
  30. /*
  31. * If fmt contains no % (or is exactly %s), use kstrdup_const. If fmt
  32. * (or the sole vararg) points to rodata, we will then save a memory
  33. * allocation and string copy. In any case, the return value should be
  34. * freed using kfree_const().
  35. */
  36. const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list ap)
  37. {
  38. if (!strchr(fmt, '%'))
  39. return kstrdup_const(fmt, gfp);
  40. if (!strcmp(fmt, "%s"))
  41. return kstrdup_const(va_arg(ap, const char*), gfp);
  42. return kvasprintf(gfp, fmt, ap);
  43. }
  44. EXPORT_SYMBOL(kvasprintf_const);
  45. char *kasprintf(gfp_t gfp, const char *fmt, ...)
  46. {
  47. va_list ap;
  48. char *p;
  49. va_start(ap, fmt);
  50. p = kvasprintf(gfp, fmt, ap);
  51. va_end(ap);
  52. return p;
  53. }
  54. EXPORT_SYMBOL(kasprintf);