snprintf.c revision 165903
1153923Siedowse/*-
2153923Siedowse * Copyright (c) 1990, 1993
3153923Siedowse *	The Regents of the University of California.  All rights reserved.
4153923Siedowse *
5153923Siedowse * This code is derived from software contributed to Berkeley by
6153923Siedowse * Chris Torek.
7153923Siedowse *
8153923Siedowse * Redistribution and use in source and binary forms, with or without
9153923Siedowse * modification, are permitted provided that the following conditions
10153923Siedowse * are met:
11153923Siedowse * 1. Redistributions of source code must retain the above copyright
12153923Siedowse *    notice, this list of conditions and the following disclaimer.
13153923Siedowse * 2. Redistributions in binary form must reproduce the above copyright
14153923Siedowse *    notice, this list of conditions and the following disclaimer in the
15153923Siedowse *    documentation and/or other materials provided with the distribution.
16153923Siedowse * 4. Neither the name of the University nor the names of its contributors
17153923Siedowse *    may be used to endorse or promote products derived from this software
18153923Siedowse *    without specific prior written permission.
19153923Siedowse *
20153923Siedowse * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21153923Siedowse * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22153923Siedowse * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23153923Siedowse * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24153923Siedowse * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25153923Siedowse * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26153923Siedowse * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27194860Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28206622Suqs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29153923Siedowse * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30153923Siedowse * SUCH DAMAGE.
31194860Sthompsa */
32194860Sthompsa
33194860Sthompsa#if defined(LIBC_SCCS) && !defined(lint)
34194860Sthompsastatic char sccsid[] = "@(#)snprintf.c	8.1 (Berkeley) 6/4/93";
35194860Sthompsa#endif /* LIBC_SCCS and not lint */
36194860Sthompsa#include <sys/cdefs.h>
37194860Sthompsa__FBSDID("$FreeBSD: head/lib/libc/stdio/snprintf.c 165903 2007-01-09 00:28:16Z imp $");
38194860Sthompsa
39194860Sthompsa#include <limits.h>
40194860Sthompsa#include <stdio.h>
41194860Sthompsa#include <stdarg.h>
42194860Sthompsa
43194860Sthompsa#include "local.h"
44194860Sthompsa
45194860Sthompsaint
46194860Sthompsasnprintf(char * __restrict str, size_t n, char const * __restrict fmt, ...)
47153923Siedowse{
48153923Siedowse	size_t on;
49153923Siedowse	int ret;
50194860Sthompsa	va_list ap;
51194860Sthompsa	FILE f;
52194860Sthompsa	struct __sFILEX ext;
53194860Sthompsa
54194860Sthompsa	on = n;
55194860Sthompsa	if (n != 0)
56194860Sthompsa		n--;
57194860Sthompsa	if (n > INT_MAX)
58194860Sthompsa		n = INT_MAX;
59194860Sthompsa	va_start(ap, fmt);
60194860Sthompsa	f._file = -1;
61194860Sthompsa	f._flags = __SWR | __SSTR;
62194860Sthompsa	f._bf._base = f._p = (unsigned char *)str;
63194860Sthompsa	f._bf._size = f._w = n;
64194860Sthompsa	f._extra = &ext;
65194860Sthompsa	INITEXTRA(&f);
66194860Sthompsa	ret = __vfprintf(&f, fmt, ap);
67194860Sthompsa	if (on > 0)
68194860Sthompsa		*f._p = '\0';
69194860Sthompsa	va_end(ap);
70194860Sthompsa	return (ret);
71194860Sthompsa}
72194860Sthompsa