Blame src/internal/treebnf_dprintf_impl.c
|
|
02e59c |
/**************************************************************/
|
|
|
02e59c |
/* treebnf: a tree oriented bnf library */
|
|
|
02e59c |
/* Copyright (C) 2024 SysDeer Technologies, LLC */
|
|
|
02e59c |
/* Released under GPLv2 and GPLv3; see COPYING.TREEBNF. */
|
|
|
02e59c |
/**************************************************************/
|
|
|
02e59c |
|
|
|
02e59c |
#include <stdio.h>
|
|
|
02e59c |
#include <stdarg.h>
|
|
|
02e59c |
#include <stdlib.h>
|
|
|
02e59c |
#include <unistd.h>
|
|
|
02e59c |
#include <errno.h>
|
|
|
02e59c |
|
|
|
02e59c |
#include "treebnf_dprintf_impl.h"
|
|
|
02e59c |
#include "treebnf_visibility_impl.h"
|
|
|
02e59c |
|
|
|
02e59c |
tbnf_hidden int tbnf_dprintf(int fd, const char * fmt, ...)
|
|
|
02e59c |
{
|
|
|
02e59c |
int ret;
|
|
|
02e59c |
int cnt;
|
|
|
02e59c |
int size;
|
|
|
02e59c |
va_list ap;
|
|
|
02e59c |
char * ch;
|
|
|
02e59c |
char * buf;
|
|
|
02e59c |
char chbuf[2048];
|
|
|
02e59c |
|
|
|
02e59c |
va_start(ap,fmt);
|
|
|
02e59c |
|
|
|
02e59c |
size = sizeof(chbuf);
|
|
|
02e59c |
buf = ((cnt = vsnprintf(chbuf,size,fmt,ap)) < size)
|
|
|
02e59c |
? chbuf : malloc(cnt + 1);
|
|
|
02e59c |
|
|
|
02e59c |
va_end(ap);
|
|
|
02e59c |
|
|
|
02e59c |
if (buf == chbuf) {
|
|
|
02e59c |
(void)0;
|
|
|
02e59c |
|
|
|
02e59c |
} else if (buf) {
|
|
|
02e59c |
va_start(ap,fmt);
|
|
|
02e59c |
vsprintf(buf,fmt,ap);
|
|
|
02e59c |
va_end(ap);
|
|
|
02e59c |
|
|
|
02e59c |
} else {
|
|
|
02e59c |
return -1;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
ret = 0;
|
|
|
02e59c |
ch = buf;
|
|
|
02e59c |
|
|
|
02e59c |
for (; cnt && ret>=0; ) {
|
|
|
02e59c |
ret = write(fd,ch,cnt);
|
|
|
02e59c |
|
|
|
02e59c |
while ((ret < 0) && (errno == EINTR))
|
|
|
02e59c |
ret = write(fd,ch,cnt);
|
|
|
02e59c |
|
|
|
02e59c |
ch += ret;
|
|
|
02e59c |
cnt -= ret;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
ret = (ret < 0) ? -1 : ch - buf;
|
|
|
02e59c |
|
|
|
02e59c |
if (buf != chbuf)
|
|
|
02e59c |
free(buf);
|
|
|
02e59c |
|
|
|
02e59c |
return ret;
|
|
|
02e59c |
}
|