asprintf.c (7e546392b5fe3a496acff53ac7aadd1c57b2a4cf) asprintf.c (64a965e7078acf9079cbe7f2c001852d4f6f2832)
1/*-
2 * Copyright (c) 1996 Peter Wemm <peter@freebsd.org>
1/* $OpenBSD: asprintf.c,v 1.4 1998/06/21 22:13:46 millert Exp $ */
2
3/*
4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
12 *
17 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#if defined(LIBC_RCS) && !defined(lint)
28 */
29
30#if defined(LIBC_RCS) && !defined(lint)
27static char rcsid[] = "$Id$";
31static char rcsid[] = "$Id: asprintf.c,v 1.3 1997/02/22 15:01:45 peter Exp $";
28#endif /* LIBC_RCS and not lint */
29
30#include <stdio.h>
32#endif /* LIBC_RCS and not lint */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <errno.h>
31#if __STDC__
32#include <stdarg.h>
33#else
34#include <varargs.h>
35#endif
36
37#if __STDC__
38#include <stdarg.h>
39#else
40#include <varargs.h>
41#endif
42
37#if __STDC__
38int
43int
44#if __STDC__
39asprintf(char **str, char const *fmt, ...)
40#else
45asprintf(char **str, char const *fmt, ...)
46#else
41int
42asprintf(str, fmt, va_alist)
43 char **str;
47asprintf(str, fmt, va_alist)
48 char **str;
44 char *fmt;
49 const char *fmt;
45 va_dcl
46#endif
47{
48 int ret;
49 va_list ap;
50 va_dcl
51#endif
52{
53 int ret;
54 va_list ap;
55 FILE f;
50
51#if __STDC__
52 va_start(ap, fmt);
53#else
54 va_start(ap);
55#endif
56
57#if __STDC__
58 va_start(ap, fmt);
59#else
60 va_start(ap);
61#endif
56 ret = vasprintf(str, fmt, ap);
62 f._file = -1;
63 f._flags = __SWR | __SSTR | __SALC;
64 f._bf._base = f._p = (unsigned char *)malloc(128);
65 if (f._bf._base == NULL) {
66 *str = NULL;
67 errno = ENOMEM;
68 return (-1);
69 }
70 f._bf._size = f._w = 127; /* Leave room for the NULL */
71 ret = vfprintf(&f, fmt, ap);
72 *f._p = '\0';
57 va_end(ap);
73 va_end(ap);
74 f._bf._base = realloc(f._bf._base, f._bf._size + 1);
75 if (f._bf._base == NULL) {
76 errno = ENOMEM;
77 ret = -1;
78 }
79 *str = (char *)f._bf._base;
58 return (ret);
59}
80 return (ret);
81}