8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sendmail / libsm / vasprintf.c
blobd8a173ce13b9cc8518a8f8284c89e298e29f8f76
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 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sm/gen.h>
29 SM_RCSID("@(#)$Id: vasprintf.c,v 1.26.2.1 2003/06/03 02:14:09 ca Exp $")
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <sm/io.h>
33 #include <sm/heap.h>
34 #include "local.h"
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.
42 ** Parameters:
43 ** str -- *str receives a pointer to the allocated string
44 ** fmt -- format directives for printing
45 ** ap -- variable argument list
47 ** Results:
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
57 int
58 sm_vasprintf(str, fmt, ap)
59 char **str;
60 const char *fmt;
61 SM_VA_LOCAL_DECL
63 int ret;
64 SM_FILE_T fake;
65 unsigned char *base;
67 fake.sm_magic = SmFileMagic;
68 fake.f_timeout = SM_TIME_FOREVER;
69 fake.f_timeoutstate = SM_TIME_BLOCK;
70 fake.f_file = -1;
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)
74 goto err2;
75 fake.f_close = NULL;
76 fake.f_open = NULL;
77 fake.f_read = NULL;
78 fake.f_write = NULL;
79 fake.f_seek = 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);
85 if (ret == -1)
86 goto err;
87 *fake.f_p = '\0';
89 /* use no more space than necessary */
90 base = (unsigned char *) sm_realloc(fake.f_bf.smb_base, ret + 1);
91 if (base == NULL)
92 goto err;
93 *str = (char *)base;
94 return ret;
96 err:
97 if (fake.f_bf.smb_base != NULL)
99 sm_free(fake.f_bf.smb_base);
100 fake.f_bf.smb_base = NULL;
102 err2:
103 *str = NULL;
104 errno = ENOMEM;
105 return -1;