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 * Copyright (c) 1990, 1993 5*7c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 6*7c478bd9Sstevel@tonic-gate * 7*7c478bd9Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by 8*7c478bd9Sstevel@tonic-gate * Chris Torek. 9*7c478bd9Sstevel@tonic-gate * 10*7c478bd9Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set 11*7c478bd9Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of 12*7c478bd9Sstevel@tonic-gate * the sendmail distribution. 13*7c478bd9Sstevel@tonic-gate */ 14*7c478bd9Sstevel@tonic-gate 15*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 16*7c478bd9Sstevel@tonic-gate 17*7c478bd9Sstevel@tonic-gate #include <sm/gen.h> 18*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: vsnprintf.c,v 1.21 2001/03/04 23:28:41 ca Exp $") 19*7c478bd9Sstevel@tonic-gate #include <limits.h> 20*7c478bd9Sstevel@tonic-gate #include <sm/io.h> 21*7c478bd9Sstevel@tonic-gate #include "local.h" 22*7c478bd9Sstevel@tonic-gate 23*7c478bd9Sstevel@tonic-gate /* 24*7c478bd9Sstevel@tonic-gate ** SM_VSNPRINTF -- format data for "output" into a string 25*7c478bd9Sstevel@tonic-gate ** 26*7c478bd9Sstevel@tonic-gate ** Assigned 'str' to a "fake" file pointer. This allows common 27*7c478bd9Sstevel@tonic-gate ** o/p formatting function sm_vprintf() to be used. 28*7c478bd9Sstevel@tonic-gate ** 29*7c478bd9Sstevel@tonic-gate ** Parameters: 30*7c478bd9Sstevel@tonic-gate ** str -- location for output 31*7c478bd9Sstevel@tonic-gate ** n -- maximum size for o/p 32*7c478bd9Sstevel@tonic-gate ** fmt -- format directives 33*7c478bd9Sstevel@tonic-gate ** ap -- data unit vectors for use by 'fmt' 34*7c478bd9Sstevel@tonic-gate ** 35*7c478bd9Sstevel@tonic-gate ** Results: 36*7c478bd9Sstevel@tonic-gate ** result from sm_io_vfprintf() 37*7c478bd9Sstevel@tonic-gate ** 38*7c478bd9Sstevel@tonic-gate ** Side Effects: 39*7c478bd9Sstevel@tonic-gate ** Limits the size ('n') to INT_MAX. 40*7c478bd9Sstevel@tonic-gate */ 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate int 43*7c478bd9Sstevel@tonic-gate sm_vsnprintf(str, n, fmt, ap) 44*7c478bd9Sstevel@tonic-gate char *str; 45*7c478bd9Sstevel@tonic-gate size_t n; 46*7c478bd9Sstevel@tonic-gate const char *fmt; 47*7c478bd9Sstevel@tonic-gate SM_VA_LOCAL_DECL 48*7c478bd9Sstevel@tonic-gate { 49*7c478bd9Sstevel@tonic-gate int ret; 50*7c478bd9Sstevel@tonic-gate char dummy; 51*7c478bd9Sstevel@tonic-gate SM_FILE_T fake; 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate /* While snprintf(3) specifies size_t stdio uses an int internally */ 54*7c478bd9Sstevel@tonic-gate if (n > INT_MAX) 55*7c478bd9Sstevel@tonic-gate n = INT_MAX; 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate /* Stdio internals do not deal correctly with zero length buffer */ 58*7c478bd9Sstevel@tonic-gate if (n == 0) 59*7c478bd9Sstevel@tonic-gate { 60*7c478bd9Sstevel@tonic-gate str = &dummy; 61*7c478bd9Sstevel@tonic-gate n = 1; 62*7c478bd9Sstevel@tonic-gate } 63*7c478bd9Sstevel@tonic-gate fake.sm_magic = SmFileMagic; 64*7c478bd9Sstevel@tonic-gate fake.f_timeout = SM_TIME_FOREVER; 65*7c478bd9Sstevel@tonic-gate fake.f_timeoutstate = SM_TIME_BLOCK; 66*7c478bd9Sstevel@tonic-gate fake.f_file = -1; 67*7c478bd9Sstevel@tonic-gate fake.f_flags = SMWR | SMSTR; 68*7c478bd9Sstevel@tonic-gate fake.f_bf.smb_base = fake.f_p = (unsigned char *)str; 69*7c478bd9Sstevel@tonic-gate fake.f_bf.smb_size = fake.f_w = n - 1; 70*7c478bd9Sstevel@tonic-gate fake.f_close = NULL; 71*7c478bd9Sstevel@tonic-gate fake.f_open = NULL; 72*7c478bd9Sstevel@tonic-gate fake.f_read = NULL; 73*7c478bd9Sstevel@tonic-gate fake.f_write = NULL; 74*7c478bd9Sstevel@tonic-gate fake.f_seek = NULL; 75*7c478bd9Sstevel@tonic-gate fake.f_setinfo = fake.f_getinfo = NULL; 76*7c478bd9Sstevel@tonic-gate fake.f_type = "sm_vsnprintf:fake"; 77*7c478bd9Sstevel@tonic-gate ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap); 78*7c478bd9Sstevel@tonic-gate *fake.f_p = 0; 79*7c478bd9Sstevel@tonic-gate return ret; 80*7c478bd9Sstevel@tonic-gate } 81