Blame src/internal/ptycon_dprintf_impl.c

235880
#include <ntapi/ntapi.h>
235880
#include <psxabi/sys_sysapi.h>
235880
#include <psxabi/sys_errno.h>
235880
235880
#include <ptycon/ptycon.h>
235880
#include "ptycon_driver_impl.h"
235880
#include "ptycon_nolibc_impl.h"
235880
235880
#include <stdio.h>
235880
#include <stdlib.h>
235880
#include <unistd.h>
235880
235880
int ptyc_dprintf(int fd, const char * fmt, ...)
235880
{
235880
	int	ret;
235880
	int	cnt;
235880
	int	size;
235880
	va_list	ap;
235880
	char *	ch;
235880
	char *	buf;
235880
	char	chbuf[2048];
235880
235880
	va_start(ap,fmt);
235880
235880
	size = sizeof(chbuf);
235880
	buf  = ((cnt = ntapi->vsnprintf(chbuf,size,fmt,ap)) < size)
235880
		? chbuf : calloc(1, cnt + 1);
235880
235880
	va_end(ap);
235880
235880
	if (buf == chbuf) {
235880
		(void)0;
235880
235880
	} else if (buf) {
235880
		va_start(ap,fmt);
235880
		ntapi->vsprintf(buf,fmt,ap);
235880
		va_end(ap);
235880
235880
	} else {
235880
		return -1;
235880
	}
235880
235880
	ret = 0;
235880
	ch  = buf;
235880
235880
	for (; cnt && ret>=0; ) {
235880
		ret = ptyc_write(fd,ch,cnt);
235880
235880
		while ((ret == NT_STATUS_ALERTED) || (ret == NT_STATUS_USER_APC))
235880
			ret = ptyc_write(fd,ch,cnt);
235880
235880
		ch  += ret;
235880
		cnt -= ret;
235880
	}
235880
235880
	ret = (ret < 0) ? -1 : ch - buf;
235880
235880
	if (buf != chbuf)
235880
		free(buf);
235880
235880
	return ret;
235880
}