strdup: remove unused variable
[single-header-libcext.git] / asprintf.h
1 /*
2 * This is free and unencumbered software released into the public domain.
3 *
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
7 * means.
8 *
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
16
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * For more information, please refer to <https://unlicense.org>
26 */
27
28 /* https://git.datadissipation.net */
29 #ifndef ASPRINTF_H_
30 #define ASPRINTF_H_ 1
31
32 #ifdef ASPRINTF_INCLUDE_LIBC
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #endif /* ASPRINTF_INCLUDE_LIBC */
37
38 #ifndef HAVE_ASPRINTF
39 #define HAVE_ASPRINTF 1
40 #define asprintf i_asprintf_
41 #define vasprintf i_vasprintf_
42 int i_vasprintf_(char **strp, const char *fmt, va_list ap);
43 int i_asprintf_(char **strp, const char *fmt, ...);
44 #endif /* !HAVE_ASPRINTF */
45
46 #ifdef ASPRINTF_IMPLEMENTATION
47 int
48 i_vasprintf_(char **strp, const char *fmt, va_list ap)
49 {
50 int size;
51 va_list tp;
52
53 va_copy(tp, ap);
54 size = vsnprintf(NULL, 0, fmt, tp);
55 va_end(tp);
56
57 if (size < 0 || !(*strp = malloc(size + 1)))
58 return -1;
59
60 return vsnprintf(*strp, size + 1, fmt, ap);
61 }
62
63 int
64 i_asprintf_(char **strp, const char *fmt, ...)
65 {
66 int ret;
67 va_list ap;
68
69 va_start(ap, fmt);
70 ret = i_vasprintf_(strp, fmt, ap);
71 va_end(ap);
72
73 return ret;
74 }
75 #endif /* ASPRINTF_IMPLEMENTATION */
76 #endif /* !ASPRINTF_H_ */