1*e15daa8bSchristos /* $NetBSD: dprintf.c,v 1.1.1.3 2023/08/18 18:36:49 christos Exp $ */
2ae9cfef6Schristos
3ae9cfef6Schristos /*
4ae9cfef6Schristos * Copyright (c) Ian F. Darwin 1986-1995.
5ae9cfef6Schristos * Software written by Ian F. Darwin and others;
6ae9cfef6Schristos * maintained 1995-present by Christos Zoulas and others.
7ae9cfef6Schristos *
8ae9cfef6Schristos * Redistribution and use in source and binary forms, with or without
9ae9cfef6Schristos * modification, are permitted provided that the following conditions
10ae9cfef6Schristos * are met:
11ae9cfef6Schristos * 1. Redistributions of source code must retain the above copyright
12ae9cfef6Schristos * notice immediately at the beginning of the file, without modification,
13ae9cfef6Schristos * this list of conditions, and the following disclaimer.
14ae9cfef6Schristos * 2. Redistributions in binary form must reproduce the above copyright
15ae9cfef6Schristos * notice, this list of conditions and the following disclaimer in the
16ae9cfef6Schristos * documentation and/or other materials provided with the distribution.
17ae9cfef6Schristos *
18ae9cfef6Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ae9cfef6Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ae9cfef6Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ae9cfef6Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22ae9cfef6Schristos * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ae9cfef6Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ae9cfef6Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ae9cfef6Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ae9cfef6Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ae9cfef6Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ae9cfef6Schristos * SUCH DAMAGE.
29ae9cfef6Schristos */
30ae9cfef6Schristos #include "file.h"
31ae9cfef6Schristos
32ae9cfef6Schristos #ifndef lint
33ae9cfef6Schristos #if 0
34*e15daa8bSchristos FILE_RCSID("@(#)$File: dprintf.c,v 1.4 2022/09/24 20:30:13 christos Exp $")
35ae9cfef6Schristos #else
36*e15daa8bSchristos __RCSID("$NetBSD: dprintf.c,v 1.1.1.3 2023/08/18 18:36:49 christos Exp $");
37ae9cfef6Schristos #endif
38ae9cfef6Schristos #endif /* lint */
39ae9cfef6Schristos
40ae9cfef6Schristos #include <assert.h>
41ae9cfef6Schristos #include <unistd.h>
42ae9cfef6Schristos #include <stdio.h>
43ae9cfef6Schristos #include <stdarg.h>
44ae9cfef6Schristos
45ae9cfef6Schristos int
dprintf(int fd,const char * fmt,...)46ae9cfef6Schristos dprintf(int fd, const char *fmt, ...)
47ae9cfef6Schristos {
48ae9cfef6Schristos va_list ap;
49ae9cfef6Schristos /* Simpler than using vasprintf() here, since we never need more */
50ae9cfef6Schristos char buf[1024];
51ae9cfef6Schristos int len;
52ae9cfef6Schristos
53ae9cfef6Schristos va_start(ap, fmt);
54ae9cfef6Schristos len = vsnprintf(buf, sizeof(buf), fmt, ap);
55ae9cfef6Schristos va_end(ap);
56ae9cfef6Schristos
57ae9cfef6Schristos if ((size_t)len >= sizeof(buf))
58ae9cfef6Schristos return -1;
59ae9cfef6Schristos
60ae9cfef6Schristos if (write(fd, buf, (size_t)len) != len)
61ae9cfef6Schristos return -1;
62ae9cfef6Schristos
63ae9cfef6Schristos return len;
64ae9cfef6Schristos }
65