Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kaem.windows.run
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ M2-Planet \
-f ${srcdest}lib/linux/malloc.c \
-f ${srcdest}lib/windows/ntdll.c \
-f ${srcdest}lib/string/memset.c \
-f ${srcdest}lib/windows/_read.c \
-f ${srcdest}lib/windows/read.c \
-f ${srcdest}lib/mes/fdgetc.c \
-f ${srcdest}lib/stdio/getchar.c \
Expand Down
32 changes: 25 additions & 7 deletions lib/windows/_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,34 @@
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/

/* Stands in for lib/linux/_read.c. On Linux _read is the bare SYS_read and
* read is the same call; here read (lib/windows/read.c) is already the bare
* NtReadFile with nothing between, so this is that. */

#include <windows/ntcall.h>
#include <windows/ntdll.h>
#include <mes/lib.h>
#include <unistd.h>
#include <fcntl.h>

/* Stands in for lib/linux/_read.c: the bare read, with nothing above it.
* configure-lib.sh's lists build read from lib/posix/buffered-read.c, whose
* __buffered_read fills its buffer with this, so this must not call read.
* NtReadFile is NtWriteFile's twin, and an NTSTATUS below zero is how the end
* of the file arrives as much as how a failure does -- both mean nothing more
* was read, which is what a caller of read wants to hear as 0. */

ssize_t
_read (int filedes, void *buffer, size_t size)
{
return read (filedes, buffer, size);
int NtReadFile;
int *iosb;
int handle;
int rc;

NtReadFile = __ntdll_resolve ("NtReadFile");
iosb = __iosb ();
iosb[0] = 0;
iosb[1] = 0;
handle = __handle (filedes);

rc = __ntcall9 (NtReadFile, handle, 0, 0, 0, iosb, buffer, size, 0, 0);
if (rc < 0)
return 0;
return iosb[1];
}
26 changes: 5 additions & 21 deletions lib/windows/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,16 @@
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows/ntcall.h>
#include <windows/ntdll.h>
#include <mes/lib.h>
#include <fcntl.h>

/* Stands in for lib/linux/read.c. NtReadFile is NtWriteFile's twin, and an
* NTSTATUS below zero is how the end of the file arrives as much as how a
* failure does -- both mean nothing more was read, which is what a caller of
* read wants to hear as 0. */
/* Stands in for lib/linux/read.c, for a build that buffers nothing, as
* kaem.windows.run's does: there read is lib/windows/_read.c's bare
* NtReadFile. configure-lib.sh's lists take read from
* lib/posix/buffered-read.c instead, and do not build this file. */

ssize_t
read (int filedes, void *buffer, size_t size)
{
int NtReadFile;
int *iosb;
int handle;
int rc;

NtReadFile = __ntdll_resolve ("NtReadFile");
iosb = __iosb ();
iosb[0] = 0;
iosb[1] = 0;
handle = __handle (filedes);

rc = __ntcall9 (NtReadFile, handle, 0, 0, 0, iosb, buffer, size, 0, 0);
if (rc < 0)
return 0;
return iosb[1];
return _read (filedes, buffer, size);
}