Blame src/internal/ptycon_ntaio_impl.c

0efa8c
/*********************************************************/
0efa8c
/*  ptycon: a pty-console bridge                         */
f25e99
/*  Copyright (C) 2016--2017  SysDeer Technologies, LLC  */
0efa8c
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.  */
0efa8c
/*********************************************************/
0efa8c
0efa8c
#include <ntapi/ntapi.h>
af27df
#include <stdio.h>
0efa8c
0efa8c
extern const ntapi_vtbl * ptyc_ntapi;
0efa8c
0efa8c
int ptyc_sprintf(char * str, const char * fmt, ...)
0efa8c
{
0efa8c
	int     ret;
0efa8c
	va_list ap;
0efa8c
0efa8c
	va_start(ap, fmt);
0efa8c
	ret = ptyc_ntapi->vsprintf(str, fmt, ap);
0efa8c
	va_end(ap);
0efa8c
0efa8c
	return ret;
0efa8c
}
0efa8c
0efa8c
int ptyc_snprintf(char * str, size_t n, const char * fmt, ...)
0efa8c
{
0efa8c
	int     ret;
0efa8c
	va_list ap;
0efa8c
0efa8c
	va_start(ap, fmt);
0efa8c
	ret = ptyc_ntapi->vsnprintf(str, n, fmt, ap);
0efa8c
	va_end(ap);
0efa8c
0efa8c
	return ret;
0efa8c
}
0efa8c
0efa8c
int ptyc_isatty(int fildes)
0efa8c
{
0efa8c
	nt_runtime_data * rtdata;
0efa8c
0efa8c
	if ((ptyc_ntapi->tt_get_runtime_data(&rtdata,0)))
0efa8c
		return 0;
0efa8c
0efa8c
	if (fildes == 0)
0efa8c
		return (rtdata->stdin_type == NT_FILE_TYPE_PTY);
0efa8c
0efa8c
	else if (fildes == 1)
0efa8c
		return (rtdata->stdout_type == NT_FILE_TYPE_PTY);
0efa8c
0efa8c
	else if (fildes == 2)
0efa8c
		return (rtdata->stderr_type == NT_FILE_TYPE_PTY);
0efa8c
0efa8c
	else
0efa8c
		return 0;
0efa8c
}
0efa8c
af27df
int ptyc_write(int fd, const void * buf, size_t size)
af27df
{
af27df
	int32_t			status;
af27df
	nt_runtime_data *	rtdata;
af27df
	ntapi_zw_write_file *	iofn;
af27df
	void *			hio;
af27df
	void *			hevent;
af27df
	int			fdtype;
af27df
	nt_iosb			iosb;
af27df
af27df
	/* size */
af27df
	if (size >= 0x80000000)
af27df
		return NT_STATUS_INVALID_PARAMETER;
af27df
af27df
	/* rtdata */
af27df
	if (ptyc_ntapi->tt_get_runtime_data(&rtdata,0))
af27df
		return NT_STATUS_REINITIALIZATION_NEEDED;
af27df
af27df
	/* hio, io type */
af27df
	if (fd == STDIN_FILENO) {
af27df
		hio    = rtdata->hstdin;
af27df
		fdtype = rtdata->stdin_type;
af27df
af27df
	} else if (fd == STDOUT_FILENO) {
af27df
		hio    = rtdata->hstdout;
af27df
		fdtype = rtdata->stdout_type;
af27df
af27df
	} else if (fd == STDERR_FILENO) {
af27df
		hio    = rtdata->hstderr;
af27df
		fdtype = rtdata->stderr_type;
af27df
	} else {
af27df
		return NT_STATUS_INVALID_PARAMETER;
af27df
	}
af27df
af27df
	if (!hio)
af27df
		return NT_STATUS_INVALID_HANDLE;
af27df
af27df
	/* iofn */
af27df
	iofn = (fdtype ==  NT_FILE_TYPE_PTY)
af27df
		? (ntapi_zw_write_file *)ptyc_ntapi->pty_write
af27df
		: ptyc_ntapi->zw_write_file;
af27df
af27df
	/* hevent */
af27df
	if ((status = ptyc_ntapi->tt_create_private_event(
af27df
			&hevent,
af27df
			NT_NOTIFICATION_EVENT,
af27df
			NT_EVENT_NOT_SIGNALED)))
af27df
		return status;
af27df
af27df
	/* iowrite */
af27df
	status = iofn(
af27df
		hio,hevent,
af27df
		0,0,&iosb,
af27df
		(void *)buf,size,
af27df
		0,0);
af27df
af27df
	/* wait */
582291
	switch (status) {
582291
		case NT_STATUS_PENDING:
582291
			status = ptyc_ntapi->zw_wait_for_single_object(
582291
				hevent,0,0);
582291
			break;
582291
582291
		default:
582291
			iosb.status = status;
582291
			break;
582291
	}
af27df
af27df
	/* hevent */
af27df
	ptyc_ntapi->zw_close(hevent);
af27df
af27df
	/* ret */
582291
	return iosb.status ? iosb.status : iosb.info;
af27df
}