diff options
author | midipix <writeonce@midipix.org> | 2018-06-26 14:18:11 +0000 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2018-06-27 07:30:01 -0400 |
commit | 5cc5f58e9585ec3e0455fbc54a227715144f91ae (patch) | |
tree | 23ec073fd95971cd50d95d7466fbaa37cb1088c2 /src/internal/slibtool_dprintf_impl.c | |
parent | 93f9e46057e3ea47fccec38ae9d5eeb46a54fef0 (diff) | |
download | slibtool-5cc5f58e9585ec3e0455fbc54a227715144f91ae.tar.bz2 slibtool-5cc5f58e9585ec3e0455fbc54a227715144f91ae.tar.xz |
internals: added slbt_dprintf(), a signal-resilient dprintf implementation.
Diffstat (limited to 'src/internal/slibtool_dprintf_impl.c')
-rw-r--r-- | src/internal/slibtool_dprintf_impl.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/internal/slibtool_dprintf_impl.c b/src/internal/slibtool_dprintf_impl.c new file mode 100644 index 0000000..a6d3482 --- /dev/null +++ b/src/internal/slibtool_dprintf_impl.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdarg.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> + +int slbt_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; +} |