diff options
author | midipix <writeonce@midipix.org> | 2018-07-14 12:39:38 -0400 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2018-07-14 20:07:40 -0400 |
commit | f72a4025ada2230fea795b9e43c58490fb0fc6b3 (patch) | |
tree | c88e4c892a4caa45f8661744a5b21f928e58b6af | |
parent | 9c9e94539f1d98036593e53a8f718126620b3a64 (diff) | |
download | apimagic-f72a4025ada2230fea795b9e43c58490fb0fc6b3.tar.bz2 apimagic-f72a4025ada2230fea795b9e43c58490fb0fc6b3.tar.xz |
internals: added amgc_dprintf(), a signal-resilient dprintf implementation.
-rw-r--r-- | project/common.mk | 1 | ||||
-rw-r--r-- | project/headers.mk | 1 | ||||
-rw-r--r-- | src/internal/apimagic_dprintf_impl.c | 56 | ||||
-rw-r--r-- | src/internal/apimagic_dprintf_impl.h | 6 |
4 files changed, 64 insertions, 0 deletions
diff --git a/project/common.mk b/project/common.mk index 45c3178..c6a7dd8 100644 --- a/project/common.mk +++ b/project/common.mk @@ -17,6 +17,7 @@ API_SRCS = \ src/skin/amgc_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 1f50364..0c357bc 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 \ diff --git a/src/internal/apimagic_dprintf_impl.c b/src/internal/apimagic_dprintf_impl.c new file mode 100644 index 0000000..40146d2 --- /dev/null +++ b/src/internal/apimagic_dprintf_impl.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdarg.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> + +int amgc_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/apimagic_dprintf_impl.h b/src/internal/apimagic_dprintf_impl.h new file mode 100644 index 0000000..becc889 --- /dev/null +++ b/src/internal/apimagic_dprintf_impl.h @@ -0,0 +1,6 @@ +#ifndef APIMAGIC_DPRINTF_IMPL_H +#define APIMAGIC_DPRINTF_IMPL_H + +int amgc_dprintf(int fd, const char * fmt, ...); + +#endif |