1189356Sdas/*-
2189356Sdas * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3189356Sdas * All rights reserved.
4189356Sdas *
5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
6227753Stheraven * All rights reserved.
7227753Stheraven * Portions of this software were developed by David Chisnall
8227753Stheraven * under sponsorship from the FreeBSD Foundation.
9227753Stheraven *
10189356Sdas * Redistribution and use in source and binary forms, with or without
11189356Sdas * modification, are permitted provided that the following conditions
12189356Sdas * are met:
13189356Sdas * 1. Redistributions of source code must retain the above copyright
14189356Sdas *    notice, this list of conditions and the following disclaimer.
15189356Sdas * 2. Redistributions in binary form must reproduce the above copyright
16189356Sdas *    notice, this list of conditions and the following disclaimer in the
17189356Sdas *    documentation and/or other materials provided with the distribution.
18189356Sdas *
19189356Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20189356Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21189356Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22189356Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23189356Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24189356Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25189356Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26189356Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27189356Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28189356Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29189356Sdas * SUCH DAMAGE.
30189356Sdas */
31189356Sdas
32189356Sdas#include <sys/cdefs.h>
33189356Sdas__FBSDID("$FreeBSD$");
34189356Sdas
35189356Sdas#include "namespace.h"
36189356Sdas#include <errno.h>
37189356Sdas#include <limits.h>
38189356Sdas#include <stdarg.h>
39189356Sdas#include <stdio.h>
40189356Sdas#include "un-namespace.h"
41189356Sdas
42189356Sdas#include "local.h"
43227753Stheraven#include "xlocale_private.h"
44189356Sdas
45189356Sdasint
46189356Sdasvdprintf(int fd, const char * __restrict fmt, va_list ap)
47189356Sdas{
48205021Sjhb	FILE f = FAKE_FILE;
49189356Sdas	unsigned char buf[BUFSIZ];
50189356Sdas	int ret;
51189356Sdas
52189356Sdas	if (fd > SHRT_MAX) {
53189356Sdas		errno = EMFILE;
54189356Sdas		return (EOF);
55189356Sdas	}
56189356Sdas
57189356Sdas	f._p = buf;
58189356Sdas	f._w = sizeof(buf);
59189356Sdas	f._flags = __SWR;
60189356Sdas	f._file = fd;
61189356Sdas	f._cookie = &f;
62189356Sdas	f._write = __swrite;
63189356Sdas	f._bf._base = buf;
64189356Sdas	f._bf._size = sizeof(buf);
65189356Sdas
66227753Stheraven	if ((ret = __vfprintf(&f, __get_locale(), fmt, ap)) < 0)
67189356Sdas		return (ret);
68189356Sdas
69189356Sdas	return (__fflush(&f) ? EOF : ret);
70189356Sdas}
71