diff options
author | midipix <writeonce@midipix.org> | 2018-07-14 21:05:10 +0000 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2018-07-14 21:22:43 -0400 |
commit | 81d6fb5d5e7b89ffbed5bfb6a81092859ffc1ec0 (patch) | |
tree | 34a4cf7212bad78134691ed2ff0faac0a512c3f4 | |
parent | a1a795b5bc036d4d8c64df1e0af7d633db70e415 (diff) | |
download | ntux-81d6fb5d5e7b89ffbed5bfb6a81092859ffc1ec0.tar.bz2 ntux-81d6fb5d5e7b89ffbed5bfb6a81092859ffc1ec0.tar.xz |
internals: added ntux_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/ntux_dprintf_impl.c | 63 | ||||
-rw-r--r-- | src/internal/ntux_dprintf_impl.h | 6 |
4 files changed, 71 insertions, 0 deletions
diff --git a/project/common.mk b/project/common.mk index c1f3080..f88c5fd 100644 --- a/project/common.mk +++ b/project/common.mk @@ -12,6 +12,7 @@ DRIVER_SRCS = \ INTERNAL_SRCS = \ src/internal/nolibc/ntux_compiler.c \ + src/internal/ntux_dprintf_impl.c \ src/internal/ntux_errinfo_impl.c \ src/internal/ntux_memfn_impl.c \ src/internal/ntux_nolibc_impl.c \ diff --git a/project/headers.mk b/project/headers.mk index 892cc91..3a75ef5 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -4,6 +4,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)_errinfo_impl.h \ $(SOURCE_DIR)/src/internal/$(PACKAGE)_init_impl.h \ diff --git a/src/internal/ntux_dprintf_impl.c b/src/internal/ntux_dprintf_impl.c new file mode 100644 index 0000000..1f87eb6 --- /dev/null +++ b/src/internal/ntux_dprintf_impl.c @@ -0,0 +1,63 @@ +#include <ntapi/ntapi.h> +#include <psxabi/sys_sysapi.h> +#include <psxabi/sys_errno.h> + +#include <ntux/ntux.h> +#include "ntux_driver_impl.h" +#include "ntux_nolibc_impl.h" +#include "ntux_errinfo_impl.h" + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +int ntux_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 = __sys_write(fd,ch,cnt); + + while ((ret < 0) && (ret == -EINTR)) + ret = __sys_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/ntux_dprintf_impl.h b/src/internal/ntux_dprintf_impl.h new file mode 100644 index 0000000..4296bd0 --- /dev/null +++ b/src/internal/ntux_dprintf_impl.h @@ -0,0 +1,6 @@ +#ifndef NTUX_DPRINTF_IMPL_H +#define NTUX_DPRINTF_IMPL_H + +int ntux_dprintf(int fd, const char * fmt, ...); + +#endif |