From d6fb64c51244529388b1f79ba8220ff608e1e4de Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 11 Feb 2010 20:18:50 +1100 Subject: libreplace: added replacements for dprintf() and vdprintf() these are very useful for writing files with formatted writes Pair-Programmed-With: Andrew Bartlett --- lib/replace/libreplace.m4 | 2 +- lib/replace/replace.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/replace/libreplace.m4 b/lib/replace/libreplace.m4 index 1353c1f7d2..7a26deb611 100644 --- a/lib/replace/libreplace.m4 +++ b/lib/replace/libreplace.m4 @@ -108,7 +108,7 @@ AC_CHECK_HEADERS(unix.h) AC_CHECK_FUNCS(seteuid setresuid setegid setresgid chroot bzero strerror) AC_CHECK_FUNCS(vsyslog setlinebuf mktime ftruncate chsize rename) AC_CHECK_FUNCS(waitpid wait4 strlcpy strlcat initgroups memmove strdup) -AC_CHECK_FUNCS(pread pwrite strndup strcasestr strtok_r mkdtemp dup2) +AC_CHECK_FUNCS(pread pwrite strndup strcasestr strtok_r mkdtemp dup2 dprintf vdprintf) AC_CHECK_FUNCS(isatty chown lchown link readlink symlink realpath) AC_HAVE_DECL(setresuid, [#include ]) AC_HAVE_DECL(setresgid, [#include ]) diff --git a/lib/replace/replace.c b/lib/replace/replace.c index df29185564..6f0851da7c 100644 --- a/lib/replace/replace.c +++ b/lib/replace/replace.c @@ -704,3 +704,34 @@ void *rep_memmem(const void *haystack, size_t haystacklen, } #endif +#ifndef HAVE_VDPRINTF +int vdprintf(int fd, const char *format, va_list ap) +{ + char *s = NULL; + int ret; + + vasprintf(&s, format, ap); + if (s == NULL) { + errno = ENOMEM; + return -1; + } + ret = write(fd, s, strlen(s)); + free(s); + return ret; +} +#endif + +#ifndef HAVE_DPRINTF +int dprintf(int fd, const char *format, ...) +{ + int ret; + va_list ap; + + va_start(ap, format); + ret = vdprintf(fd, format, ap); + va_end(ap); + + return ret; +} +#endif + -- cgit