forked from iczelia/asmbf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasprintf.h
More file actions
48 lines (36 loc) · 697 Bytes
/
asprintf.h
File metadata and controls
48 lines (36 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef _ASPRINTF_H
#define _ASPRINTF_H
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static int vasprintf(char **buf, const char *format, va_list va) {
int len, ret;
va_list tmp_va;
char dummy;
va_copy(tmp_va, va);
len = vsnprintf(&dummy, 0, format, tmp_va);
va_end(tmp_va);
if (len < 0) {
*buf = NULL;
return (len);
}
len++;
*buf = malloc(len);
if (*buf == NULL)
return -1;
ret = vsnprintf(*buf, len, format, va);
if (ret < 0) {
free(*buf);
*buf = NULL;
}
return ret;
}
static int asprintf(char **buf, const char *format, ...) {
int ret;
va_list va;
va_start(va, format);
ret = vasprintf(buf, format, va);
va_end(va);
return ret;
}
#endif