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