Simplify and fix reading and writing so they seem to work.
This commit is contained in:
parent
cc356b5c75
commit
5329c98b81
|
@ -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,33 +46,36 @@ ssize_t read(int fd, void* buffer, size_t count)
|
||||||
if (r <= 0)
|
if (r <= 0)
|
||||||
return tot ? tot : r;
|
return tot ? tot : r;
|
||||||
|
|
||||||
q = memchr(p, 0x1a, (size_t)r);
|
if (text)
|
||||||
if (q)
|
|
||||||
{
|
{
|
||||||
_sys_rawlseek(fd, (off_t)(q - p) - r, SEEK_CUR);
|
q = memchr(p, 0x1a, (size_t)r);
|
||||||
r = q - p;
|
if (q)
|
||||||
eof = 1;
|
|
||||||
_sys_seteof(fd, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
q = memchr(p, '\r', (size_t)r);
|
|
||||||
if (!q)
|
|
||||||
return tot + r;
|
|
||||||
|
|
||||||
tot += q - p;
|
|
||||||
left = r - (q + 1 - p);
|
|
||||||
p = q;
|
|
||||||
++q;
|
|
||||||
|
|
||||||
while (left)
|
|
||||||
{
|
|
||||||
char c = *q++;
|
|
||||||
if (c != '\r')
|
|
||||||
{
|
{
|
||||||
*p++ = c;
|
_sys_rawlseek(fd, (off_t)(q - p) - r, SEEK_CUR);
|
||||||
++tot;
|
r = q - p;
|
||||||
|
eof = 1;
|
||||||
|
_sys_seteof(fd, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
q = memchr(p, '\r', (size_t)r);
|
||||||
|
if (!q)
|
||||||
|
return tot + r;
|
||||||
|
|
||||||
|
tot += q - p;
|
||||||
|
left = r - (q + 1 - p);
|
||||||
|
p = q;
|
||||||
|
++q;
|
||||||
|
|
||||||
|
while (left)
|
||||||
|
{
|
||||||
|
char c = *q++;
|
||||||
|
if (c != '\r')
|
||||||
|
{
|
||||||
|
*p++ = c;
|
||||||
|
++tot;
|
||||||
|
}
|
||||||
|
--left;
|
||||||
}
|
}
|
||||||
--left;
|
|
||||||
}
|
}
|
||||||
} while (tot < count && !eof && _sys_isreadyr(fd));
|
} while (tot < count && !eof && _sys_isreadyr(fd));
|
||||||
|
|
||||||
|
|
|
@ -20,42 +20,55 @@ 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;
|
||||||
while (left)
|
|
||||||
{
|
|
||||||
q = memchr(p, '\n', left);
|
|
||||||
if (!q)
|
|
||||||
return _sys_rawwrite(fd, p, left);
|
|
||||||
|
|
||||||
n = q - p;
|
if (_sys_getmode(fd) == O_BINARY)
|
||||||
if (n)
|
{
|
||||||
|
while (left)
|
||||||
{
|
{
|
||||||
r = _sys_rawwrite(fd, p, n);
|
r = _sys_rawwrite(fd, p, left);
|
||||||
if (r <= 0)
|
if (r <= 0)
|
||||||
return tot ? tot : r;
|
return tot ? tot : r;
|
||||||
tot += r;
|
tot += r;
|
||||||
p += r;
|
p += r;
|
||||||
left -= r;
|
left -= r;
|
||||||
if (r != n)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
r = _sys_rawwrite(fd, crlf, sizeof crlf);
|
else
|
||||||
if (r != 2)
|
{
|
||||||
|
/* If the file descriptor is an O_TEXT fd, translate LFs to CRLFs. */
|
||||||
|
while (left)
|
||||||
{
|
{
|
||||||
if (r > 0)
|
q = memchr(p, '\n', left);
|
||||||
r = 0;
|
if (!q)
|
||||||
return tot ? tot : r;
|
return _sys_rawwrite(fd, p, left);
|
||||||
|
|
||||||
|
n = q - p;
|
||||||
|
if (n)
|
||||||
|
{
|
||||||
|
r = _sys_rawwrite(fd, p, n);
|
||||||
|
if (r <= 0)
|
||||||
|
return tot ? tot : r;
|
||||||
|
tot += r;
|
||||||
|
p += r;
|
||||||
|
left -= r;
|
||||||
|
if (r != n)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
r = _sys_rawwrite(fd, crlf, sizeof crlf);
|
||||||
|
if (r != 2)
|
||||||
|
{
|
||||||
|
if (r > 0)
|
||||||
|
r = 0;
|
||||||
|
return tot ? tot : r;
|
||||||
|
}
|
||||||
|
++tot;
|
||||||
|
++p;
|
||||||
|
--left;
|
||||||
}
|
}
|
||||||
++tot;
|
|
||||||
++p;
|
|
||||||
--left;
|
|
||||||
}
|
}
|
||||||
return tot;
|
return tot;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -26,53 +26,42 @@
|
||||||
|
|
||||||
.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
|
mov eax, amount_to_write(ebp)
|
||||||
|
mov ecx, 32*1024 ! size of transfer buffer
|
||||||
mainloop:
|
cmp eax, ecx
|
||||||
mov eax, amount_to_write(ebp)
|
jge 2f
|
||||||
test eax, eax
|
mov ecx, eax
|
||||||
jz exit
|
|
||||||
|
|
||||||
mov ecx, 32*1024
|
|
||||||
cmp eax, ecx
|
|
||||||
jge 2f
|
|
||||||
mov ecx, eax
|
|
||||||
2:
|
2:
|
||||||
|
|
||||||
! Copy ecx bytes into the transfer buffer.
|
! Copy ecx bytes into the transfer buffer.
|
||||||
|
|
||||||
push ecx
|
push ecx
|
||||||
mov esi, read_buffer(ebp)
|
mov esi, read_buffer(ebp)
|
||||||
movzx edi, (transfer_buffer_ptr)
|
movzx edi, (transfer_buffer_ptr)
|
||||||
mov es, (pmode_ds)
|
mov es, (pmode_ds)
|
||||||
cld
|
cld
|
||||||
rep movsb
|
rep movsb
|
||||||
pop ecx
|
pop ecx
|
||||||
|
|
||||||
! Write from the transfer buffer to DOS.
|
! Write from the transfer buffer to DOS.
|
||||||
|
|
||||||
movb ah, 0x40
|
movb ah, 0x40
|
||||||
o16 mov dx, (transfer_buffer_ptr)
|
o16 mov dx, (transfer_buffer_ptr)
|
||||||
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
|
||||||
|
|
||||||
|
|
|
@ -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:
|
||||||
|
|
Loading…
Reference in a new issue