blob: 6f8b0ff5fc58c557d2108189a19f067df405c663 [file] [log] [blame] [raw]
#include <windows.h>
#include <nt.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
static int write_tty(const void *buffer, size_t count) {
//printf("nativelibc debug: function: write_tty(%p, %u)\n", buffer, count);
if(!count) return 0;
size_t len = strnlen(buffer, count) + 1;
if(len < count) {
const char *p = buffer;
//if(!len) return write_tty(p + 1, count - 1) + 1;
int r = write_tty(p, len);
r += write_tty(p + len, count - len);
return r;
}
len = count + 1;
wchar_t wbuffer[len];
mbstowcs(wbuffer, buffer, len);
UNICODE_STRING wstrsut = { count * sizeof(wchar_t), len * sizeof(wchar_t), wbuffer };
long int status = NtDisplayString(&wstrsut);
return ntstatus_to_errno(status) ? -1 : count;
//return status < 0 ? -1 : count; // TODO: implemente the errno
}
int write(int fd, const void *buffer, size_t count) {
if(!count) return 0;
if(fd == STDIN_FILENO) {
errno = EBADF;
return -1;
}
if(fd == STDOUT_FILENO) {
int real_fd = _get_stdout_fd();
switch(real_fd) {
case TTY_FD_INVALID:
errno = EBADF;
return -1;
case TTY_FD_DEFAULT:
return write_tty(buffer, count);
default:
fd = real_fd;
}
} else if(fd == STDERR_FILENO) {
int real_fd = _get_stderr_fd();
switch(real_fd) {
case TTY_FD_INVALID:
errno = EBADF;
return -1;
case TTY_FD_DEFAULT:
return write_tty(buffer, count);
default:
fd = real_fd;
}
}
IO_STATUS_BLOCK io_status;
long int status = NtWriteFile((void *)fd, NULL, NULL, NULL, &io_status, buffer, count, NULL, NULL);
//printf("nativelibc debug: write: status = 0x%lx\n", status);
if(ntstatus_to_errno(status)) return -1;
//if(status < 0) return -1; // TODO: implemente the errno
return io_status.Information;
}