xref: /freebsd/contrib/sendmail/libsm/vasprintf.c (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1 /*
2  * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers.
3  *      All rights reserved.
4  *
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.
8  */
9 
10 /*
11  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
12  *
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.
16  *
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.
24  */
25 
26 #include <sm/gen.h>
27 SM_RCSID("@(#)$Id: vasprintf.c,v 1.28 2013-11-22 20:51:44 ca Exp $")
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <sm/io.h>
31 #include <sm/heap.h>
32 #include "local.h"
33 
34 /*
35 **  SM_VASPRINTF -- printf to a dynamically allocated string
36 **
37 **  Write 'printf' output to a dynamically allocated string
38 **  buffer which is returned to the caller.
39 **
40 **	Parameters:
41 **		str -- *str receives a pointer to the allocated string
42 **		fmt -- format directives for printing
43 **		ap -- variable argument list
44 **
45 **	Results:
46 **		On failure, set *str to NULL, set errno, and return -1.
47 **
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).
51 */
52 
53 #define SM_VA_BUFSIZE	128
54 
55 int
56 sm_vasprintf(str, fmt, ap)
57 	char **str;
58 	const char *fmt;
59 	SM_VA_LOCAL_DECL
60 {
61 	int ret;
62 	SM_FILE_T fake;
63 	unsigned char *base;
64 
65 	fake.sm_magic = SmFileMagic;
66 	fake.f_timeout = SM_TIME_FOREVER;
67 	fake.f_timeoutstate = SM_TIME_BLOCK;
68 	fake.f_file = -1;
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)
72 		goto err2;
73 	fake.f_close = NULL;
74 	fake.f_open = NULL;
75 	fake.f_read = NULL;
76 	fake.f_write = NULL;
77 	fake.f_seek = 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);
83 	if (ret == -1)
84 		goto err;
85 	*fake.f_p = '\0';
86 
87 	/* use no more space than necessary */
88 	base = (unsigned char *) sm_realloc(fake.f_bf.smb_base, ret + 1);
89 	if (base == NULL)
90 		goto err;
91 	*str = (char *)base;
92 	return ret;
93 
94 err:
95 	if (fake.f_bf.smb_base != NULL)
96 	{
97 		sm_free(fake.f_bf.smb_base);
98 		fake.f_bf.smb_base = NULL;
99 	}
100 err2:
101 	*str = NULL;
102 	errno = ENOMEM;
103 	return -1;
104 }
105