summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2018-06-26 14:18:11 +0000
committermidipix <writeonce@midipix.org>2018-07-14 20:29:21 -0400
commit419b74a393e084299c240b6f76b34ab362548dfe (patch)
tree52c812da1471815b46e94d4c909c9739440f99f8
parent6b4bf589f338325a0b6dc8f07029982fbc23f5e9 (diff)
downloadmdso-419b74a393e084299c240b6f76b34ab362548dfe.tar.bz2
mdso-419b74a393e084299c240b6f76b34ab362548dfe.tar.xz
internals: added mdso_dprintf(), a signal-resilient dprintf implementation.
-rw-r--r--project/common.mk1
-rw-r--r--project/headers.mk1
-rw-r--r--src/internal/mdso_dprintf_impl.c56
-rw-r--r--src/internal/mdso_dprintf_impl.h6
4 files changed, 64 insertions, 0 deletions
diff --git a/project/common.mk b/project/common.mk
index 8f693ff..f5cfa2d 100644
--- a/project/common.mk
+++ b/project/common.mk
@@ -21,6 +21,7 @@ API_SRCS = \
src/util/mdso_create_implib_sources.c \
INTERNAL_SRCS = \
+ src/internal/$(PACKAGE)_dprintf_impl.c \
src/internal/$(PACKAGE)_errinfo_impl.c \
APP_SRCS = \
diff --git a/project/headers.mk b/project/headers.mk
index e44ed18..a238cfe 100644
--- a/project/headers.mk
+++ b/project/headers.mk
@@ -11,6 +11,7 @@ INTERNAL_HEADERS = \
$(PROJECT_DIR)/src/internal/argv/argv.h \
$(PROJECT_DIR)/src/internal/perk/perk_consts.h \
$(PROJECT_DIR)/src/internal/perk/perk_structs.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)_object_impl.h \
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 <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+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