/*1* Copyright (c) Ian F. Darwin 1986-1995.2* Software written by Ian F. Darwin and others;3* maintained 1995-present by Christos Zoulas and others.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice immediately at the beginning of the file, without modification,10* this list of conditions, and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR19* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/27#include "file.h"2829#ifndef lint30FILE_RCSID("@(#)$File: dprintf.c,v 1.4 2022/09/24 20:30:13 christos Exp $")31#endif /* lint */3233#include <assert.h>34#include <unistd.h>35#include <stdio.h>36#include <stdarg.h>3738int39dprintf(int fd, const char *fmt, ...)40{41va_list ap;42/* Simpler than using vasprintf() here, since we never need more */43char buf[1024];44int len;4546va_start(ap, fmt);47len = vsnprintf(buf, sizeof(buf), fmt, ap);48va_end(ap);4950if ((size_t)len >= sizeof(buf))51return -1;5253if (write(fd, buf, (size_t)len) != len)54return -1;5556return len;57}585960