blob: d8ce09b58952ad28c1d22d9f4e67b81ae54050e0 [file] [log] [blame] [raw]
/* A part of the Native C Library for Windows NT
Copyright 2007-2015 PC GO Ld.
Copyright 2015-2018 Rivoreo
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include <windows.h>
#include <nt.h>
#include <errno.h>
#include <unistd.h>
#include <ntstatus.h>
#include <wchar.h>
//#include <stdio.h>
int read(int fd, void *buffer, size_t count) {
//printf("function: read(%d, %p, %u)\n", fd, buffer, count);
if(!buffer) {
errno = EFAULT;
return -1;
}
if(!count) return 0;
#if 0
if(fd == STDOUT_FILENO || fd == STDERR_FILENO) {
errno = EBADF;
return -1;
}
#else
if(IS_STD_PSEUDO_FD(fd)) {
int real_fd = _get_std_fd(fd);
switch(real_fd) {
case TTY_FD_INVALID:
errno = EBADF;
return -1;
case TTY_FD_DEFAULT:
if(fd == STDIN_FILENO) return read_keyboard(buffer, count);
errno = EBADF; // read(2) on write-only fd (1 or 2)
return -1;
}
fd = real_fd;
}
#endif
int is_file = 0;
size_t oti_size = sizeof(OBJECT_TYPE_INFORMATION) + 64;
OBJECT_TYPE_INFORMATION *oti = malloc(oti_size);
if(!oti) return -1;
long int status = NtQueryObject((void *)fd, ObjectTypeInformation, oti, oti_size, NULL);
if(status < 0) {
free(oti);
__set_errno_from_ntstatus(status);
return -1;
}
if(wcscmp(oti->TypeName.Buffer, L"Directory") == 0) {
free(oti);
errno = EISDIR; // Directory object in OM
return -1;
}
if(wcscmp(oti->TypeName.Buffer, L"File") == 0) is_file = 1;
free(oti);
IO_STATUS_BLOCK io_status;
status = NtReadFile((void *)fd, NULL, NULL, NULL, &io_status, buffer, count, NULL, NULL);
//printf("nativelibc debug: read: status = 0x%lx, is_file = %d\n", status, is_file);
if(status == STATUS_END_OF_FILE) return 0;
if(status == STATUS_INVALID_PARAMETER && is_file && count % 2 == 0) {
if((unsigned long int)buffer % 4) errno = EFAULT;
else errno = EISDIR; // Directory file in FS
return -1;
}
if(status < 0) {
__set_errno_from_ntstatus(status);
return -1;
}
return io_status.Information;
}