Blame src/internal/tpax_dprintf_impl.c

5874a9
/**************************************************************/
5874a9
/*  tpax: a topological pax implementation                    */
bef28d
/*  Copyright (C) 2020--2024  SysDeer Technologies, LLC       */
5874a9
/*  Released under GPLv2 and GPLv3; see COPYING.TPAX.         */
5874a9
/**************************************************************/
88751e
88751e
#include <stdio.h>
88751e
#include <stdarg.h>
88751e
#include <stdlib.h>
88751e
#include <unistd.h>
88751e
#include <errno.h>
88751e
538329
#include "tpax_visibility_impl.h"
538329
538329
tpax_hidden int tpax_dprintf(int fd, const char * fmt, ...)
88751e
{
88751e
	int	ret;
88751e
	int	cnt;
88751e
	int	size;
88751e
	va_list	ap;
88751e
	char *	ch;
88751e
	char *	buf;
88751e
	char	chbuf[2048];
88751e
88751e
	va_start(ap,fmt);
88751e
88751e
	size = sizeof(chbuf);
88751e
	buf  = ((cnt = vsnprintf(chbuf,size,fmt,ap)) < size)
88751e
		? chbuf : malloc(cnt + 1);
88751e
88751e
	va_end(ap);
88751e
88751e
	if (buf == chbuf) {
88751e
		(void)0;
88751e
88751e
	} else if (buf) {
88751e
		va_start(ap,fmt);
88751e
		vsprintf(buf,fmt,ap);
88751e
		va_end(ap);
88751e
88751e
	} else {
88751e
		return -1;
88751e
	}
88751e
88751e
	ret = 0;
88751e
	ch  = buf;
88751e
88751e
	for (; cnt && ret>=0; ) {
88751e
		ret = write(fd,ch,cnt);
88751e
88751e
		while ((ret < 0) && (errno == EINTR))
88751e
			ret = write(fd,ch,cnt);
88751e
88751e
		ch  += ret;
88751e
		cnt -= ret;
88751e
	}
88751e
88751e
	ret = (ret < 0) ? -1 : ch - buf;
88751e
88751e
	if (buf != chbuf)
88751e
		free(buf);
88751e
88751e
	return ret;
88751e
}