2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
11 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
18 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
20 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
22 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 SM_RCSID("@(#)$Id: vasprintf.c,v 1.27 2003/06/03 02:14:24 ca Exp $")
35 ** SM_VASPRINTF -- printf to a dynamically allocated string
37 ** Write 'printf' output to a dynamically allocated string
38 ** buffer which is returned to the caller.
41 ** str -- *str receives a pointer to the allocated string
42 ** fmt -- format directives for printing
43 ** ap -- variable argument list
46 ** On failure, set *str to NULL, set errno, and return -1.
48 ** On success, set *str to a pointer to a nul-terminated
49 ** string buffer containing printf output, and return the
50 ** length of the string (not counting the nul).
53 #define SM_VA_BUFSIZE 128
56 sm_vasprintf(str, fmt, ap)
65 fake.sm_magic = SmFileMagic;
66 fake.f_timeout = SM_TIME_FOREVER;
67 fake.f_timeoutstate = SM_TIME_BLOCK;
69 fake.f_flags = SMWR | SMSTR | SMALC;
70 fake.f_bf.smb_base = fake.f_p = (unsigned char *)sm_malloc(SM_VA_BUFSIZE);
71 if (fake.f_bf.smb_base == NULL)
78 fake.f_setinfo = fake.f_getinfo = NULL;
79 fake.f_type = "sm_vasprintf:fake";
80 fake.f_bf.smb_size = fake.f_w = SM_VA_BUFSIZE - 1;
81 fake.f_timeout = SM_TIME_FOREVER;
82 ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap);
87 /* use no more space than necessary */
88 base = (unsigned char *) sm_realloc(fake.f_bf.smb_base, ret + 1);
95 if (fake.f_bf.smb_base != NULL)
97 sm_free(fake.f_bf.smb_base);
98 fake.f_bf.smb_base = NULL;