#include <sys/types.h>
#include <sys/errno.h>
#include <sys/stat.h>

#include "plasma.h"

off_t lseek(int __fildes, off_t __offset, int __whence ) {
	return EBADF;
}

int fstat( int __fd, struct stat *__sbuf ) {
	return EBADF;
	return -1;
}

int isatty(int desc) {
  return 0;
}

int open(const char *a, int b, ...) {
	errno = EACCES;
	return -1;
}

int close(int fd) {
	errno = EBADF;
	return -1;
}

#define MemoryRead(A) (*(volatile unsigned int*)(A))
#define MemoryWrite(A,V) *(volatile unsigned int*)(A)=(V)

static void sendchar(const char value)
{
   while((MemoryRead(IRQ_STATUS) & IRQ_UART_WRITE_AVAILABLE) == 0)
		;
   MemoryWrite(UART_WRITE, value);
}

int write(int fd, const void *_buf, size_t nbyte ) {
	const char *buf = _buf;
	int i;

	if (fd != 1) {
		errno = EBADF;
		return -1;
	}
	
	for (i = 0; i < nbyte; i++)
		sendchar(*buf++);

    return nbyte;
}

ssize_t read(int fd, void *buf, size_t count) {
	errno = EBADF;
	return -1;
}

