| #include <errno.h> |
| #include <sys/stat.h> |
| #include <unistd.h> |
| |
| #include <springbok_intrinsics.h> |
| |
| void _exit(int rc) { |
| springbok_finish(); |
| |
| while (1) { } |
| } |
| |
| void *_sbrk(int nbytes) { |
| extern int _sheap, _eheap; |
| static void *_heap_ptr = (void *)&_sheap; |
| if ((void*)&_eheap >= _heap_ptr + nbytes) { |
| void *base = _heap_ptr; |
| _heap_ptr += nbytes; |
| return base; |
| } else { |
| errno = ENOMEM; |
| return (void *)-1; |
| } |
| } |
| |
| int _read(int file, char *ptr, int len) { |
| if (file != STDIN_FILENO) { |
| errno = EBADF; |
| return -1; |
| } |
| |
| return 0; |
| } |
| |
| |
| int _write(int file, char *buf, int nbytes) { |
| static int _write_line_buffer_len[2] = {0, 0}; |
| static char _write_line_buffer[2][256]; |
| |
| if (file != STDOUT_FILENO && file != STDERR_FILENO) { |
| errno = EBADF; |
| return -1; |
| } |
| |
| if (nbytes == 0) { |
| return 0; |
| } |
| |
| if (buf == NULL) { |
| errno = EFAULT; |
| return -1; |
| } |
| |
| const int buffer_num = (file == STDOUT_FILENO)? 0 : 1; |
| const int buffer_level = (file == STDOUT_FILENO)? SPRINGBOK_SIMPRINT_INFO : SPRINGBOK_SIMPRINT_ERROR; |
| |
| char c; |
| do { |
| c = *(buf++); |
| if (c == '\n') { |
| _write_line_buffer[buffer_num][_write_line_buffer_len[buffer_num]] = '\0'; |
| springbok_simprint(buffer_level, _write_line_buffer[buffer_num], buffer_num); |
| _write_line_buffer_len[buffer_num] = 0; |
| } else if (c != '\0') { |
| _write_line_buffer[buffer_num][_write_line_buffer_len[buffer_num]] = c; |
| _write_line_buffer_len[buffer_num]++; |
| if (_write_line_buffer_len[buffer_num] == 255) { |
| _write_line_buffer[buffer_num][_write_line_buffer_len[buffer_num]] = '\0'; |
| springbok_simprint(buffer_level, _write_line_buffer[buffer_num], buffer_num); |
| _write_line_buffer_len[buffer_num] = 0; |
| } |
| } |
| } while(c != '\0'); |
| |
| return nbytes; |
| } |
| |
| int _close(int file) { |
| errno = EBADF; |
| return -1; |
| } |
| |
| int _lseek(int file, int offset, int whence) { |
| if (file != STDOUT_FILENO && file != STDERR_FILENO) { |
| errno = EBADF; |
| return -1; |
| } |
| |
| return 0; |
| } |
| |
| int _fstat(int file, struct stat *st) { |
| if (file != STDOUT_FILENO && file != STDERR_FILENO) { |
| errno = EBADF; |
| return -1; |
| } |
| |
| if (st == NULL) { |
| errno = EFAULT; |
| return -1; |
| } |
| |
| st->st_mode = S_IFCHR; |
| return 0; |
| } |
| |
| int _isatty(int file) { |
| if (file != STDOUT_FILENO && file != STDERR_FILENO) { |
| errno = EBADF; |
| return -1; |
| } |
| |
| return 1; |
| } |