Blame src/internal/mdso_dprintf_impl.c

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