dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libc / port / print / asprintf.c
blobbdc557a45ff1200a5aab7b4b8503849141001440
1 /*
2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /*
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.
25 #include <lint.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <errno.h>
33 #define INIT_SZ 128
35 /* VARARGS2 */
36 int
37 vasprintf(char **str, const char *format, va_list ap)
39 char string[INIT_SZ];
40 char *newstr;
41 int ret;
42 size_t len;
44 *str = NULL;
45 ret = vsnprintf(string, INIT_SZ, format, ap);
46 if (ret < 0) /* retain the value of errno from vsnprintf() */
47 return (-1);
48 if (ret < INIT_SZ) {
49 len = ret + 1;
50 if ((newstr = malloc(len)) == NULL)
51 return (-1); /* retain errno from malloc() */
52 (void) memcpy(newstr, string, len);
53 *str = newstr;
54 return (ret);
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().
61 for (;;) {
62 if (ret == INT_MAX) { /* Bad length */
63 errno = ENOMEM;
64 return (-1);
66 len = ret + 1;
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() */
71 free(newstr);
72 return (-1);
74 if (ret < len) {
75 *str = newstr;
76 return (ret);
78 free(newstr);
82 int
83 asprintf(char **str, const char *format, ...)
85 va_list ap;
86 int ret;
88 *str = NULL;
89 va_start(ap, format);
90 ret = vasprintf(str, format, ap);
91 va_end(ap);
93 return (ret);