diff options
author | midipix <writeonce@midipix.org> | 2024-04-29 02:39:30 +0000 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2024-05-07 20:07:25 +0000 |
commit | 02e59c946d51faf976a85610e44d7b0ccf4a6e59 (patch) | |
tree | 660056e01b242e947e27c470b9989c7b1ae6b2b7 /src/internal/treebnf_dprintf_impl.c | |
parent | e9098c5591f439cdf8a923c9298b8fc7ff28a3ac (diff) | |
download | treebnf-02e59c946d51faf976a85610e44d7b0ccf4a6e59.tar.bz2 treebnf-02e59c946d51faf976a85610e44d7b0ccf4a6e59.tar.xz |
driver: created program skeleton.
Diffstat (limited to 'src/internal/treebnf_dprintf_impl.c')
-rw-r--r-- | src/internal/treebnf_dprintf_impl.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/internal/treebnf_dprintf_impl.c b/src/internal/treebnf_dprintf_impl.c new file mode 100644 index 0000000..8b24a93 --- /dev/null +++ b/src/internal/treebnf_dprintf_impl.c @@ -0,0 +1,65 @@ +/**************************************************************/ +/* treebnf: a tree oriented bnf library */ +/* Copyright (C) 2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.TREEBNF. */ +/**************************************************************/ + +#include <stdio.h> +#include <stdarg.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> + +#include "treebnf_dprintf_impl.h" +#include "treebnf_visibility_impl.h" + +tbnf_hidden int tbnf_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; +} |