1 // Copyright 2018 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License for more details.
11 #include <io.h>
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16
17 // From https://msdn.microsoft.com/en-us/library/28d5ce15.aspx
vasprintf(char ** buf,const char * format,va_list args)18 int vasprintf(char** buf, const char* format, va_list args) {
19 int len;
20
21 if (buf == NULL) {
22 return -1;
23 }
24
25 len = _vscprintf(format, args) // _vscprintf doesn't count
26 + 1; // terminating '\0'
27
28 if (len <= 0) {
29 return len;
30 }
31
32 *buf = (char*)malloc(len * sizeof(char));
33
34 vsprintf(*buf, format, args); // C4996
35 // Note: vsprintf is deprecated; consider using vsprintf_s instead
36 return len;
37 }
38
39