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 #include <sm/gen.h> 16*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: vsnprintf.c,v 1.21 2001/03/04 23:28:41 ca Exp $") 17*7c478bd9Sstevel@tonic-gate #include <limits.h> 18*7c478bd9Sstevel@tonic-gate #include <sm/io.h> 19*7c478bd9Sstevel@tonic-gate #include "local.h" 20*7c478bd9Sstevel@tonic-gate 21*7c478bd9Sstevel@tonic-gate /* 22*7c478bd9Sstevel@tonic-gate ** SM_VSNPRINTF -- format data for "output" into a string 23*7c478bd9Sstevel@tonic-gate ** 24*7c478bd9Sstevel@tonic-gate ** Assigned 'str' to a "fake" file pointer. This allows common 25*7c478bd9Sstevel@tonic-gate ** o/p formatting function sm_vprintf() to be used. 26*7c478bd9Sstevel@tonic-gate ** 27*7c478bd9Sstevel@tonic-gate ** Parameters: 28*7c478bd9Sstevel@tonic-gate ** str -- location for output 29*7c478bd9Sstevel@tonic-gate ** n -- maximum size for o/p 30*7c478bd9Sstevel@tonic-gate ** fmt -- format directives 31*7c478bd9Sstevel@tonic-gate ** ap -- data unit vectors for use by 'fmt' 32*7c478bd9Sstevel@tonic-gate ** 33*7c478bd9Sstevel@tonic-gate ** Results: 34*7c478bd9Sstevel@tonic-gate ** result from sm_io_vfprintf() 35*7c478bd9Sstevel@tonic-gate ** 36*7c478bd9Sstevel@tonic-gate ** Side Effects: 37*7c478bd9Sstevel@tonic-gate ** Limits the size ('n') to INT_MAX. 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate int 41*7c478bd9Sstevel@tonic-gate sm_vsnprintf(str, n, fmt, ap) 42*7c478bd9Sstevel@tonic-gate char *str; 43*7c478bd9Sstevel@tonic-gate size_t n; 44*7c478bd9Sstevel@tonic-gate const char *fmt; 45*7c478bd9Sstevel@tonic-gate SM_VA_LOCAL_DECL 46*7c478bd9Sstevel@tonic-gate { 47*7c478bd9Sstevel@tonic-gate int ret; 48*7c478bd9Sstevel@tonic-gate char dummy; 49*7c478bd9Sstevel@tonic-gate SM_FILE_T fake; 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate /* While snprintf(3) specifies size_t stdio uses an int internally */ 52*7c478bd9Sstevel@tonic-gate if (n > INT_MAX) 53*7c478bd9Sstevel@tonic-gate n = INT_MAX; 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate /* Stdio internals do not deal correctly with zero length buffer */ 56*7c478bd9Sstevel@tonic-gate if (n == 0) 57*7c478bd9Sstevel@tonic-gate { 58*7c478bd9Sstevel@tonic-gate str = &dummy; 59*7c478bd9Sstevel@tonic-gate n = 1; 60*7c478bd9Sstevel@tonic-gate } 61*7c478bd9Sstevel@tonic-gate fake.sm_magic = SmFileMagic; 62*7c478bd9Sstevel@tonic-gate fake.f_timeout = SM_TIME_FOREVER; 63*7c478bd9Sstevel@tonic-gate fake.f_timeoutstate = SM_TIME_BLOCK; 64*7c478bd9Sstevel@tonic-gate fake.f_file = -1; 65*7c478bd9Sstevel@tonic-gate fake.f_flags = SMWR | SMSTR; 66*7c478bd9Sstevel@tonic-gate fake.f_bf.smb_base = fake.f_p = (unsigned char *)str; 67*7c478bd9Sstevel@tonic-gate fake.f_bf.smb_size = fake.f_w = n - 1; 68*7c478bd9Sstevel@tonic-gate fake.f_close = NULL; 69*7c478bd9Sstevel@tonic-gate fake.f_open = NULL; 70*7c478bd9Sstevel@tonic-gate fake.f_read = NULL; 71*7c478bd9Sstevel@tonic-gate fake.f_write = NULL; 72*7c478bd9Sstevel@tonic-gate fake.f_seek = NULL; 73*7c478bd9Sstevel@tonic-gate fake.f_setinfo = fake.f_getinfo = NULL; 74*7c478bd9Sstevel@tonic-gate fake.f_type = "sm_vsnprintf:fake"; 75*7c478bd9Sstevel@tonic-gate ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap); 76*7c478bd9Sstevel@tonic-gate *fake.f_p = 0; 77*7c478bd9Sstevel@tonic-gate return ret; 78*7c478bd9Sstevel@tonic-gate } 79