summaryrefslogtreecommitdiffhomepage
path: root/src/internal/ntux_ntaio_impl.c
blob: a1a4d8e94d05784ad5f7579025f21e74e9ae7cb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/***********************************************************/
/*  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);
}