Remove some unnecessary casts.
[dragonfly.git] / contrib / sendmail-8.14 / libsm / vasprintf.c
blob69d85763399510f6a325e4d75969efcd8147743d
1 /*
2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
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 */
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 #include <sm/gen.h>
27 SM_RCSID("@(#)$Id: vasprintf.c,v 1.27 2003/06/03 02:14:24 ca Exp $")
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <sm/io.h>
31 #include <sm/heap.h>
32 #include "local.h"
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.
40 ** Parameters:
41 ** str -- *str receives a pointer to the allocated string
42 ** fmt -- format directives for printing
43 ** ap -- variable argument list
45 ** Results:
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
55 int
56 sm_vasprintf(str, fmt, ap)
57 char **str;
58 const char *fmt;
59 SM_VA_LOCAL_DECL
61 int ret;
62 SM_FILE_T fake;
63 unsigned char *base;
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';
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;
94 err:
95 if (fake.f_bf.smb_base != NULL)
97 sm_free(fake.f_bf.smb_base);
98 fake.f_bf.smb_base = NULL;
100 err2:
101 *str = NULL;
102 errno = ENOMEM;
103 return -1;