Simplify and fix reading and writing so they seem to work.

This commit is contained in:
David Given 2022-08-18 21:21:33 +02:00
parent cc356b5c75
commit 5329c98b81
6 changed files with 113 additions and 120 deletions

View file

@ -5,6 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
@ -16,9 +17,7 @@ ssize_t read(int fd, void* buffer, size_t count)
ssize_t r, tot; ssize_t r, tot;
size_t left; size_t left;
int eof = 0; int eof = 0;
bool text = _sys_getmode(fd) == O_TEXT;
if (!count || _sys_getmode(fd) == O_BINARY)
return _sys_rawread(fd, buffer, count);
if (_sys_iseof(fd)) if (_sys_iseof(fd))
return 0; return 0;
@ -47,6 +46,8 @@ ssize_t read(int fd, void* buffer, size_t count)
if (r <= 0) if (r <= 0)
return tot ? tot : r; return tot ? tot : r;
if (text)
{
q = memchr(p, 0x1a, (size_t)r); q = memchr(p, 0x1a, (size_t)r);
if (q) if (q)
{ {
@ -75,6 +76,7 @@ ssize_t read(int fd, void* buffer, size_t count)
} }
--left; --left;
} }
}
} while (tot < count && !eof && _sys_isreadyr(fd)); } while (tot < count && !eof && _sys_isreadyr(fd));
return tot; return tot;

View file

@ -20,13 +20,25 @@ ssize_t write(int fd, void* buffer, size_t count)
if (!count) if (!count)
return 0; return 0;
if (_sys_getmode(fd) == O_BINARY)
return _sys_rawwrite(fd, buffer, count);
/* If the file descriptor is an O_TEXT fd, translate LFs to CRLFs. */
p = buffer; p = buffer;
left = count; left = count;
tot = 0; tot = 0;
if (_sys_getmode(fd) == O_BINARY)
{
while (left)
{
r = _sys_rawwrite(fd, p, left);
if (r <= 0)
return tot ? tot : r;
tot += r;
p += r;
left -= r;
}
}
else
{
/* If the file descriptor is an O_TEXT fd, translate LFs to CRLFs. */
while (left) while (left)
{ {
q = memchr(p, '\n', left); q = memchr(p, '\n', left);
@ -57,5 +69,6 @@ ssize_t write(int fd, void* buffer, size_t count)
++p; ++p;
--left; --left;
} }
}
return tot; return tot;
} }

View file

@ -166,7 +166,7 @@ empty_environment:
! argc, argv, and envp are now at the stack top. Now go. ! argc, argv, and envp are now at the stack top. Now go.
call __m_a_i_n call __m_a_i_n
add sp, 6 add sp, 3*4
push ax push ax
call _exit call _exit

View file

@ -26,19 +26,12 @@
.define __sys_rawread .define __sys_rawread
__sys_rawread: __sys_rawread:
enter 4, 0 enter 0, 0
amount_transferred = -1*4
file_handle = 2*4 file_handle = 2*4
write_buffer = 3*4 write_buffer = 3*4
amount_to_read = 4*4 amount_to_read = 4*4
mov amount_transferred(ebp), 0
mainloop:
mov eax, amount_to_read(ebp) mov eax, amount_to_read(ebp)
test eax, eax
jz exit
mov ecx, 32*1024 mov ecx, 32*1024
cmp eax, ecx cmp eax, ecx
jge 2f jge 2f
@ -53,33 +46,31 @@ mainloop:
mov ecx, 0x80 mov ecx, 0x80
or ebx, 0x210000 or ebx, 0x210000
callf (interrupt_ptr) callf (interrupt_ptr)
jc exit jnc success
test eax, eax
jz exit ! Process errors.
push eax
call __sys_seterrno
leave
ret
success:
! Copy eax bytes out of the transfer buffer. ! Copy eax bytes out of the transfer buffer.
mov ecx, eax
push eax push eax
mov ecx, eax
movzx esi, (transfer_buffer_ptr) movzx esi, (transfer_buffer_ptr)
mov edi, write_buffer(ebp) mov edi, write_buffer(ebp)
mov es, (pmode_ds) mov es, (pmode_ds)
cld cld
1: 1:
eseg lods eseg lodsb
movb (edi), al movb (edi), al
add edi, 4 inc edi
loop 1b loop 1b
pop eax pop eax
add write_buffer(ebp), eax
add amount_transferred(ebp), eax
sub amount_to_read(ebp), eax
jmp mainloop
exit:
mov eax, amount_transferred(ebp)
leave leave
ret ret

