2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * Copyright (c) 2004 Darren Tucker.
9 * Based originally on asprintf.c from OpenBSD:
10 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 vasprintf(char **str
, const char *format
, va_list ap
)
45 ret
= vsnprintf(string
, INIT_SZ
, format
, ap
);
46 if (ret
< 0) /* retain the value of errno from vsnprintf() */
50 if ((newstr
= malloc(len
)) == NULL
)
51 return (-1); /* retain errno from malloc() */
52 (void) memcpy(newstr
, string
, len
);
57 * We will perform this loop more than once only if some other
58 * thread modifies one of the vasprintf() arguments after our
59 * previous call to vsnprintf().
62 if (ret
== INT_MAX
) { /* Bad length */
67 if ((newstr
= malloc(len
)) == NULL
)
68 return (-1); /* retain errno from malloc() */
69 ret
= vsnprintf(newstr
, len
, format
, ap
);
70 if (ret
< 0) { /* retain errno from vsnprintf() */
83 asprintf(char **str
, const char *format
, ...)
90 ret
= vasprintf(str
, format
, ap
);