xref: /freebsd/lib/libc/stdio/vdprintf.c (revision 74f1007fcc838501c74a633792c3f01833bf65e1)
1ad760e6fSDavid Schultz /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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
85b5fa75aSEd 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 "namespace.h"
35ad760e6fSDavid Schultz #include <errno.h>
36ad760e6fSDavid Schultz #include <limits.h>
37ad760e6fSDavid Schultz #include <stdarg.h>
38ad760e6fSDavid Schultz #include <stdio.h>
39ad760e6fSDavid Schultz #include "un-namespace.h"
40ad760e6fSDavid Schultz 
41ad760e6fSDavid Schultz #include "local.h"
423c87aa1dSDavid Chisnall #include "xlocale_private.h"
43ad760e6fSDavid Schultz 
44ad760e6fSDavid Schultz int
vdprintf(int fd,const char * __restrict fmt,va_list ap)45ad760e6fSDavid Schultz vdprintf(int fd, const char * __restrict fmt, va_list ap)
46ad760e6fSDavid Schultz {
471b0181dfSJohn Baldwin 	FILE f = FAKE_FILE;
48ad760e6fSDavid Schultz 	unsigned char buf[BUFSIZ];
49*74f1007fSDag-Erling Smørgrav 	int serrno = errno;
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*74f1007fSDag-Erling Smørgrav 	if ((ret = __vfprintf(&f, __get_locale(), serrno, fmt, ap)) < 0)
67ad760e6fSDavid Schultz 		return (ret);
68ad760e6fSDavid Schultz 
69ad760e6fSDavid Schultz 	return (__fflush(&f) ? EOF : ret);
70ad760e6fSDavid Schultz }
71