2007-04-27 22:42:41 +00:00
|
|
|
/* $Source$
|
|
|
|
* $State$
|
|
|
|
* $Revision$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <cpm.h>
|
|
|
|
|
2018-06-23 09:46:55 +00:00
|
|
|
ssize_t read(int fd, void* buffer, size_t count)
|
2007-04-27 22:42:41 +00:00
|
|
|
{
|
2018-05-04 22:21:01 +00:00
|
|
|
short save;
|
|
|
|
unsigned char before_n;
|
|
|
|
|
2007-04-27 22:42:41 +00:00
|
|
|
/* We're only allowed to read from fd 0, 1 or 2. */
|
|
|
|
if ((fd < 0) || (fd > 2))
|
|
|
|
{
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-04 22:21:01 +00:00
|
|
|
|
|
|
|
/* We need room for at least 1 char plus '\n'. */
|
|
|
|
if (count < 2)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make room to append '\n' later. */
|
|
|
|
before_n = count > 255 ? 255 : count - 1;
|
|
|
|
|
|
|
|
/* Borrow 2 bytes of RAM before the buffer. */
|
|
|
|
/* This might overwrite count!!! */
|
|
|
|
save = ((short*)buffer)[-1];
|
|
|
|
|
|
|
|
/* Read one line from the console. */
|
|
|
|
((unsigned char*)buffer)[-2] = before_n;
|
|
|
|
cpm_bc_register = CPM_BDOS_READ_CONSOLE_BUFFER;
|
2018-06-23 09:46:55 +00:00
|
|
|
cpm_de_register = (uint16_t)(char*)buffer - 2;
|
2018-05-04 22:21:01 +00:00
|
|
|
cpm_bdos();
|
|
|
|
before_n = ((unsigned char*)buffer)[-1];
|
|
|
|
|
|
|
|
((char*)buffer)[before_n] = '\n'; /* Append '\n'. */
|
|
|
|
((short*)buffer)[-1] = save; /* Give back borrowed bytes. */
|
|
|
|
|
|
|
|
/* Echo '\n' to console. */
|
|
|
|
cpm_bc_register = CPM_BDOS_PRINT_STRING;
|
2018-06-23 09:46:55 +00:00
|
|
|
cpm_de_register = (uint16_t)"\r\n$";
|
2007-04-27 22:42:41 +00:00
|
|
|
cpm_bdos();
|
|
|
|
|
2018-05-04 22:21:01 +00:00
|
|
|
return (int)before_n + 1;
|
2007-04-27 22:42:41 +00:00
|
|
|
}
|