/* System Calls Remapping */

#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#undef _write
#undef write
int _write (int file, char * ptr, int len);
int write (int file, char * ptr, int len);

#undef errno
extern int errno;

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

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

extern int putchar (int ch);

int _write (int file, char * ptr, int len) {
  int i;

  for (i = 0; i < len; i++) putchar (*ptr++);
  return len;
}

int write (int file, char * ptr, int len) {
  int i;

  
  for (i = 0; i < len; i++) putchar (*ptr++);
  return len;
}


int _open (const char * path, int flags, ...) {
  return -1;
}

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

void _exit (int n) {
label:  goto label; /* endless loop */
}

int _kill (int n, int m) {
  errno = EINVAL;
  return(-1);
}

int _getpid (int n) {
  return 1;
}

register char * stack_ptr asm ("sp");

caddr_t _sbrk (int incr) {
  extern char   end asm ("end");	/* Defined by the linker */
  static char * heap_end;
         char * prev_heap_end;

  if (heap_end == NULL) heap_end = &end;
  prev_heap_end = heap_end;
  
  if (heap_end + incr > stack_ptr) {
    _write (1, "_sbrk: Heap and stack collision\n", 32);
    abort ();
  }  
  heap_end += incr;

  return (caddr_t) prev_heap_end;
}

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

int _link (void) {
  errno = EMLINK;
  return -1;
}

int _unlink (void) {
  errno = ENOENT;
  return -1;
}

void _raise (void) {
}
void raise (void) {
}

int isatty (int fd) {
  return 1;
}

