Blob Blame History Raw
/***********************************************************/
/*  ntux: native translation und extension                 */
/*  Copyright (C) 2016--2018  Z. Gilboa                    */
/*  Released under GPLv2 and GPLv3; see COPYING.NTUX.      */
/***********************************************************/

#include <ntapi/ntapi.h>
#include <psxabi/sys_sysapi.h>
#include <psxabi/sys_errno.h>

extern const ntapi_vtbl * ntux_ntapi;

typedef struct ntux_file {
        void *  hany;
} FILE;

int ntux_fputs(const char * str, FILE * file)
{
	ssize_t		ret;
	size_t		size;
	size_t		nbytes;

	size   = ntux_ntapi->tt_string_null_offset_multibyte(str);
	nbytes = size;

	while (nbytes) {
		ret = __sys_write(
			(int)(intptr_t)file,
			str,nbytes);

		if (ret == -EINTR)
			(void)0;

		else if (ret < 0)
			return -1;

		else {
			str    += ret;
			nbytes -= ret;
		}
	}

	/* all done */
	return (int)size;
}

int ntux_fprintf(FILE * file, const char * fmt, ...)
{
	va_list	ap;
	char *	str;
	size_t	buffer[32768/sizeof(size_t)];

	ntux_ntapi->tt_aligned_block_memset(
		buffer,0,sizeof(buffer));

	str = (char *)buffer;

	va_start(ap, fmt);
	ntux_ntapi->vsnprintf(str, sizeof(buffer), fmt, ap);
	va_end(ap);

	str[sizeof(buffer)-1] = 0;

	return ntux_fputs(str,file);
}

int ntux_sprintf(char * str, const char * fmt, ...)
{
	int     ret;
	va_list ap;

	va_start(ap, fmt);
	ret = ntux_ntapi->vsprintf(str, fmt, ap);
	va_end(ap);

	return ret;
}

int ntux_snprintf(char * str, size_t n, const char * fmt, ...)
{
	int     ret;
	va_list ap;

	va_start(ap, fmt);
	ret = ntux_ntapi->vsnprintf(str, n, fmt, ap);
	va_end(ap);

	return ret;
}

int ntux_isatty(int fildes)
{
	nt_runtime_data * rtdata;

	if ((ntux_ntapi->tt_get_runtime_data(&rtdata,0)))
		return 0;

	if (fildes == 0)
		return (rtdata->stdin_type == NT_FILE_TYPE_PTY);

	else if (fildes == 1)
		return (rtdata->stdout_type == NT_FILE_TYPE_PTY);

	else if (fildes == 2)
		return (rtdata->stderr_type == NT_FILE_TYPE_PTY);

	else
		return 0;
}

int ntux_fileno(void * any)
{
	return (int)(intptr_t)any;
}

int ntux_fflush(FILE * file)
{
	return __sys_fsync((int)(intptr_t)file);
}