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