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