1330569Sgordon/* 2330569Sgordon * Copyright (c) Ian F. Darwin 1986-1995. 3330569Sgordon * Software written by Ian F. Darwin and others; 4330569Sgordon * maintained 1995-present by Christos Zoulas and others. 5330569Sgordon * 6330569Sgordon * Redistribution and use in source and binary forms, with or without 7330569Sgordon * modification, are permitted provided that the following conditions 8330569Sgordon * are met: 9330569Sgordon * 1. Redistributions of source code must retain the above copyright 10330569Sgordon * notice immediately at the beginning of the file, without modification, 11330569Sgordon * this list of conditions, and the following disclaimer. 12330569Sgordon * 2. Redistributions in binary form must reproduce the above copyright 13330569Sgordon * notice, this list of conditions and the following disclaimer in the 14330569Sgordon * documentation and/or other materials provided with the distribution. 15330569Sgordon * 16330569Sgordon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17330569Sgordon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18330569Sgordon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19330569Sgordon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20330569Sgordon * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21330569Sgordon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22330569Sgordon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23330569Sgordon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24330569Sgordon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25330569Sgordon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26330569Sgordon * SUCH DAMAGE. 27330569Sgordon */ 28330569Sgordon#include "file.h" 29330569Sgordon 30330569Sgordon#ifndef lint 31330569SgordonFILE_RCSID("@(#)$File: dprintf.c,v 1.1 2015/11/13 15:36:14 christos Exp $") 32330569Sgordon#endif /* lint */ 33330569Sgordon 34330569Sgordon#include <assert.h> 35330569Sgordon#include <unistd.h> 36330569Sgordon#include <stdio.h> 37330569Sgordon#include <stdarg.h> 38330569Sgordon 39330569Sgordonint 40330569Sgordondprintf(int fd, const char *fmt, ...) 41330569Sgordon{ 42330569Sgordon va_list ap; 43330569Sgordon /* Simpler than using vasprintf() here, since we never need more */ 44330569Sgordon char buf[1024]; 45330569Sgordon int len; 46330569Sgordon 47330569Sgordon va_start(ap, fmt); 48330569Sgordon len = vsnprintf(buf, sizeof(buf), fmt, ap); 49330569Sgordon va_end(ap); 50330569Sgordon 51330569Sgordon if ((size_t)len >= sizeof(buf)) 52330569Sgordon return -1; 53330569Sgordon 54330569Sgordon if (write(fd, buf, (size_t)len) != len) 55330569Sgordon return -1; 56330569Sgordon 57330569Sgordon return len; 58330569Sgordon} 59