diff options
author | midipix <writeonce@midipix.org> | 2018-06-26 14:18:11 +0000 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2018-07-14 22:40:52 -0400 |
commit | 7c1290914960e89e2aba185e098eb36cc0d3ea42 (patch) | |
tree | 43ec114af8775f0f74797c7e585c4352397dd2e4 /src | |
parent | 871a4fd677c080b9254ad476497c92f677de1e7c (diff) | |
download | sofort-7c1290914960e89e2aba185e098eb36cc0d3ea42.tar.bz2 sofort-7c1290914960e89e2aba185e098eb36cc0d3ea42.tar.xz |
internals: added sfrt_dprintf(), a signal-resilient dprintf implementation.
Diffstat (limited to 'src')
-rw-r--r-- | src/internal/sofort_dprintf_impl.c | 56 | ||||
-rw-r--r-- | src/internal/sofort_dprintf_impl.h | 6 |
2 files changed, 62 insertions, 0 deletions
diff --git a/src/internal/sofort_dprintf_impl.c b/src/internal/sofort_dprintf_impl.c new file mode 100644 index 0000000..01f1ee1 --- /dev/null +++ b/src/internal/sofort_dprintf_impl.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdarg.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> + +int sfrt_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/sofort_dprintf_impl.h b/src/internal/sofort_dprintf_impl.h new file mode 100644 index 0000000..bfcc4ca --- /dev/null +++ b/src/internal/sofort_dprintf_impl.h @@ -0,0 +1,6 @@ +#ifndef SOFORT_DPRINTF_IMPL_H +#define SOFORT_DPRINTF_IMPL_H + +int sfrt_dprintf(int fd, const char * fmt, ...); + +#endif |