1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *      All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate  */
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate /*
11*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
12*7c478bd9Sstevel@tonic-gate  *
13*7c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
14*7c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
15*7c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
18*7c478bd9Sstevel@tonic-gate  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
19*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
20*7c478bd9Sstevel@tonic-gate  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21*7c478bd9Sstevel@tonic-gate  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
22*7c478bd9Sstevel@tonic-gate  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23*7c478bd9Sstevel@tonic-gate  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
27*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: vasprintf.c,v 1.26.2.1 2003/06/03 02:14:09 ca Exp $")
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <errno.h>
30*7c478bd9Sstevel@tonic-gate #include <sm/io.h>
31*7c478bd9Sstevel@tonic-gate #include <sm/heap.h>
32*7c478bd9Sstevel@tonic-gate #include "local.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate **  SM_VASPRINTF -- printf to a dynamically allocated string
36*7c478bd9Sstevel@tonic-gate **
37*7c478bd9Sstevel@tonic-gate **  Write 'printf' output to a dynamically allocated string
38*7c478bd9Sstevel@tonic-gate **  buffer which is returned to the caller.
39*7c478bd9Sstevel@tonic-gate **
40*7c478bd9Sstevel@tonic-gate **	Parameters:
41*7c478bd9Sstevel@tonic-gate **		str -- *str receives a pointer to the allocated string
42*7c478bd9Sstevel@tonic-gate **		fmt -- format directives for printing
43*7c478bd9Sstevel@tonic-gate **		ap -- variable argument list
44*7c478bd9Sstevel@tonic-gate **
45*7c478bd9Sstevel@tonic-gate **	Results:
46*7c478bd9Sstevel@tonic-gate **		On failure, set *str to NULL, set errno, and return -1.
47*7c478bd9Sstevel@tonic-gate **
48*7c478bd9Sstevel@tonic-gate **		On success, set *str to a pointer to a nul-terminated
49*7c478bd9Sstevel@tonic-gate **		string buffer containing printf output,	and return the
50*7c478bd9Sstevel@tonic-gate **		length of the string (not counting the nul).
51*7c478bd9Sstevel@tonic-gate */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #define SM_VA_BUFSIZE	128
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate int
56*7c478bd9Sstevel@tonic-gate sm_vasprintf(str, fmt, ap)
57*7c478bd9Sstevel@tonic-gate 	char **str;
58*7c478bd9Sstevel@tonic-gate 	const char *fmt;
59*7c478bd9Sstevel@tonic-gate 	SM_VA_LOCAL_DECL
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	int ret;
62*7c478bd9Sstevel@tonic-gate 	SM_FILE_T fake;
63*7c478bd9Sstevel@tonic-gate 	unsigned char *base;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	fake.sm_magic = SmFileMagic;
66*7c478bd9Sstevel@tonic-gate 	fake.f_timeout = SM_TIME_FOREVER;
67*7c478bd9Sstevel@tonic-gate 	fake.f_timeoutstate = SM_TIME_BLOCK;
68*7c478bd9Sstevel@tonic-gate 	fake.f_file = -1;
69*7c478bd9Sstevel@tonic-gate 	fake.f_flags = SMWR | SMSTR | SMALC;
70*7c478bd9Sstevel@tonic-gate 	fake.f_bf.smb_base = fake.f_p = (unsigned char *)sm_malloc(SM_VA_BUFSIZE);
71*7c478bd9Sstevel@tonic-gate 	if (fake.f_bf.smb_base == NULL)
72*7c478bd9Sstevel@tonic-gate 		goto err2;
73*7c478bd9Sstevel@tonic-gate 	fake.f_close = NULL;
74*7c478bd9Sstevel@tonic-gate 	fake.f_open = NULL;
75*7c478bd9Sstevel@tonic-gate 	fake.f_read = NULL;
76*7c478bd9Sstevel@tonic-gate 	fake.f_write = NULL;
77*7c478bd9Sstevel@tonic-gate 	fake.f_seek = NULL;
78*7c478bd9Sstevel@tonic-gate 	fake.f_setinfo = fake.f_getinfo = NULL;
79*7c478bd9Sstevel@tonic-gate 	fake.f_type = "sm_vasprintf:fake";
80*7c478bd9Sstevel@tonic-gate 	fake.f_bf.smb_size = fake.f_w = SM_VA_BUFSIZE - 1;
81*7c478bd9Sstevel@tonic-gate 	fake.f_timeout = SM_TIME_FOREVER;
82*7c478bd9Sstevel@tonic-gate 	ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap);
83*7c478bd9Sstevel@tonic-gate 	if (ret == -1)
84*7c478bd9Sstevel@tonic-gate 		goto err;
85*7c478bd9Sstevel@tonic-gate 	*fake.f_p = '\0';
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	/* use no more space than necessary */
88*7c478bd9Sstevel@tonic-gate 	base = (unsigned char *) sm_realloc(fake.f_bf.smb_base, ret + 1);
89*7c478bd9Sstevel@tonic-gate 	if (base == NULL)
90*7c478bd9Sstevel@tonic-gate 		goto err;
91*7c478bd9Sstevel@tonic-gate 	*str = (char *)base;
92*7c478bd9Sstevel@tonic-gate 	return ret;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate err:
95*7c478bd9Sstevel@tonic-gate 	if (fake.f_bf.smb_base != NULL)
96*7c478bd9Sstevel@tonic-gate 	{
97*7c478bd9Sstevel@tonic-gate 		sm_free(fake.f_bf.smb_base);
98*7c478bd9Sstevel@tonic-gate 		fake.f_bf.smb_base = NULL;
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate err2:
101*7c478bd9Sstevel@tonic-gate 	*str = NULL;
102*7c478bd9Sstevel@tonic-gate 	errno = ENOMEM;
103*7c478bd9Sstevel@tonic-gate 	return -1;
104*7c478bd9Sstevel@tonic-gate }
105