xref: /csrg-svn/lib/libc/stdio/snprintf.c (revision 61180)
146111Sbostic /*-
2*61180Sbostic  * Copyright (c) 1990, 1993
3*61180Sbostic  *	The Regents of the University of California.  All rights reserved.
446111Sbostic  *
546111Sbostic  * This code is derived from software contributed to Berkeley by
646111Sbostic  * Chris Torek.
746111Sbostic  *
846111Sbostic  * %sccs.include.redist.c%
946111Sbostic  */
1046111Sbostic 
1146111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*61180Sbostic static char sccsid[] = "@(#)snprintf.c	8.1 (Berkeley) 06/04/93";
1346111Sbostic #endif /* LIBC_SCCS and not lint */
1446111Sbostic 
1546111Sbostic #include <stdio.h>
1646111Sbostic #if __STDC__
1746111Sbostic #include <stdarg.h>
1846111Sbostic #else
1946111Sbostic #include <varargs.h>
2046111Sbostic #endif
2146111Sbostic 
2246111Sbostic #if __STDC__
snprintf(char * str,size_t n,char const * fmt,...)2346111Sbostic snprintf(char *str, size_t n, char const *fmt, ...)
2446111Sbostic #else
2546111Sbostic snprintf(str, n, fmt, va_alist)
2646111Sbostic 	char *str;
2746111Sbostic 	size_t n;
2846111Sbostic 	char *fmt;
2946111Sbostic 	va_dcl
3046111Sbostic #endif
3146111Sbostic {
3246111Sbostic 	int ret;
3346111Sbostic 	va_list ap;
3446111Sbostic 	FILE f;
3546111Sbostic 
3646111Sbostic 	if ((int)n < 1)
3746111Sbostic 		return (EOF);
3846111Sbostic #if __STDC__
3946111Sbostic 	va_start(ap, fmt);
4046111Sbostic #else
4146111Sbostic 	va_start(ap);
4246111Sbostic #endif
4346111Sbostic 	f._flags = __SWR | __SSTR;
4446111Sbostic 	f._bf._base = f._p = (unsigned char *)str;
4546111Sbostic 	f._bf._size = f._w = n - 1;
4646111Sbostic 	ret = vfprintf(&f, fmt, ap);
4746111Sbostic 	*f._p = 0;
4846111Sbostic 	va_end(ap);
4946111Sbostic 	return (ret);
5046111Sbostic }
51