From 2358806aa095d5c79a17ab9da01243506fca8016 Mon Sep 17 00:00:00 2001 From: midipix Date: Sun, 15 Jul 2018 08:20:34 -0400 Subject: internals: added ptyc_dprintf(), a signal-resilient dprintf implementation. --- project/common.mk | 1 + project/headers.mk | 1 + src/internal/ptycon_dprintf_impl.c | 62 ++++++++++++++++++++++++++++++++++++++ src/internal/ptycon_dprintf_impl.h | 6 ++++ 4 files changed, 70 insertions(+) create mode 100644 src/internal/ptycon_dprintf_impl.c create mode 100644 src/internal/ptycon_dprintf_impl.h diff --git a/project/common.mk b/project/common.mk index f3f009d..18ab81b 100644 --- a/project/common.mk +++ b/project/common.mk @@ -20,6 +20,7 @@ COMMON_SRCS = \ src/driver/ptyc_driver_ctx.c \ src/internal/gdi/gdi.c \ src/internal/nolibc/ptyc_compiler.c \ + src/internal/ptycon_dprintf_impl.c \ src/internal/ptycon_memfn_impl.c \ src/internal/ptycon_nolibc_impl.c \ src/internal/ptycon_ntaio_impl.c \ diff --git a/project/headers.mk b/project/headers.mk index 9da8130..2525ef7 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -9,6 +9,7 @@ INTERNAL_HEADERS = \ $(PROJECT_DIR)/src/internal/gdi/gdi_window.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_bridge_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_daemon_impl.h \ + $(PROJECT_DIR)/src/internal/$(PACKAGE)_dprintf_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_driver_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_init_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_ioctl_impl.h \ diff --git a/src/internal/ptycon_dprintf_impl.c b/src/internal/ptycon_dprintf_impl.c new file mode 100644 index 0000000..8589854 --- /dev/null +++ b/src/internal/ptycon_dprintf_impl.c @@ -0,0 +1,62 @@ +#include +#include +#include + +#include +#include "ptycon_driver_impl.h" +#include "ptycon_nolibc_impl.h" + +#include +#include +#include + +int ptyc_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 = ntapi->vsnprintf(chbuf,size,fmt,ap)) < size) + ? chbuf : calloc(1, cnt + 1); + + va_end(ap); + + if (buf == chbuf) { + (void)0; + + } else if (buf) { + va_start(ap,fmt); + ntapi->vsprintf(buf,fmt,ap); + va_end(ap); + + } else { + return -1; + } + + ret = 0; + ch = buf; + + for (; cnt && ret>=0; ) { + ret = ptyc_write(fd,ch,cnt); + + while ((ret == NT_STATUS_ALERTED) || (ret == NT_STATUS_USER_APC)) + ret = ptyc_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/ptycon_dprintf_impl.h b/src/internal/ptycon_dprintf_impl.h new file mode 100644 index 0000000..495b787 --- /dev/null +++ b/src/internal/ptycon_dprintf_impl.h @@ -0,0 +1,6 @@ +#ifndef PTYCON_DPRINTF_IMPL_H +#define PTYCON_DPRINTF_IMPL_H + +int ptyc_dprintf(int fd, const char * fmt, ...); + +#endif -- cgit v1.2.3