140266059SGregory Neil Shapiro /* 25dd76dd0SGregory Neil Shapiro * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers. 340266059SGregory Neil Shapiro * All rights reserved. 440266059SGregory Neil Shapiro * Copyright (c) 1990, 1993 540266059SGregory Neil Shapiro * The Regents of the University of California. All rights reserved. 640266059SGregory Neil Shapiro * 740266059SGregory Neil Shapiro * This code is derived from software contributed to Berkeley by 840266059SGregory Neil Shapiro * Chris Torek. 940266059SGregory Neil Shapiro * 1040266059SGregory Neil Shapiro * By using this file, you agree to the terms and conditions set 1140266059SGregory Neil Shapiro * forth in the LICENSE file which can be found at the top level of 1240266059SGregory Neil Shapiro * the sendmail distribution. 1340266059SGregory Neil Shapiro */ 1440266059SGregory Neil Shapiro 1540266059SGregory Neil Shapiro #include <sm/gen.h> 164313cc83SGregory Neil Shapiro SM_RCSID("@(#)$Id: vsnprintf.c,v 1.24 2013-11-22 20:51:44 ca Exp $") 1740266059SGregory Neil Shapiro #include <limits.h> 1840266059SGregory Neil Shapiro #include <sm/io.h> 1940266059SGregory Neil Shapiro #include "local.h" 2040266059SGregory Neil Shapiro 2140266059SGregory Neil Shapiro /* 2240266059SGregory Neil Shapiro ** SM_VSNPRINTF -- format data for "output" into a string 2340266059SGregory Neil Shapiro ** 2440266059SGregory Neil Shapiro ** Assigned 'str' to a "fake" file pointer. This allows common 2540266059SGregory Neil Shapiro ** o/p formatting function sm_vprintf() to be used. 2640266059SGregory Neil Shapiro ** 2740266059SGregory Neil Shapiro ** Parameters: 2840266059SGregory Neil Shapiro ** str -- location for output 2940266059SGregory Neil Shapiro ** n -- maximum size for o/p 3040266059SGregory Neil Shapiro ** fmt -- format directives 3140266059SGregory Neil Shapiro ** ap -- data unit vectors for use by 'fmt' 3240266059SGregory Neil Shapiro ** 3340266059SGregory Neil Shapiro ** Results: 3440266059SGregory Neil Shapiro ** result from sm_io_vfprintf() 3540266059SGregory Neil Shapiro ** 3640266059SGregory Neil Shapiro ** Side Effects: 3740266059SGregory Neil Shapiro ** Limits the size ('n') to INT_MAX. 3840266059SGregory Neil Shapiro */ 3940266059SGregory Neil Shapiro 4040266059SGregory Neil Shapiro int 4140266059SGregory Neil Shapiro sm_vsnprintf(str, n, fmt, ap) 4240266059SGregory Neil Shapiro char *str; 4340266059SGregory Neil Shapiro size_t n; 4440266059SGregory Neil Shapiro const char *fmt; 45*2fb4f839SGregory Neil Shapiro va_list ap; 4640266059SGregory Neil Shapiro { 4740266059SGregory Neil Shapiro int ret; 4840266059SGregory Neil Shapiro char dummy; 4940266059SGregory Neil Shapiro SM_FILE_T fake; 5040266059SGregory Neil Shapiro 5140266059SGregory Neil Shapiro /* While snprintf(3) specifies size_t stdio uses an int internally */ 5240266059SGregory Neil Shapiro if (n > INT_MAX) 5340266059SGregory Neil Shapiro n = INT_MAX; 5440266059SGregory Neil Shapiro 5540266059SGregory Neil Shapiro /* Stdio internals do not deal correctly with zero length buffer */ 5640266059SGregory Neil Shapiro if (n == 0) 5740266059SGregory Neil Shapiro { 5840266059SGregory Neil Shapiro str = &dummy; 5940266059SGregory Neil Shapiro n = 1; 6040266059SGregory Neil Shapiro } 6140266059SGregory Neil Shapiro fake.sm_magic = SmFileMagic; 6240266059SGregory Neil Shapiro fake.f_timeout = SM_TIME_FOREVER; 6340266059SGregory Neil Shapiro fake.f_timeoutstate = SM_TIME_BLOCK; 6440266059SGregory Neil Shapiro fake.f_file = -1; 6540266059SGregory Neil Shapiro fake.f_flags = SMWR | SMSTR; 6640266059SGregory Neil Shapiro fake.f_bf.smb_base = fake.f_p = (unsigned char *)str; 6740266059SGregory Neil Shapiro fake.f_bf.smb_size = fake.f_w = n - 1; 6840266059SGregory Neil Shapiro fake.f_close = NULL; 6940266059SGregory Neil Shapiro fake.f_open = NULL; 7040266059SGregory Neil Shapiro fake.f_read = NULL; 7140266059SGregory Neil Shapiro fake.f_write = NULL; 7240266059SGregory Neil Shapiro fake.f_seek = NULL; 7340266059SGregory Neil Shapiro fake.f_setinfo = fake.f_getinfo = NULL; 7440266059SGregory Neil Shapiro fake.f_type = "sm_vsnprintf:fake"; 7540266059SGregory Neil Shapiro ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap); 7640266059SGregory Neil Shapiro *fake.f_p = 0; 7740266059SGregory Neil Shapiro return ret; 7840266059SGregory Neil Shapiro } 79