Debian Codegraph @

contrib/stdio-shim.c

raw symbols SCIP
1 /* librist. Copyright © 2019 SipRadius LLC. All right reserved.
2 * Author: Sergio Ammirata, Ph.D. <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7 #include "stdio-shim.h"
8 #include <stdlib.h>
9 #include <limits.h>
10
11 #ifdef _MSC_VER
12 int vasprintf(char** strp, const char* fmt, va_list ap)
13 {
14 #ifdef _WIN32
15 int size = _vscprintf(fmt, ap);
16 #else
17 // https://stackoverflow.com/a/4785411
18 va_list apcopy;
19 va_copy(apcopy, ap);
20 int size = vsnprintf(NULL, 0, fmt, apcopy);
21 va_end(apcopy);
22 #endif
23 if ((size < 0) || (size >= INT_MAX)) {
24 goto fail;
25 }
26
27 char *str = malloc(size + 1); //+1 for null
28 if (str == NULL) {
29 goto fail;
30 }
31
32 int r = vsnprintf(str, size + 1, fmt, ap); //+1 for null
33 if ((r < 0) || (r > size)) {
34 free(str);
35 goto fail;
36 }
37
38 *strp = str;
39 return r;
40
41 fail:
42 *strp = NULL;
43 return -1;
44 }
45
46 int asprintf(char** strp, const char* fmt, ...)
47 {
48 int r;
49 va_list ap;
50 va_start(ap, fmt);
51 r = vasprintf(strp, fmt, ap);
52 va_end(ap);
53 return r;
54 }
55 #endif