Blame src/internal/toksvc_dprintf_impl.c

70a4a0
/*********************************************************/
70a4a0
/*  toksvc: a framework-native token broker service      */
70a4a0
/*  Copyright (C) 2020  Z. Gilboa                        */
70a4a0
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
70a4a0
/*********************************************************/
70a4a0
70a4a0
#include <psxtypes/psxtypes.h>
70a4a0
#include <ntapi/ntapi.h>
70a4a0
70a4a0
#include <toksvc/toksvc.h>
70a4a0
#include "toksvc_driver_impl.h"
70a4a0
#include "toksvc_nolibc_impl.h"
70a4a0
70a4a0
#include <stdio.h>
70a4a0
#include <stdlib.h>
70a4a0
#include <unistd.h>
70a4a0
70a4a0
int toks_dprintf(int fd, const char * fmt, ...)
70a4a0
{
70a4a0
	int	ret;
70a4a0
	int	cnt;
70a4a0
	int	size;
70a4a0
	va_list	ap;
70a4a0
	char *	ch;
70a4a0
	char *	buf;
70a4a0
	char	chbuf[2048];
70a4a0
70a4a0
	va_start(ap,fmt);
70a4a0
70a4a0
	size = sizeof(chbuf);
70a4a0
	buf  = ((cnt = ntapi->vsnprintf(chbuf,size,fmt,ap)) < size)
70a4a0
		? chbuf : calloc(1, cnt + 1);
70a4a0
70a4a0
	va_end(ap);
70a4a0
70a4a0
	if (buf == chbuf) {
70a4a0
		(void)0;
70a4a0
70a4a0
	} else if (buf) {
70a4a0
		va_start(ap,fmt);
70a4a0
		ntapi->vsprintf(buf,fmt,ap);
70a4a0
		va_end(ap);
70a4a0
70a4a0
	} else {
70a4a0
		return -1;
70a4a0
	}
70a4a0
70a4a0
	ret = 0;
70a4a0
	ch  = buf;
70a4a0
70a4a0
	for (; cnt && ret>=0; ) {
70a4a0
		ret = toks_write(fd,ch,cnt);
70a4a0
6638ed
		while ((ret == -NT_STATUS_ALERTED) || (ret == -NT_STATUS_USER_APC))
70a4a0
			ret = toks_write(fd,ch,cnt);
70a4a0
70a4a0
		ch  += ret;
70a4a0
		cnt -= ret;
70a4a0
	}
70a4a0
70a4a0
	ret = (ret < 0) ? -1 : ch - buf;
70a4a0
70a4a0
	if (buf != chbuf)
70a4a0
		free(buf);
70a4a0
70a4a0
	return ret;
70a4a0
}