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