diff options
author | midipix <writeonce@midipix.org> | 2018-07-15 08:20:34 -0400 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2018-07-15 11:01:59 -0400 |
commit | 2358806aa095d5c79a17ab9da01243506fca8016 (patch) | |
tree | 76f31ea430b9046f2dd03c16a1150c98965efb55 /src | |
parent | af27dfdd0ae13b638a472ce834e5fccd718a84ca (diff) | |
download | ptycon-2358806aa095d5c79a17ab9da01243506fca8016.tar.bz2 ptycon-2358806aa095d5c79a17ab9da01243506fca8016.tar.xz |
internals: added ptyc_dprintf(), a signal-resilient dprintf implementation.
Diffstat (limited to 'src')
-rw-r--r-- | src/internal/ptycon_dprintf_impl.c | 62 | ||||
-rw-r--r-- | src/internal/ptycon_dprintf_impl.h | 6 |
2 files changed, 68 insertions, 0 deletions
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 <ntapi/ntapi.h> +#include <psxabi/sys_sysapi.h> +#include <psxabi/sys_errno.h> + +#include <ptycon/ptycon.h> +#include "ptycon_driver_impl.h" +#include "ptycon_nolibc_impl.h" + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +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 |