xref: /csrg-svn/lib/libc/stdio/snprintf.c (revision 46111)
1*46111Sbostic /*-
2*46111Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46111Sbostic  * All rights reserved.
4*46111Sbostic  *
5*46111Sbostic  * This code is derived from software contributed to Berkeley by
6*46111Sbostic  * Chris Torek.
7*46111Sbostic  *
8*46111Sbostic  * %sccs.include.redist.c%
9*46111Sbostic  */
10*46111Sbostic 
11*46111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*46111Sbostic static char sccsid[] = "@(#)snprintf.c	5.1 (Berkeley) 01/20/91";
13*46111Sbostic #endif /* LIBC_SCCS and not lint */
14*46111Sbostic 
15*46111Sbostic #include <stdio.h>
16*46111Sbostic #if __STDC__
17*46111Sbostic #include <stdarg.h>
18*46111Sbostic #else
19*46111Sbostic #include <varargs.h>
20*46111Sbostic #endif
21*46111Sbostic 
22*46111Sbostic #if __STDC__
23*46111Sbostic snprintf(char *str, size_t n, char const *fmt, ...)
24*46111Sbostic #else
25*46111Sbostic snprintf(str, n, fmt, va_alist)
26*46111Sbostic 	char *str;
27*46111Sbostic 	size_t n;
28*46111Sbostic 	char *fmt;
29*46111Sbostic 	va_dcl
30*46111Sbostic #endif
31*46111Sbostic {
32*46111Sbostic 	int ret;
33*46111Sbostic 	va_list ap;
34*46111Sbostic 	FILE f;
35*46111Sbostic 
36*46111Sbostic 	if ((int)n < 1)
37*46111Sbostic 		return (EOF);
38*46111Sbostic #if __STDC__
39*46111Sbostic 	va_start(ap, fmt);
40*46111Sbostic #else
41*46111Sbostic 	va_start(ap);
42*46111Sbostic #endif
43*46111Sbostic 	f._flags = __SWR | __SSTR;
44*46111Sbostic 	f._bf._base = f._p = (unsigned char *)str;
45*46111Sbostic 	f._bf._size = f._w = n - 1;
46*46111Sbostic 	ret = vfprintf(&f, fmt, ap);
47*46111Sbostic 	*f._p = 0;
48*46111Sbostic 	va_end(ap);
49*46111Sbostic 	return (ret);
50*46111Sbostic }
51