Blame src/internal/perk_dprintf_impl.c

c353c6
#include <stdio.h>
c353c6
#include <stdarg.h>
c353c6
#include <stdlib.h>
c353c6
#include <unistd.h>
c353c6
#include <errno.h>
c353c6
c353c6
int pe_dprintf(int fd, const char * fmt, ...)
c353c6
{
c353c6
	int	ret;
c353c6
	int	cnt;
c353c6
	int	size;
c353c6
	va_list	ap;
c353c6
	char *	ch;
c353c6
	char *	buf;
c353c6
	char	chbuf[2048];
c353c6
c353c6
	va_start(ap,fmt);
c353c6
c353c6
	size = sizeof(chbuf);
c353c6
	buf  = ((cnt = vsnprintf(chbuf,size,fmt,ap)) < size)
c353c6
		? chbuf : malloc(cnt + 1);
c353c6
c353c6
	va_end(ap);
c353c6
c353c6
	if (buf == chbuf) {
c353c6
		(void)0;
c353c6
c353c6
	} else if (buf) {
c353c6
		va_start(ap,fmt);
c353c6
		vsprintf(buf,fmt,ap);
c353c6
		va_end(ap);
c353c6
c353c6
	} else {
c353c6
		return -1;
c353c6
	}
c353c6
c353c6
	ret = 0;
c353c6
	ch  = buf;
c353c6
c353c6
	for (; cnt && ret>=0; ) {
c353c6
		ret = write(fd,ch,cnt);
c353c6
c353c6
		while ((ret < 0) && (errno == EINTR))
c353c6
			ret = write(fd,ch,cnt);
c353c6
c353c6
		ch  += ret;
c353c6
		cnt -= ret;
c353c6
	}
c353c6
c353c6
	ret = (ret < 0) ? -1 : ch - buf;
c353c6
c353c6
	if (buf != chbuf)
c353c6
		free(buf);
c353c6
c353c6
	return ret;
c353c6
}