summaryrefslogtreecommitdiff
path: root/gnu/usr.sbin/sendmail/libsm/vsnprintf.c
blob: 98a62d2fe5eaa1c505979118ce298709c8e2e727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
 *      All rights reserved.
 * Copyright (c) 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Sendmail: vsnprintf.c,v 1.23 2001/09/11 04:04:49 gshapiro Exp $")
#include <limits.h>
#include <sm/io.h>
#include "local.h"

/*
**  SM_VSNPRINTF -- format data for "output" into a string
**
**	Assigned 'str' to a "fake" file pointer. This allows common
**	o/p formatting function sm_vprintf() to be used.
**
**	Parameters:
**		str -- location for output
**		n -- maximum size for o/p
**		fmt -- format directives
**		ap -- data unit vectors for use by 'fmt'
**
**	Results:
**		result from sm_io_vfprintf()
**
**	Side Effects:
**		Limits the size ('n') to INT_MAX.
*/

int
sm_vsnprintf(str, n, fmt, ap)
	char *str;
	size_t n;
	const char *fmt;
	SM_VA_LOCAL_DECL
{
	int ret;
	char dummy;
	SM_FILE_T fake;

	/* While snprintf(3) specifies size_t stdio uses an int internally */
	if (n > INT_MAX)
		n = INT_MAX;

	/* Stdio internals do not deal correctly with zero length buffer */
	if (n == 0)
	{
		str = &dummy;
		n = 1;
	}
	fake.sm_magic = SmFileMagic;
	fake.f_timeout = SM_TIME_FOREVER;
	fake.f_timeoutstate = SM_TIME_BLOCK;
	fake.f_file = -1;
	fake.f_flags = SMWR | SMSTR;
	fake.f_bf.smb_base = fake.f_p = (unsigned char *)str;
	fake.f_bf.smb_size = fake.f_w = n - 1;
	fake.f_close = NULL;
	fake.f_open = NULL;
	fake.f_read = NULL;
	fake.f_write = NULL;
	fake.f_seek = NULL;
	fake.f_setinfo = fake.f_getinfo = NULL;
	fake.f_type = "sm_vsnprintf:fake";
	ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap);
	*fake.f_p = 0;
	return ret;
}