Blame src/internal/sofort_dprintf_impl.c

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