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.
16 SM_RCSID("@(#)$Id: snprintf.c,v 1.24 2006/10/12 21:50:10 ca Exp $")
18 #include <sm/varargs.h>
20 #include <sm/string.h>
24 ** SM_SNPRINTF -- format a string to a memory location of restricted size
27 ** str -- memory location to place formatted string
28 ** n -- size of buffer pointed to by str, capped to
29 ** a maximum of INT_MAX
30 ** fmt -- the formatting directives
31 ** ... -- the data to satisfy the formatting
35 ** Success: number of bytes that would have been written
36 ** to str, not including the trailing '\0',
37 ** up to a maximum of INT_MAX, as if there was
38 ** no buffer size limitation. If the result >= n
39 ** then the output was truncated.
42 ** If n > 0, then between 0 and n-1 bytes of formatted output
43 ** are written into 'str', followed by a '\0'.
48 sm_snprintf(char *str
, size_t n
, char const *fmt
, ...)
50 sm_snprintf(str
, n
, fmt
, va_alist
)
55 #endif /* SM_VA_STD */
61 /* While snprintf(3) specifies size_t stdio uses an int internally */
66 /* XXX put this into a static? */
67 fake
.sm_magic
= SmFileMagic
;
69 fake
.f_flags
= SMWR
| SMSTR
;
70 fake
.f_cookie
= &fake
;
71 fake
.f_bf
.smb_base
= fake
.f_p
= (unsigned char *)str
;
72 fake
.f_bf
.smb_size
= fake
.f_w
= n
? n
- 1 : 0;
73 fake
.f_timeout
= SM_TIME_FOREVER
;
74 fake
.f_timeoutstate
= SM_TIME_BLOCK
;
80 fake
.f_setinfo
= fake
.f_getinfo
= NULL
;
81 fake
.f_type
= "sm_snprintf:fake";
82 ret
= sm_io_vfprintf(&fake
, SM_TIME_FOREVER
, fmt
, ap
);