summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2018-06-26 14:18:11 +0000
committermidipix <writeonce@midipix.org>2018-07-14 22:11:25 -0400
commitc353c661a24622db379f19fb401861dea8740f4b (patch)
tree334a0b3de430a0884d8b955d60d725d6a2cc0742
parentae96cc9d975d7f65c3abc73db3c4f22984faa00c (diff)
downloadperk-c353c661a24622db379f19fb401861dea8740f4b.tar.bz2
perk-c353c661a24622db379f19fb401861dea8740f4b.tar.xz
internals: added pe_dprintf(), a signal-resilient dprintf implementation.
-rw-r--r--project/common.mk1
-rw-r--r--project/headers.mk1
-rw-r--r--src/internal/perk_dprintf_impl.c56
-rw-r--r--src/internal/perk_dprintf_impl.h6
4 files changed, 64 insertions, 0 deletions
diff --git a/project/common.mk b/project/common.mk
index 1b513e8..09d2d34 100644
--- a/project/common.mk
+++ b/project/common.mk
@@ -26,6 +26,7 @@ API_SRCS = \
src/skin/pe_skin_default.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 ce4c411..f82d46c 100644
--- a/project/headers.mk
+++ b/project/headers.mk
@@ -8,6 +8,7 @@ API_HEADERS = \
INTERNAL_HEADERS = \
$(SOURCE_DIR)/src/internal/argv/argv.h \
+ $(SOURCE_DIR)/src/internal/$(PACKAGE)_dprintf_impl.h \
$(SOURCE_DIR)/src/internal/$(PACKAGE)_driver_impl.h \
$(SOURCE_DIR)/src/internal/$(PACKAGE)_endian_impl.h \
$(SOURCE_DIR)/src/internal/$(PACKAGE)_errinfo_impl.h \
diff --git a/src/internal/perk_dprintf_impl.c b/src/internal/perk_dprintf_impl.c
new file mode 100644
index 0000000..30d3d11
--- /dev/null
+++ b/src/internal/perk_dprintf_impl.c
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+int pe_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/perk_dprintf_impl.h b/src/internal/perk_dprintf_impl.h
new file mode 100644
index 0000000..61e400d
--- /dev/null
+++ b/src/internal/perk_dprintf_impl.h
@@ -0,0 +1,6 @@
+#ifndef PERK_DPRINTF_IMPL_H
+#define PERK_DPRINTF_IMPL_H
+
+int pe_dprintf(int fd, const char * fmt, ...);
+
+#endif