View file

@ -26,20 +26,13 @@
.define __sys_rawwrite .define __sys_rawwrite
__sys_rawwrite: __sys_rawwrite:
enter 4, 0 enter 0, 0
amount_transferred = -1*4
file_handle = 2*4 file_handle = 2*4
read_buffer = 3*4 read_buffer = 3*4
amount_to_write = 4*4 amount_to_write = 4*4
mov amount_transferred(ebp), 0
mainloop:
mov eax, amount_to_write(ebp) mov eax, amount_to_write(ebp)
test eax, eax mov ecx, 32*1024 ! size of transfer buffer
jz exit
mov ecx, 32*1024
cmp eax, ecx cmp eax, ecx
jge 2f jge 2f
mov ecx, eax mov ecx, eax
@ -62,17 +55,13 @@ mainloop:
o16 mov bx, file_handle(ebp) o16 mov bx, file_handle(ebp)
or ebx, 0x210000 or ebx, 0x210000
callf (interrupt_ptr) callf (interrupt_ptr)
jc exit jnc exit
! Update counters and go again.
add read_buffer(ebp), eax
add amount_transferred(ebp), eax
sub amount_to_write(ebp), eax
jmp mainloop
push eax
call __sys_seterrno
exit: exit:
mov eax, amount_transferred(ebp)
leave leave
ret ret
! vim: sw=4 ts=4 et

View file

@ -116,7 +116,7 @@ exe_start:
callf (pmode_switch) callf (pmode_switch)
jc bad_dpmi jc bad_dpmi
! We're now in protected mode. (ae) ! We're now in protected mode.
mov (psegcs), cs mov (psegcs), cs
mov (psegds), ds mov (psegds), ds
@ -222,10 +222,6 @@ exe_start:
push 0 push 0
retf ! 19b retf ! 19b
! ALL CODE ABOVE THIS POINT DISCARDED ON ENTRY TO 32-BIT CODE
! (it's reused as the 16-bit stack)
stack16:
! Helper routine which reallocates the linear block that the 32-bit code ! Helper routine which reallocates the linear block that the 32-bit code
! is running from. This can't happen from inside the 32-bit code itself ! is running from. This can't happen from inside the 32-bit code itself
! because it might move. ! because it might move.
@ -238,7 +234,7 @@ realloc:
o32 mov (dpmi_ebp), esp ! yes, saving esp into the ebp field o32 mov (dpmi_ebp), esp ! yes, saving esp into the ebp field
mov (dpmi_ss), ss mov (dpmi_ss), ss
mov ss, (psegds) mov ss, (psegds)
mov sp, stack16 mov sp, dosstack
sti sti
pusha pusha
@ -323,10 +319,10 @@ interrupt:
mov (dpmi_edi), di mov (dpmi_edi), di
mov ax, (rseg) mov ax, (rseg)
mov (dpmi_ds), ax mov (dpmi_ds), ax
mov (dpmi_ss), ax
push es push es
push ds push ds
xor ax, ax mov ax, dosstack ! auto stack is too small
mov (dpmi_ss), ax ! zero stack: DPMI host allocates one.
mov (dpmi_sp), ax mov (dpmi_sp), ax
push ds push ds
pop es pop es
@ -401,8 +397,10 @@ pmemhandle: .space 4 ! protected mode linear memory handle
pmemlen: .space 4 ! protected mode linear memory length pmemlen: .space 4 ! protected mode linear memory length
fh: .space 2 fh: .space 2
.space 128 .space 512
stack: stack:
.space 512
dosstack:
TRANSFER_BUFFER_SIZE = 32*1024 TRANSFER_BUFFER_SIZE = 32*1024
transfer_buffer: transfer_buffer: