xref: /freebsd/lib/libc/stdio/vdprintf.c (revision 3c87aa1d3dc1d8dad3efad322852a8e1e76dee55)
1ad760e6fSDavid Schultz /*-
2ad760e6fSDavid Schultz  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3ad760e6fSDavid Schultz  * All rights reserved.
4ad760e6fSDavid Schultz  *
5*3c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
6*3c87aa1dSDavid Chisnall  * All rights reserved.
7*3c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
8*3c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
9*3c87aa1dSDavid Chisnall  *
10ad760e6fSDavid Schultz  * Redistribution and use in source and binary forms, with or without
11ad760e6fSDavid Schultz  * modification, are permitted provided that the following conditions
12ad760e6fSDavid Schultz  * are met:
13ad760e6fSDavid Schultz  * 1. Redistributions of source code must retain the above copyright
14ad760e6fSDavid Schultz  *    notice, this list of conditions and the following disclaimer.
15ad760e6fSDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
16ad760e6fSDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
17ad760e6fSDavid Schultz  *    documentation and/or other materials provided with the distribution.
18ad760e6fSDavid Schultz  *
19ad760e6fSDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20ad760e6fSDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ad760e6fSDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ad760e6fSDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23ad760e6fSDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ad760e6fSDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ad760e6fSDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ad760e6fSDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ad760e6fSDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ad760e6fSDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ad760e6fSDavid Schultz  * SUCH DAMAGE.
30ad760e6fSDavid Schultz  */
31ad760e6fSDavid Schultz 
32ad760e6fSDavid Schultz #include <sys/cdefs.h>
33ad760e6fSDavid Schultz __FBSDID("$FreeBSD$");
34ad760e6fSDavid Schultz 
35ad760e6fSDavid Schultz #include "namespace.h"
36ad760e6fSDavid Schultz #include <errno.h>
37ad760e6fSDavid Schultz #include <limits.h>
38ad760e6fSDavid Schultz #include <stdarg.h>
39ad760e6fSDavid Schultz #include <stdio.h>
40ad760e6fSDavid Schultz #include "un-namespace.h"
41ad760e6fSDavid Schultz 
42ad760e6fSDavid Schultz #include "local.h"
43*3c87aa1dSDavid Chisnall #include "xlocale_private.h"
44ad760e6fSDavid Schultz 
45ad760e6fSDavid Schultz int
46ad760e6fSDavid Schultz vdprintf(int fd, const char * __restrict fmt, va_list ap)
47ad760e6fSDavid Schultz {
481b0181dfSJohn Baldwin 	FILE f = FAKE_FILE;
49ad760e6fSDavid Schultz 	unsigned char buf[BUFSIZ];
50ad760e6fSDavid Schultz 	int ret;
51ad760e6fSDavid Schultz 
52ad760e6fSDavid Schultz 	if (fd > SHRT_MAX) {
53ad760e6fSDavid Schultz 		errno = EMFILE;
54ad760e6fSDavid Schultz 		return (EOF);
55ad760e6fSDavid Schultz 	}
56ad760e6fSDavid Schultz 
57ad760e6fSDavid Schultz 	f._p = buf;
58ad760e6fSDavid Schultz 	f._w = sizeof(buf);
59ad760e6fSDavid Schultz 	f._flags = __SWR;
60ad760e6fSDavid Schultz 	f._file = fd;
61ad760e6fSDavid Schultz 	f._cookie = &f;
62ad760e6fSDavid Schultz 	f._write = __swrite;
63ad760e6fSDavid Schultz 	f._bf._base = buf;
64ad760e6fSDavid Schultz 	f._bf._size = sizeof(buf);
65ad760e6fSDavid Schultz 
66*3c87aa1dSDavid Chisnall 	if ((ret = __vfprintf(&f, __get_locale(), fmt, ap)) < 0)
67ad760e6fSDavid Schultz 		return (ret);
68ad760e6fSDavid Schultz 
69ad760e6fSDavid Schultz 	return (__fflush(&f) ? EOF : ret);
70ad760e6fSDavid Schultz }
71