1298178Sdelphij/* 2298178Sdelphij * Copyright (c) Ian F. Darwin 1986-1995. 3298178Sdelphij * Software written by Ian F. Darwin and others; 4298178Sdelphij * maintained 1995-present by Christos Zoulas and others. 5354939Sdelphij * 6298178Sdelphij * Redistribution and use in source and binary forms, with or without 7298178Sdelphij * modification, are permitted provided that the following conditions 8298178Sdelphij * are met: 9298178Sdelphij * 1. Redistributions of source code must retain the above copyright 10298178Sdelphij * notice immediately at the beginning of the file, without modification, 11298178Sdelphij * this list of conditions, and the following disclaimer. 12298178Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 13298178Sdelphij * notice, this list of conditions and the following disclaimer in the 14298178Sdelphij * documentation and/or other materials provided with the distribution. 15354939Sdelphij * 16298178Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17298178Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18298178Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19298178Sdelphij * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20298178Sdelphij * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21298178Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22298178Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23298178Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24298178Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25298178Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26298178Sdelphij * SUCH DAMAGE. 27298178Sdelphij */ 28298178Sdelphij#include "file.h" 29298178Sdelphij 30298178Sdelphij#ifndef lint 31354939SdelphijFILE_RCSID("@(#)$File: dprintf.c,v 1.2 2018/09/09 20:33:28 christos Exp $") 32298178Sdelphij#endif /* lint */ 33298178Sdelphij 34298178Sdelphij#include <assert.h> 35298178Sdelphij#include <unistd.h> 36298178Sdelphij#include <stdio.h> 37298178Sdelphij#include <stdarg.h> 38298178Sdelphij 39298178Sdelphijint 40298178Sdelphijdprintf(int fd, const char *fmt, ...) 41298178Sdelphij{ 42298178Sdelphij va_list ap; 43298178Sdelphij /* Simpler than using vasprintf() here, since we never need more */ 44298178Sdelphij char buf[1024]; 45298178Sdelphij int len; 46298178Sdelphij 47298178Sdelphij va_start(ap, fmt); 48298178Sdelphij len = vsnprintf(buf, sizeof(buf), fmt, ap); 49298178Sdelphij va_end(ap); 50298178Sdelphij 51298178Sdelphij if ((size_t)len >= sizeof(buf)) 52298178Sdelphij return -1; 53298178Sdelphij 54298178Sdelphij if (write(fd, buf, (size_t)len) != len) 55298178Sdelphij return -1; 56298178Sdelphij 57298178Sdelphij return len; 58298178Sdelphij} 59