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