add Chapter10

This commit is contained in:
Amar Mahmutbegovic
2024-09-09 20:55:25 +02:00
parent 8673086b40
commit 8ce616c0c9
203 changed files with 302587 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
#include <retarget.hpp>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
namespace
{
hal::uart *uart_stdio;
};
void retarget::set_stdio_uart(hal::uart *uart)
{
uart_stdio = uart;
/* Disable I/O buffering for STDOUT stream, so that
* chars are sent out as soon as they are printed. */
setvbuf(stdout, NULL, _IONBF, 0);
}
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
extern "C" int _write(int fd, char * ptr, int len)
{
if(fd == STDOUT_FILENO || fd == STDERR_FILENO)
{
uart_stdio->write_array(ptr, len);
}
return len;
}
extern "C" int _isatty(int fd)
{
if(fd >= STDIN_FILENO && fd <= STDERR_FILENO)
{
return 1;
}
errno = EBADF;
return 0;
}
extern "C" int _close(int fd)
{
if(fd >= STDIN_FILENO && fd <= STDERR_FILENO)
return 0;
errno = EBADF;
return -1;
}
extern "C" int _lseek(int fd, int ptr, int dir)
{
(void)fd;
(void)ptr;
(void)dir;
errno = EBADF;
return -1;
}
extern "C" int _read(int fd, char *ptr, int len)
{
return -1;
}
int _fstat(int fd, struct stat *st)
{
if(fd >= STDIN_FILENO && fd <= STDERR_FILENO)
{
st->st_mode = S_IFCHR;
return 0;
}
errno = EBADF;
return 0;
}

View File

@@ -0,0 +1,10 @@
#pragma once
#include <uart.hpp>
namespace retarget
{
void set_stdio_uart(hal::uart *uart);
};