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: snprintf.c,v 1.21 2001/03/02 23:53:41 ca Exp $") 19*7c478bd9Sstevel@tonic-gate #include <limits.h> 20*7c478bd9Sstevel@tonic-gate #include <sm/varargs.h> 21*7c478bd9Sstevel@tonic-gate #include <sm/io.h> 22*7c478bd9Sstevel@tonic-gate #include "local.h" 23*7c478bd9Sstevel@tonic-gate 24*7c478bd9Sstevel@tonic-gate /* 25*7c478bd9Sstevel@tonic-gate ** SM_SNPRINTF -- format a string to a memory location of restricted size 26*7c478bd9Sstevel@tonic-gate ** 27*7c478bd9Sstevel@tonic-gate ** Parameters: 28*7c478bd9Sstevel@tonic-gate ** str -- memory location to place formatted string 29*7c478bd9Sstevel@tonic-gate ** n -- size of buffer pointed to by str, capped to 30*7c478bd9Sstevel@tonic-gate ** a maximum of INT_MAX 31*7c478bd9Sstevel@tonic-gate ** fmt -- the formatting directives 32*7c478bd9Sstevel@tonic-gate ** ... -- the data to satisfy the formatting 33*7c478bd9Sstevel@tonic-gate ** 34*7c478bd9Sstevel@tonic-gate ** Returns: 35*7c478bd9Sstevel@tonic-gate ** Failure: -1 36*7c478bd9Sstevel@tonic-gate ** Success: number of bytes that would have been written 37*7c478bd9Sstevel@tonic-gate ** to str, not including the trailing '\0', 38*7c478bd9Sstevel@tonic-gate ** up to a maximum of INT_MAX, as if there was 39*7c478bd9Sstevel@tonic-gate ** no buffer size limitation. If the result >= n 40*7c478bd9Sstevel@tonic-gate ** then the output was truncated. 41*7c478bd9Sstevel@tonic-gate ** 42*7c478bd9Sstevel@tonic-gate ** Side Effects: 43*7c478bd9Sstevel@tonic-gate ** If n > 0, then between 0 and n-1 bytes of formatted output 44*7c478bd9Sstevel@tonic-gate ** are written into 'str', followed by a '\0'. 45*7c478bd9Sstevel@tonic-gate */ 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate int 48*7c478bd9Sstevel@tonic-gate #if SM_VA_STD 49*7c478bd9Sstevel@tonic-gate sm_snprintf(char *str, size_t n, char const *fmt, ...) 50*7c478bd9Sstevel@tonic-gate #else /* SM_VA_STD */ 51*7c478bd9Sstevel@tonic-gate sm_snprintf(str, n, fmt, va_alist) 52*7c478bd9Sstevel@tonic-gate char *str; 53*7c478bd9Sstevel@tonic-gate size_t n; 54*7c478bd9Sstevel@tonic-gate char *fmt; 55*7c478bd9Sstevel@tonic-gate va_dcl 56*7c478bd9Sstevel@tonic-gate #endif /* SM_VA_STD */ 57*7c478bd9Sstevel@tonic-gate { 58*7c478bd9Sstevel@tonic-gate int ret; 59*7c478bd9Sstevel@tonic-gate SM_VA_LOCAL_DECL 60*7c478bd9Sstevel@tonic-gate SM_FILE_T fake; 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate /* While snprintf(3) specifies size_t stdio uses an int internally */ 63*7c478bd9Sstevel@tonic-gate if (n > INT_MAX) 64*7c478bd9Sstevel@tonic-gate n = INT_MAX; 65*7c478bd9Sstevel@tonic-gate SM_VA_START(ap, fmt); 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate /* XXX put this into a static? */ 68*7c478bd9Sstevel@tonic-gate fake.sm_magic = SmFileMagic; 69*7c478bd9Sstevel@tonic-gate fake.f_file = -1; 70*7c478bd9Sstevel@tonic-gate fake.f_flags = SMWR | SMSTR; 71*7c478bd9Sstevel@tonic-gate fake.f_cookie = &fake; 72*7c478bd9Sstevel@tonic-gate fake.f_bf.smb_base = fake.f_p = (unsigned char *)str; 73*7c478bd9Sstevel@tonic-gate fake.f_bf.smb_size = fake.f_w = n ? n - 1 : 0; 74*7c478bd9Sstevel@tonic-gate fake.f_timeout = SM_TIME_FOREVER; 75*7c478bd9Sstevel@tonic-gate fake.f_timeoutstate = SM_TIME_BLOCK; 76*7c478bd9Sstevel@tonic-gate fake.f_close = NULL; 77*7c478bd9Sstevel@tonic-gate fake.f_open = NULL; 78*7c478bd9Sstevel@tonic-gate fake.f_read = NULL; 79*7c478bd9Sstevel@tonic-gate fake.f_write = NULL; 80*7c478bd9Sstevel@tonic-gate fake.f_seek = NULL; 81*7c478bd9Sstevel@tonic-gate fake.f_setinfo = fake.f_getinfo = NULL; 82*7c478bd9Sstevel@tonic-gate fake.f_type = "sm_snprintf:fake"; 83*7c478bd9Sstevel@tonic-gate ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap); 84*7c478bd9Sstevel@tonic-gate if (n > 0) 85*7c478bd9Sstevel@tonic-gate *fake.f_p = '\0'; 86*7c478bd9Sstevel@tonic-gate SM_VA_END(ap); 87*7c478bd9Sstevel@tonic-gate return ret; 88*7c478bd9Sstevel@tonic-gate } 89