xref: /minix3/lib/libc/stdio/vsnprintf.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: vsnprintf.c,v 1.28 2014/09/29 14:58:33 christos Exp $	*/
2efcfaf4bSBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Chris Torek.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
332fe8fb19SBen Gras  */
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)vsnprintf.c	8.1 (Berkeley) 6/4/93";
392fe8fb19SBen Gras #else
40*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: vsnprintf.c,v 1.28 2014/09/29 14:58:33 christos Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include "namespace.h"
452fe8fb19SBen Gras 
462fe8fb19SBen Gras #include <assert.h>
472fe8fb19SBen Gras #include <errno.h>
4884d9c625SLionel Sambuc #include <locale.h>
49efcfaf4bSBen Gras #include <stdio.h>
502fe8fb19SBen Gras #include "reentrant.h"
5184d9c625SLionel Sambuc #include "setlocale_local.h"
522fe8fb19SBen Gras #include "local.h"
532fe8fb19SBen Gras 
542fe8fb19SBen Gras #if defined(_FORTIFY_SOURCE) && !defined(__lint__)
552fe8fb19SBen Gras #undef vsnprintf
562fe8fb19SBen Gras #define vsnprintf _vsnprintf
5784d9c625SLionel Sambuc #undef snprintf
5884d9c625SLionel Sambuc #define snprintf _snprintf
592fe8fb19SBen Gras #endif
602fe8fb19SBen Gras 
612fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(vsnprintf,_vsnprintf)622fe8fb19SBen Gras __weak_alias(vsnprintf,_vsnprintf)
6384d9c625SLionel Sambuc __weak_alias(vsnprintf_l,_vsnprintf_l)
6484d9c625SLionel Sambuc __weak_alias(snprintf,_snprintf)
6584d9c625SLionel Sambuc __weak_alias(snprintf_l,_snprintf_l)
662fe8fb19SBen Gras #endif
67efcfaf4bSBen Gras 
68efcfaf4bSBen Gras int
6984d9c625SLionel Sambuc vsnprintf_l(char *str, size_t n, locale_t loc, const char *fmt, va_list ap)
70efcfaf4bSBen Gras {
712fe8fb19SBen Gras 	int ret;
722fe8fb19SBen Gras 	FILE f;
732fe8fb19SBen Gras 	struct __sfileext fext;
742fe8fb19SBen Gras 	unsigned char dummy[1];
75efcfaf4bSBen Gras 
762fe8fb19SBen Gras 	_DIAGASSERT(n == 0 || str != NULL);
772fe8fb19SBen Gras 	_DIAGASSERT(fmt != NULL);
78efcfaf4bSBen Gras 
79*0a6a1f1dSLionel Sambuc 	if (n > INT_MAX) {
80*0a6a1f1dSLionel Sambuc 		errno = EOVERFLOW;
81f14fb602SLionel Sambuc 		return -1;
82efcfaf4bSBen Gras 	}
83efcfaf4bSBen Gras 
842fe8fb19SBen Gras 	_FILEEXT_SETUP(&f, &fext);
852fe8fb19SBen Gras 	f._file = -1;
862fe8fb19SBen Gras 	f._flags = __SWR | __SSTR;
872fe8fb19SBen Gras 	if (n == 0) {
882fe8fb19SBen Gras 		f._bf._base = f._p = dummy;
892fe8fb19SBen Gras 		f._bf._size = f._w = 0;
902fe8fb19SBen Gras 	} else {
912fe8fb19SBen Gras 		f._bf._base = f._p = (unsigned char *)str;
92f14fb602SLionel Sambuc 		_DIAGASSERT(__type_fit(int, n - 1));
93f14fb602SLionel Sambuc 		f._bf._size = f._w = (int)(n - 1);
94efcfaf4bSBen Gras 	}
9584d9c625SLionel Sambuc 	ret = __vfprintf_unlocked_l(&f, loc, fmt, ap);
962fe8fb19SBen Gras 	*f._p = 0;
97f14fb602SLionel Sambuc 	return ret;
982fe8fb19SBen Gras }
9984d9c625SLionel Sambuc 
10084d9c625SLionel Sambuc int
vsnprintf(char * str,size_t n,const char * fmt,va_list ap)10184d9c625SLionel Sambuc vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
10284d9c625SLionel Sambuc {
10384d9c625SLionel Sambuc 	return vsnprintf_l(str, n, _current_locale(), fmt, ap);
10484d9c625SLionel Sambuc }
10584d9c625SLionel Sambuc 
10684d9c625SLionel Sambuc int
snprintf(char * str,size_t n,const char * fmt,...)10784d9c625SLionel Sambuc snprintf(char *str, size_t n, const char *fmt, ...)
10884d9c625SLionel Sambuc {
10984d9c625SLionel Sambuc 	va_list ap;
11084d9c625SLionel Sambuc 	int ret;
11184d9c625SLionel Sambuc 
11284d9c625SLionel Sambuc 	va_start(ap, fmt);
11384d9c625SLionel Sambuc 	ret = vsnprintf(str, n, fmt, ap);
11484d9c625SLionel Sambuc 	va_end(ap);
11584d9c625SLionel Sambuc 	return ret;
11684d9c625SLionel Sambuc }
11784d9c625SLionel Sambuc 
11884d9c625SLionel Sambuc int
snprintf_l(char * str,size_t n,locale_t loc,const char * fmt,...)11984d9c625SLionel Sambuc snprintf_l(char *str, size_t n, locale_t loc, const char *fmt, ...)
12084d9c625SLionel Sambuc {
12184d9c625SLionel Sambuc 	va_list ap;
12284d9c625SLionel Sambuc 	int ret;
12384d9c625SLionel Sambuc 
12484d9c625SLionel Sambuc 	va_start(ap, fmt);
12584d9c625SLionel Sambuc 	ret = vsnprintf_l(str, n, loc, fmt, ap);
12684d9c625SLionel Sambuc 	va_end(ap);
12784d9c625SLionel Sambuc 	return ret;
12884d9c625SLionel Sambuc }
129