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.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 SM_RCSID("@(#)$Id: vasprintf.c,v 1.26.2.1 2003/06/03 02:14:09 ca Exp $")
37 ** SM_VASPRINTF -- printf to a dynamically allocated string
39 ** Write 'printf' output to a dynamically allocated string
40 ** buffer which is returned to the caller.
43 ** str -- *str receives a pointer to the allocated string
44 ** fmt -- format directives for printing
45 ** ap -- variable argument list
48 ** On failure, set *str to NULL, set errno, and return -1.
50 ** On success, set *str to a pointer to a nul-terminated
51 ** string buffer containing printf output, and return the
52 ** length of the string (not counting the nul).
55 #define SM_VA_BUFSIZE 128
58 sm_vasprintf(str
, fmt
, ap
)
67 fake
.sm_magic
= SmFileMagic
;
68 fake
.f_timeout
= SM_TIME_FOREVER
;
69 fake
.f_timeoutstate
= SM_TIME_BLOCK
;
71 fake
.f_flags
= SMWR
| SMSTR
| SMALC
;
72 fake
.f_bf
.smb_base
= fake
.f_p
= (unsigned char *)sm_malloc(SM_VA_BUFSIZE
);
73 if (fake
.f_bf
.smb_base
== NULL
)
80 fake
.f_setinfo
= fake
.f_getinfo
= NULL
;
81 fake
.f_type
= "sm_vasprintf:fake";
82 fake
.f_bf
.smb_size
= fake
.f_w
= SM_VA_BUFSIZE
- 1;
83 fake
.f_timeout
= SM_TIME_FOREVER
;
84 ret
= sm_io_vfprintf(&fake
, SM_TIME_FOREVER
, fmt
, ap
);
89 /* use no more space than necessary */
90 base
= (unsigned char *) sm_realloc(fake
.f_bf
.smb_base
, ret
+ 1);
97 if (fake
.f_bf
.smb_base
!= NULL
)
99 sm_free(fake
.f_bf
.smb_base
);
100 fake
.f_bf
.smb_base
= NULL
;