From 419b74a393e084299c240b6f76b34ab362548dfe Mon Sep 17 00:00:00 2001 From: midipix Date: Tue, 26 Jun 2018 14:18:11 +0000 Subject: internals: added mdso_dprintf(), a signal-resilient dprintf implementation. --- src/internal/mdso_dprintf_impl.c | 56 ++++++++++++++++++++++++++++++++++++++++ src/internal/mdso_dprintf_impl.h | 6 +++++ 2 files changed, 62 insertions(+) create mode 100644 src/internal/mdso_dprintf_impl.c create mode 100644 src/internal/mdso_dprintf_impl.h (limited to 'src/internal') diff --git a/src/internal/mdso_dprintf_impl.c b/src/internal/mdso_dprintf_impl.c new file mode 100644 index 0000000..c160828 --- /dev/null +++ b/src/internal/mdso_dprintf_impl.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include + +int mdso_dprintf(int fd, const char * fmt, ...) +{ + int ret; + int cnt; + int size; + va_list ap; + char * ch; + char * buf; + char chbuf[2048]; + + va_start(ap,fmt); + + size = sizeof(chbuf); + buf = ((cnt = vsnprintf(chbuf,size,fmt,ap)) < size) + ? chbuf : malloc(cnt + 1); + + va_end(ap); + + if (buf == chbuf) { + (void)0; + + } else if (buf) { + va_start(ap,fmt); + vsprintf(buf,fmt,ap); + va_end(ap); + + } else { + return -1; + } + + ret = 0; + ch = buf; + + for (; cnt && ret>=0; ) { + ret = write(fd,ch,cnt); + + while ((ret < 0) && (errno == EINTR)) + ret = write(fd,ch,cnt); + + ch += ret; + cnt -= ret; + } + + ret = (ret < 0) ? -1 : ch - buf; + + if (buf != chbuf) + free(buf); + + return ret; +} diff --git a/src/internal/mdso_dprintf_impl.h b/src/internal/mdso_dprintf_impl.h new file mode 100644 index 0000000..0b3b079 --- /dev/null +++ b/src/internal/mdso_dprintf_impl.h @@ -0,0 +1,6 @@ +#ifndef MDSO_DPRINTF_IMPL_H +#define MDSO_DPRINTF_IMPL_H + +int mdso_dprintf(int fd, const char * fmt, ...); + +#endif -- cgit v1.2.3