From 5cc5f58e9585ec3e0455fbc54a227715144f91ae Mon Sep 17 00:00:00 2001 From: midipix Date: Jun 27 2018 11:30:01 +0000 Subject: internals: added slbt_dprintf(), a signal-resilient dprintf implementation. --- diff --git a/project/common.mk b/project/common.mk index f41ee7a..20a8112 100644 --- a/project/common.mk +++ b/project/common.mk @@ -19,6 +19,7 @@ API_SRCS = \ src/skin/slbt_skin_uninstall.c \ INTERNAL_SRCS = \ + src/internal/$(PACKAGE)_dprintf_impl.c \ src/internal/$(PACKAGE)_errinfo_impl.c \ src/internal/$(PACKAGE)_libmeta_impl.c \ src/internal/$(PACKAGE)_objmeta_impl.c \ diff --git a/project/headers.mk b/project/headers.mk index 0a518a8..e238221 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -4,6 +4,7 @@ API_HEADERS = \ INTERNAL_HEADERS = \ $(PROJECT_DIR)/src/internal/argv/argv.h \ + $(PROJECT_DIR)/src/internal/$(PACKAGE)_dprintf_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_driver_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_errinfo_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_install_impl.h \ diff --git a/src/internal/slibtool_dprintf_impl.c b/src/internal/slibtool_dprintf_impl.c new file mode 100644 index 0000000..a6d3482 --- /dev/null +++ b/src/internal/slibtool_dprintf_impl.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include + +int slbt_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/slibtool_dprintf_impl.h b/src/internal/slibtool_dprintf_impl.h new file mode 100644 index 0000000..b223f8a --- /dev/null +++ b/src/internal/slibtool_dprintf_impl.h @@ -0,0 +1,6 @@ +#ifndef SLIBTOOL_DPRINTF_IMPL_H +#define SLIBTOOL_DPRINTF_IMPL_H + +int slbt_dprintf(int fd, const char * fmt, ...); + +#endif