~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/kasprintf.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php