/shell/system.c
#include <sysops.h>

#include <reent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/times.h>

#include <errno.h>
#undef errno
extern int errno;

void _exit(int status) {
	puts("No exit!\r\n");
	while (1) { }
}

int _close(int file) {
	return -1;
}

int _fstat(int file, struct stat *st) {
	st->st_mode = S_IFCHR;
	return 0;
}

int _gettimeofday(struct timeval *tv, struct timezone *tz) {
	errno = EINVAL;
	return -1;
};

int _isatty(int fd) {
	if (fd < 3)
		return 1;
	errno = EBADF;
	return 0;
}

int _getpid(void) {
	return 1;
}

int _kill(int pid, int sig) {
	errno = EINVAL;
	return -1;
}

int _link(char *old, char *new) {
	errno = EMLINK;
	return -1;
}

int _lseek(int file, int ptr, int dir) {
	return 0;
}

int _open(const char *name, int flags, int mode) {
	return -1;
}

int _read(int file, char *ptr, int len) {
	return 0;
}

int _times(struct tms *buf) {
	return -1;
}

int _unlink(char *name) {
	errno = ENOENT;
	return -1; 
}

int _write(int file, char *ptr, int len) {
	if (file == 1 || file == 2) {
		sys_ops->console->write((uint8_t *)ptr, len);
		return len;
	} else {
		errno = EBADF;
		return -1;
	}
}

void * malloc(size_t nbytes) {
	return sys_ops->alloc->malloc(nbytes);
}

void * _malloc_r(struct _reent *r, size_t nbytes) {
	return malloc(nbytes);
}

void * calloc(size_t n, size_t elem_size) {
	void *ptr = malloc(n * elem_size);
	memset(ptr, 0, n * elem_size);
	return ptr;
}

void * _calloc_r(struct _reent *r, size_t n, size_t elem_size) {
	return calloc(n, elem_size);
}

void free(void *ptr) {
	sys_ops->alloc->free(ptr);
}

void _free_r(struct _reent *r, void *ptr) {
	free(ptr);
}

void * realloc(void *ptr, size_t nbytes) {
	return sys_ops->alloc->realloc(ptr, nbytes);
}

void * _realloc_r(struct _reent *r, void *ptr, size_t nbytes) {
	return realloc(ptr, nbytes);
}