Blame src/internal/ntux_ntaio_impl.c

26e14f
/***********************************************************/
26e14f
/*  ntux: native translation und extension                 */
26e14f
/*  Copyright (C) 2016  Z. Gilboa                          */
26e14f
/*  Released under GPLv2 and GPLv3; see COPYING.NTUX.      */
26e14f
/***********************************************************/
26e14f
26e14f
#include <ntapi/ntapi.h>
26e14f
#include <psxabi/sys_sysapi.h>
26e14f
#include <psxabi/sys_errno.h>
26e14f
26e14f
extern const ntapi_vtbl * ntux_ntapi;
26e14f
26e14f
typedef struct ntux_file {
26e14f
        void *  hany;
26e14f
} FILE;
26e14f
26e14f
int ntux_fputs(const char * str, FILE * file)
26e14f
{
26e14f
	ssize_t		ret;
26e14f
	size_t		size;
26e14f
	size_t		nbytes;
26e14f
26e14f
	size   = ntux_ntapi->tt_string_null_offset_multibyte(str);
26e14f
	nbytes = size;
26e14f
26e14f
	while (nbytes) {
26e14f
		ret = __sys_write(
26e14f
			(int)(intptr_t)file,
26e14f
			str,nbytes);
26e14f
26e14f
		if (ret == -EINTR)
26e14f
			(void)0;
26e14f
26e14f
		else if (ret < 0)
26e14f
			return -1;
26e14f
26e14f
		else {
26e14f
			str    += ret;
26e14f
			nbytes -= ret;
26e14f
		}
26e14f
	}
26e14f
26e14f
	/* all done */
26e14f
	return (int)size;
26e14f
}
26e14f
26e14f
int ntux_fprintf(FILE * file, const char * fmt, ...)
26e14f
{
26e14f
	va_list	ap;
26e14f
	char *	str;
26e14f
	size_t	buffer[32768/sizeof(size_t)];
26e14f
26e14f
	ntux_ntapi->tt_aligned_block_memset(
26e14f
		buffer,0,sizeof(buffer));
26e14f
26e14f
	str = (char *)buffer;
26e14f
26e14f
	va_start(ap, fmt);
26e14f
	ntux_ntapi->vsnprintf(str, sizeof(buffer), fmt, ap);
26e14f
	va_end(ap);
26e14f
26e14f
	str[sizeof(buffer)-1] = 0;
26e14f
26e14f
	return ntux_fputs(str,file);
26e14f
}
26e14f
26e14f
int ntux_sprintf(char * str, const char * fmt, ...)
26e14f
{
26e14f
	int     ret;
26e14f
	va_list ap;
26e14f
26e14f
	va_start(ap, fmt);
26e14f
	ret = ntux_ntapi->vsprintf(str, fmt, ap);
26e14f
	va_end(ap);
26e14f
26e14f
	return ret;
26e14f
}
26e14f
26e14f
int ntux_snprintf(char * str, size_t n, const char * fmt, ...)
26e14f
{
26e14f
	int     ret;
26e14f
	va_list ap;
26e14f
26e14f
	va_start(ap, fmt);
26e14f
	ret = ntux_ntapi->vsnprintf(str, n, fmt, ap);
26e14f
	va_end(ap);
26e14f
26e14f
	return ret;
26e14f
}
26e14f
26e14f
int ntux_isatty(int fildes)
26e14f
{
26e14f
	nt_runtime_data * rtdata;
26e14f
26e14f
	if ((ntux_ntapi->tt_get_runtime_data(&rtdata,0)))
26e14f
		return 0;
26e14f
26e14f
	if (fildes == 0)
26e14f
		return (rtdata->stdin_type == NT_FILE_TYPE_PTY);
26e14f
26e14f
	else if (fildes == 1)
26e14f
		return (rtdata->stdout_type == NT_FILE_TYPE_PTY);
26e14f
26e14f
	else if (fildes == 2)
26e14f
		return (rtdata->stderr_type == NT_FILE_TYPE_PTY);
26e14f
26e14f
	else
26e14f
		return 0;
26e14f
}
26e14f
26e14f
int ntux_fileno(void * any)
26e14f
{
26e14f
	return (int)(intptr_t)any;
26e14f
}