2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * By using this file, you agree to the terms and conditions set
11 * forth in the LICENSE file which can be found at the top level of
12 * the sendmail distribution.
15 #pragma ident "%Z%%M% %I% %E% SMI"
18 SM_RCSID("@(#)$Id: snprintf.c,v 1.24 2006/10/12 21:50:10 ca Exp $")
20 #include <sm/varargs.h>
22 #include <sm/string.h>
26 ** SM_SNPRINTF -- format a string to a memory location of restricted size
29 ** str -- memory location to place formatted string
30 ** n -- size of buffer pointed to by str, capped to
31 ** a maximum of INT_MAX
32 ** fmt -- the formatting directives
33 ** ... -- the data to satisfy the formatting
37 ** Success: number of bytes that would have been written
38 ** to str, not including the trailing '\0',
39 ** up to a maximum of INT_MAX, as if there was
40 ** no buffer size limitation. If the result >= n
41 ** then the output was truncated.
44 ** If n > 0, then between 0 and n-1 bytes of formatted output
45 ** are written into 'str', followed by a '\0'.
50 sm_snprintf(char *str
, size_t n
, char const *fmt
, ...)
52 sm_snprintf(str
, n
, fmt
, va_alist
)
57 #endif /* SM_VA_STD */
63 /* While snprintf(3) specifies size_t stdio uses an int internally */
68 /* XXX put this into a static? */
69 fake
.sm_magic
= SmFileMagic
;
71 fake
.f_flags
= SMWR
| SMSTR
;
72 fake
.f_cookie
= &fake
;
73 fake
.f_bf
.smb_base
= fake
.f_p
= (unsigned char *)str
;
74 fake
.f_bf
.smb_size
= fake
.f_w
= n
? n
- 1 : 0;
75 fake
.f_timeout
= SM_TIME_FOREVER
;
76 fake
.f_timeoutstate
= SM_TIME_BLOCK
;
82 fake
.f_setinfo
= fake
.f_getinfo
= NULL
;
83 fake
.f_type
= "sm_snprintf:fake";
84 ret
= sm_io_vfprintf(&fake
, SM_TIME_FOREVER
, fmt
, ap
);