xref: /titanic_52/usr/src/cmd/sendmail/libsm/vasprintf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
29*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: vasprintf.c,v 1.26.2.1 2003/06/03 02:14:09 ca Exp $")
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <errno.h>
32*7c478bd9Sstevel@tonic-gate #include <sm/io.h>
33*7c478bd9Sstevel@tonic-gate #include <sm/heap.h>
34*7c478bd9Sstevel@tonic-gate #include "local.h"
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate /*
37*7c478bd9Sstevel@tonic-gate **  SM_VASPRINTF -- printf to a dynamically allocated string
38*7c478bd9Sstevel@tonic-gate **
39*7c478bd9Sstevel@tonic-gate **  Write 'printf' output to a dynamically allocated string
40*7c478bd9Sstevel@tonic-gate **  buffer which is returned to the caller.
41*7c478bd9Sstevel@tonic-gate **
42*7c478bd9Sstevel@tonic-gate **	Parameters:
43*7c478bd9Sstevel@tonic-gate **		str -- *str receives a pointer to the allocated string
44*7c478bd9Sstevel@tonic-gate **		fmt -- format directives for printing
45*7c478bd9Sstevel@tonic-gate **		ap -- variable argument list
46*7c478bd9Sstevel@tonic-gate **
47*7c478bd9Sstevel@tonic-gate **	Results:
48*7c478bd9Sstevel@tonic-gate **		On failure, set *str to NULL, set errno, and return -1.
49*7c478bd9Sstevel@tonic-gate **
50*7c478bd9Sstevel@tonic-gate **		On success, set *str to a pointer to a nul-terminated
51*7c478bd9Sstevel@tonic-gate **		string buffer containing printf output,	and return the
52*7c478bd9Sstevel@tonic-gate **		length of the string (not counting the nul).
53*7c478bd9Sstevel@tonic-gate */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #define SM_VA_BUFSIZE	128
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate int
58*7c478bd9Sstevel@tonic-gate sm_vasprintf(str, fmt, ap)
59*7c478bd9Sstevel@tonic-gate 	char **str;
60*7c478bd9Sstevel@tonic-gate 	const char *fmt;
61*7c478bd9Sstevel@tonic-gate 	SM_VA_LOCAL_DECL
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	int ret;
64*7c478bd9Sstevel@tonic-gate 	SM_FILE_T fake;
65*7c478bd9Sstevel@tonic-gate 	unsigned char *base;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	fake.sm_magic = SmFileMagic;
68*7c478bd9Sstevel@tonic-gate 	fake.f_timeout = SM_TIME_FOREVER;
69*7c478bd9Sstevel@tonic-gate 	fake.f_timeoutstate = SM_TIME_BLOCK;
70*7c478bd9Sstevel@tonic-gate 	fake.f_file = -1;
71*7c478bd9Sstevel@tonic-gate 	fake.f_flags = SMWR | SMSTR | SMALC;
72*7c478bd9Sstevel@tonic-gate 	fake.f_bf.smb_base = fake.f_p = (unsigned char *)sm_malloc(SM_VA_BUFSIZE);
73*7c478bd9Sstevel@tonic-gate 	if (fake.f_bf.smb_base == NULL)
74*7c478bd9Sstevel@tonic-gate 		goto err2;
75*7c478bd9Sstevel@tonic-gate 	fake.f_close = NULL;
76*7c478bd9Sstevel@tonic-gate 	fake.f_open = NULL;
77*7c478bd9Sstevel@tonic-gate 	fake.f_read = NULL;
78*7c478bd9Sstevel@tonic-gate 	fake.f_write = NULL;
79*7c478bd9Sstevel@tonic-gate 	fake.f_seek = NULL;
80*7c478bd9Sstevel@tonic-gate 	fake.f_setinfo = fake.f_getinfo = NULL;
81*7c478bd9Sstevel@tonic-gate 	fake.f_type = "sm_vasprintf:fake";
82*7c478bd9Sstevel@tonic-gate 	fake.f_bf.smb_size = fake.f_w = SM_VA_BUFSIZE - 1;
83*7c478bd9Sstevel@tonic-gate 	fake.f_timeout = SM_TIME_FOREVER;
84*7c478bd9Sstevel@tonic-gate 	ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap);
85*7c478bd9Sstevel@tonic-gate 	if (ret == -1)
86*7c478bd9Sstevel@tonic-gate 		goto err;
87*7c478bd9Sstevel@tonic-gate 	*fake.f_p = '\0';
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	/* use no more space than necessary */
90*7c478bd9Sstevel@tonic-gate 	base = (unsigned char *) sm_realloc(fake.f_bf.smb_base, ret + 1);
91*7c478bd9Sstevel@tonic-gate 	if (base == NULL)
92*7c478bd9Sstevel@tonic-gate 		goto err;
93*7c478bd9Sstevel@tonic-gate 	*str = (char *)base;
94*7c478bd9Sstevel@tonic-gate 	return ret;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate err:
97*7c478bd9Sstevel@tonic-gate 	if (fake.f_bf.smb_base != NULL)
98*7c478bd9Sstevel@tonic-gate 	{
99*7c478bd9Sstevel@tonic-gate 		sm_free(fake.f_bf.smb_base);
100*7c478bd9Sstevel@tonic-gate 		fake.f_bf.smb_base = NULL;
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate err2:
103*7c478bd9Sstevel@tonic-gate 	*str = NULL;
104*7c478bd9Sstevel@tonic-gate 	errno = ENOMEM;
105*7c478bd9Sstevel@tonic-gate 	return -1;
106*7c478bd9Sstevel@tonic-gate }
107