scsi: fc: Export fc_bsg_jobdone and use it in FC drivers
[linux/fpc-iii.git] / lib / kasprintf.c
blob7f6c506a494226479af1152f13f99f1870f554d1
1 /*
2 * linux/lib/kasprintf.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <stdarg.h>
8 #include <linux/export.h>
9 #include <linux/slab.h>
10 #include <linux/types.h>
11 #include <linux/string.h>
13 /* Simplified asprintf. */
14 char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
16 unsigned int first, second;
17 char *p;
18 va_list aq;
20 va_copy(aq, ap);
21 first = vsnprintf(NULL, 0, fmt, aq);
22 va_end(aq);
24 p = kmalloc_track_caller(first+1, gfp);
25 if (!p)
26 return NULL;
28 second = vsnprintf(p, first+1, fmt, ap);
29 WARN(first != second, "different return values (%u and %u) from vsnprintf(\"%s\", ...)",
30 first, second, fmt);
32 return p;
34 EXPORT_SYMBOL(kvasprintf);
37 * If fmt contains no % (or is exactly %s), use kstrdup_const. If fmt
38 * (or the sole vararg) points to rodata, we will then save a memory
39 * allocation and string copy. In any case, the return value should be
40 * freed using kfree_const().
42 const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list ap)
44 if (!strchr(fmt, '%'))
45 return kstrdup_const(fmt, gfp);
46 if (!strcmp(fmt, "%s"))
47 return kstrdup_const(va_arg(ap, const char*), gfp);
48 return kvasprintf(gfp, fmt, ap);
50 EXPORT_SYMBOL(kvasprintf_const);
52 char *kasprintf(gfp_t gfp, const char *fmt, ...)
54 va_list ap;
55 char *p;
57 va_start(ap, fmt);
58 p = kvasprintf(gfp, fmt, ap);
59 va_end(ap);
61 return p;
63 EXPORT_SYMBOL(kasprintf);