]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Stdio/vasprintf.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Stdio / vasprintf.c
CommitLineData
2aa62f2b 1/* $NetBSD: vasprintf.c,v 1.10 2005/02/09 21:35:47 kleink Exp $ */\r
2\r
3/*\r
4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>\r
5 * All rights reserved.\r
6 *\r
7 * Redistribution and use in source and binary forms, with or without\r
8 * modification, are permitted provided that the following conditions\r
9 * are met:\r
10 * 1. Redistributions of source code must retain the above copyright\r
11 * notice, this list of conditions and the following disclaimer.\r
12 * 2. Redistributions in binary form must reproduce the above copyright\r
13 * notice, this list of conditions and the following disclaimer in the\r
14 * documentation and/or other materials provided with the distribution.\r
15 * 3. The name of the author may not be used to endorse or promote products\r
16 * derived from this software without specific prior written permission.\r
17 *\r
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,\r
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY\r
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL\r
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\r
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\r
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 */\r
29#include <LibConfig.h>\r
30#include <sys/EfiCdefs.h>\r
31#if defined(LIBC_SCCS) && !defined(lint)\r
32__RCSID("$NetBSD: vasprintf.c,v 1.10 2005/02/09 21:35:47 kleink Exp $");\r
33#endif /* LIBC_SCCS and not lint */\r
34\r
35#include <assert.h>\r
36#include <errno.h>\r
37#include <stdio.h>\r
38#include <stdlib.h>\r
39#include "reentrant.h"\r
40#include "local.h"\r
41\r
42int\r
43vasprintf(str, fmt, ap)\r
44 char **str;\r
45 const char *fmt;\r
46 _BSD_VA_LIST_ ap;\r
47{\r
48 int ret;\r
49 FILE f;\r
50 struct __sfileext fext;\r
51 unsigned char *_base;\r
52\r
53 _DIAGASSERT(str != NULL);\r
54 _DIAGASSERT(fmt != NULL);\r
55\r
56 _FILEEXT_SETUP(&f, &fext);\r
57 f._file = -1;\r
58 f._flags = __SWR | __SSTR | __SALC;\r
59 f._bf._base = f._p = (unsigned char *)malloc(128);\r
60 if (f._bf._base == NULL)\r
61 goto err;\r
62 f._bf._size = f._w = 127; /* Leave room for the NUL */\r
63 ret = __vfprintf_unlocked(&f, fmt, ap);\r
64 if (ret == -1)\r
65 goto err;\r
66 *f._p = '\0';\r
67 _base = realloc(f._bf._base, (size_t)(ret + 1));\r
68 if (_base == NULL)\r
69 goto err;\r
70 *str = (char *)_base;\r
71 return (ret);\r
72\r
73err:\r
74 if (f._bf._base)\r
75 free(f._bf._base);\r
76 *str = NULL;\r
77 errno = ENOMEM;\r
78 return (-1);\r
79}\r