1*eb9ea332Schristos /* $NetBSD: vasprintf.c,v 1.17 2013/05/19 21:45:00 christos Exp $ */
2253ef37dSperry
3253ef37dSperry /*
4253ef37dSperry * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5253ef37dSperry * All rights reserved.
6253ef37dSperry *
7253ef37dSperry * Redistribution and use in source and binary forms, with or without
8253ef37dSperry * modification, are permitted provided that the following conditions
9253ef37dSperry * are met:
10253ef37dSperry * 1. Redistributions of source code must retain the above copyright
11253ef37dSperry * notice, this list of conditions and the following disclaimer.
12253ef37dSperry * 2. Redistributions in binary form must reproduce the above copyright
13253ef37dSperry * notice, this list of conditions and the following disclaimer in the
14253ef37dSperry * documentation and/or other materials provided with the distribution.
15253ef37dSperry * 3. The name of the author may not be used to endorse or promote products
16253ef37dSperry * derived from this software without specific prior written permission.
17253ef37dSperry *
18253ef37dSperry * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19253ef37dSperry * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20253ef37dSperry * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21253ef37dSperry * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22253ef37dSperry * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23253ef37dSperry * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24253ef37dSperry * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25253ef37dSperry * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26253ef37dSperry * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27253ef37dSperry * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28253ef37dSperry */
29253ef37dSperry
30253ef37dSperry #include <sys/cdefs.h>
31253ef37dSperry #if defined(LIBC_SCCS) && !defined(lint)
32*eb9ea332Schristos __RCSID("$NetBSD: vasprintf.c,v 1.17 2013/05/19 21:45:00 christos Exp $");
33253ef37dSperry #endif /* LIBC_SCCS and not lint */
34253ef37dSperry
352561b634Sjoerg #include "namespace.h"
362561b634Sjoerg
37b48252f3Slukem #include <assert.h>
38b48252f3Slukem #include <errno.h>
392561b634Sjoerg #include <locale.h>
40253ef37dSperry #include <stdio.h>
41253ef37dSperry #include <stdlib.h>
422561b634Sjoerg
433fdac2b8Sthorpej #include "reentrant.h"
442561b634Sjoerg #include "setlocale_local.h"
4517f3654aSyamt #include "local.h"
46253ef37dSperry
__weak_alias(asprintf,_asprintf)472561b634Sjoerg __weak_alias(asprintf, _asprintf)
482561b634Sjoerg __weak_alias(asprintf_l, _asprintf_l)
492561b634Sjoerg __weak_alias(vasprintf, _vasprintf)
502561b634Sjoerg __weak_alias(vasprintf_l, _vasprintf_l)
512561b634Sjoerg
52253ef37dSperry int
532561b634Sjoerg vasprintf_l(char **str, locale_t loc, const char *fmt, va_list ap)
54253ef37dSperry {
55253ef37dSperry int ret;
56253ef37dSperry FILE f;
5717f3654aSyamt struct __sfileext fext;
58253ef37dSperry unsigned char *_base;
59253ef37dSperry
60b48252f3Slukem _DIAGASSERT(str != NULL);
61b48252f3Slukem _DIAGASSERT(fmt != NULL);
62b48252f3Slukem
6317f3654aSyamt _FILEEXT_SETUP(&f, &fext);
64442e96d2Smycroft f._file = -1;
65253ef37dSperry f._flags = __SWR | __SSTR | __SALC;
66*eb9ea332Schristos f._bf._base = f._p = malloc(128);
67ef3079f0Smycroft if (f._bf._base == NULL)
68ef3079f0Smycroft goto err;
69ef3079f0Smycroft f._bf._size = f._w = 127; /* Leave room for the NUL */
702561b634Sjoerg ret = __vfprintf_unlocked_l(&f, loc, fmt, ap);
71ef3079f0Smycroft if (ret == -1)
72ef3079f0Smycroft goto err;
73ef3079f0Smycroft *f._p = '\0';
74cfbb35edSchristos _base = realloc(f._bf._base, (size_t)(ret + 1));
75ef3079f0Smycroft if (_base == NULL)
76ef3079f0Smycroft goto err;
77ef3079f0Smycroft *str = (char *)_base;
78526d9427Schristos return ret;
79ef3079f0Smycroft
80ef3079f0Smycroft err:
81ef3079f0Smycroft if (f._bf._base)
82ef3079f0Smycroft free(f._bf._base);
83253ef37dSperry *str = NULL;
84253ef37dSperry errno = ENOMEM;
85526d9427Schristos return -1;
86253ef37dSperry }
872561b634Sjoerg
882561b634Sjoerg int
vasprintf(char ** str,const char * fmt,va_list ap)892561b634Sjoerg vasprintf(char **str, const char *fmt, va_list ap)
902561b634Sjoerg {
91e0ac190eSjoerg return vasprintf_l(str, _current_locale(), fmt, ap);
922561b634Sjoerg }
932561b634Sjoerg
942561b634Sjoerg int
asprintf_l(char ** str,locale_t loc,char const * fmt,...)952561b634Sjoerg asprintf_l(char **str, locale_t loc, char const *fmt, ...)
962561b634Sjoerg {
972561b634Sjoerg va_list ap;
982561b634Sjoerg int ret;
992561b634Sjoerg
1002561b634Sjoerg va_start(ap, fmt);
1012561b634Sjoerg ret = vasprintf_l(str, loc, fmt, ap);
1022561b634Sjoerg va_end(ap);
1032561b634Sjoerg return ret;
1042561b634Sjoerg }
1052561b634Sjoerg
1062561b634Sjoerg int
asprintf(char ** str,char const * fmt,...)1072561b634Sjoerg asprintf(char **str, char const *fmt, ...)
1082561b634Sjoerg {
1092561b634Sjoerg va_list ap;
1102561b634Sjoerg int ret;
1112561b634Sjoerg
1122561b634Sjoerg va_start(ap, fmt);
1132561b634Sjoerg ret = vasprintf(str, fmt, ap);
1142561b634Sjoerg va_end(ap);
1152561b634Sjoerg return ret;
1162561b634Sjoerg }
117