vsnprintf.c revision 98121
167754Smsmith/*
267754Smsmith * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
367754Smsmith *      All rights reserved.
467754Smsmith * Copyright (c) 1990, 1993
567754Smsmith *	The Regents of the University of California.  All rights reserved.
667754Smsmith *
7217365Sjkim * This code is derived from software contributed to Berkeley by
8278970Sjkim * Chris Torek.
970243Smsmith *
1067754Smsmith * By using this file, you agree to the terms and conditions set
11217365Sjkim * forth in the LICENSE file which can be found at the top level of
12217365Sjkim * the sendmail distribution.
13217365Sjkim */
14217365Sjkim
15217365Sjkim#include <sm/gen.h>
16217365SjkimSM_RCSID("@(#)$Id: vsnprintf.c,v 1.23 2001/09/11 04:04:49 gshapiro Exp $")
17217365Sjkim#include <limits.h>
18217365Sjkim#include <sm/io.h>
19217365Sjkim#include "local.h"
20217365Sjkim
21217365Sjkim/*
22217365Sjkim**  SM_VSNPRINTF -- format data for "output" into a string
23217365Sjkim**
24217365Sjkim**	Assigned 'str' to a "fake" file pointer. This allows common
2567754Smsmith**	o/p formatting function sm_vprintf() to be used.
26217365Sjkim**
27217365Sjkim**	Parameters:
28217365Sjkim**		str -- location for output
2967754Smsmith**		n -- maximum size for o/p
30217365Sjkim**		fmt -- format directives
31217365Sjkim**		ap -- data unit vectors for use by 'fmt'
32217365Sjkim**
33217365Sjkim**	Results:
34217365Sjkim**		result from sm_io_vfprintf()
35217365Sjkim**
36217365Sjkim**	Side Effects:
37217365Sjkim**		Limits the size ('n') to INT_MAX.
38217365Sjkim*/
39217365Sjkim
40217365Sjkimint
41217365Sjkimsm_vsnprintf(str, n, fmt, ap)
42217365Sjkim	char *str;
4367754Smsmith	size_t n;
44272444Sjkim	const char *fmt;
4567754Smsmith	SM_VA_LOCAL_DECL
46193341Sjkim{
47193341Sjkim	int ret;
48193341Sjkim	char dummy;
4967754Smsmith	SM_FILE_T fake;
5077424Smsmith
5191116Smsmith	/* While snprintf(3) specifies size_t stdio uses an int internally */
5267754Smsmith	if (n > INT_MAX)
5367754Smsmith		n = INT_MAX;
54231844Sjkim
5577424Smsmith	/* Stdio internals do not deal correctly with zero length buffer */
5667754Smsmith	if (n == 0)
5767754Smsmith	{
5867754Smsmith		str = &dummy;
5967754Smsmith		n = 1;
6067754Smsmith	}
6167754Smsmith	fake.sm_magic = SmFileMagic;
6267754Smsmith	fake.f_timeout = SM_TIME_FOREVER;
6367754Smsmith	fake.f_timeoutstate = SM_TIME_BLOCK;
6467754Smsmith	fake.f_file = -1;
6577424Smsmith	fake.f_flags = SMWR | SMSTR;
6667754Smsmith	fake.f_bf.smb_base = fake.f_p = (unsigned char *)str;
6767754Smsmith	fake.f_bf.smb_size = fake.f_w = n - 1;
68151937Sjkim	fake.f_close = NULL;
69151937Sjkim	fake.f_open = NULL;
7067754Smsmith	fake.f_read = NULL;
7187031Smsmith	fake.f_write = NULL;
7267754Smsmith	fake.f_seek = NULL;
7367754Smsmith	fake.f_setinfo = fake.f_getinfo = NULL;
74167802Sjkim	fake.f_type = "sm_vsnprintf:fake";
7567754Smsmith	ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap);
7667754Smsmith	*fake.f_p = 0;
77167802Sjkim	return ret;
7867754Smsmith}
79167802Sjkim