13e41d09dSXin LI /*
23e41d09dSXin LI * Copyright (c) Ian F. Darwin 1986-1995.
33e41d09dSXin LI * Software written by Ian F. Darwin and others;
43e41d09dSXin LI * maintained 1995-present by Christos Zoulas and others.
53e41d09dSXin LI *
63e41d09dSXin LI * Redistribution and use in source and binary forms, with or without
73e41d09dSXin LI * modification, are permitted provided that the following conditions
83e41d09dSXin LI * are met:
93e41d09dSXin LI * 1. Redistributions of source code must retain the above copyright
103e41d09dSXin LI * notice immediately at the beginning of the file, without modification,
113e41d09dSXin LI * this list of conditions, and the following disclaimer.
123e41d09dSXin LI * 2. Redistributions in binary form must reproduce the above copyright
133e41d09dSXin LI * notice, this list of conditions and the following disclaimer in the
143e41d09dSXin LI * documentation and/or other materials provided with the distribution.
153e41d09dSXin LI *
163e41d09dSXin LI * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
173e41d09dSXin LI * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183e41d09dSXin LI * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193e41d09dSXin LI * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
203e41d09dSXin LI * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
213e41d09dSXin LI * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
223e41d09dSXin LI * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233e41d09dSXin LI * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
243e41d09dSXin LI * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253e41d09dSXin LI * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263e41d09dSXin LI * SUCH DAMAGE.
273e41d09dSXin LI */
283e41d09dSXin LI #include "file.h"
293e41d09dSXin LI
303e41d09dSXin LI #ifndef lint
31*898496eeSXin LI FILE_RCSID("@(#)$File: dprintf.c,v 1.4 2022/09/24 20:30:13 christos Exp $")
323e41d09dSXin LI #endif /* lint */
333e41d09dSXin LI
343e41d09dSXin LI #include <assert.h>
353e41d09dSXin LI #include <unistd.h>
363e41d09dSXin LI #include <stdio.h>
373e41d09dSXin LI #include <stdarg.h>
383e41d09dSXin LI
393e41d09dSXin LI int
dprintf(int fd,const char * fmt,...)403e41d09dSXin LI dprintf(int fd, const char *fmt, ...)
413e41d09dSXin LI {
423e41d09dSXin LI va_list ap;
433e41d09dSXin LI /* Simpler than using vasprintf() here, since we never need more */
443e41d09dSXin LI char buf[1024];
453e41d09dSXin LI int len;
463e41d09dSXin LI
473e41d09dSXin LI va_start(ap, fmt);
483e41d09dSXin LI len = vsnprintf(buf, sizeof(buf), fmt, ap);
493e41d09dSXin LI va_end(ap);
503e41d09dSXin LI
513e41d09dSXin LI if ((size_t)len >= sizeof(buf))
523e41d09dSXin LI return -1;
533e41d09dSXin LI
543e41d09dSXin LI if (write(fd, buf, (size_t)len) != len)
553e41d09dSXin LI return -1;
563e41d09dSXin LI
573e41d09dSXin LI return len;
583e41d09dSXin LI }
59