#include "poll.h" #include "sys/syscall.h" #include "__syscall.h" #include "unistd.h" extern void *__curr_brk; ssize_t read(int fd, void *buf, size_t count) { intptr_t res = __syscall3(SYS_read, (uintptr_t)fd, (uintptr_t)buf, (uintptr_t)count); if (res < 0) { return -1; } return (ssize_t)res; } ssize_t write(int fd, const void *buf, size_t count) { intptr_t res = __syscall3(SYS_write, (uintptr_t)fd, (uintptr_t)buf, (uintptr_t)count); if (res < 0) { return -1; } return (ssize_t)res; } int open(const char *pathname, int flags) { intptr_t res = __syscall3(SYS_open, (uintptr_t)pathname, (uintptr_t)flags, 0); if (res < 0) { return -1; } return (int)res; } int close(int fd) { intptr_t res = __syscall1(SYS_close, (uintptr_t)fd); if (res < 0) { return -1; } return (int)res; } int stat(const char *pathname, struct stat *statbuf) { intptr_t res = __syscall2(SYS_stat, (uintptr_t)pathname, (uintptr_t)statbuf); if (res < 0) { return -1; } return (int)res; } int fstat(int fd, struct stat *statbuf) { intptr_t res = __syscall2(SYS_fstat, (uintptr_t)fd, (uintptr_t)statbuf); if (res < 0) { return -1; } return (int)res; } int lstat(const char *pathname, struct stat *statbuf) { intptr_t res = __syscall2(SYS_lstat, (uintptr_t)pathname, (uintptr_t)statbuf); if (res < 0) { return -1; } return (int)res; } int poll(struct pollfd *fds, nfds_t nfds, int timeout) { intptr_t res = __syscall3(SYS_poll, (uintptr_t)fds, (uintptr_t)nfds, (uintptr_t)timeout); if (res < 0) { return -1; } return (int)res; } off_t lseek(int fd, off_t offset, int whence) { intptr_t res = __syscall3(SYS_lseek, (uintptr_t)fd, (uintptr_t)offset, (uintptr_t)whence); if (res < 0) { return -1; } return (int)res; } void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off) { intptr_t res = __syscall6(SYS_mmap, (uintptr_t)addr, (uintptr_t)len, (uintptr_t)prot, (uintptr_t)flags, (uintptr_t)fd, (uintptr_t)off); if (res < 0) { return NULL; } return (void *)res; } int mprotect(void *addr, size_t len, int prot) { intptr_t res = __syscall3(SYS_mprotect, (uintptr_t)addr, (uintptr_t)len, (uintptr_t)prot); if (res < 0) { return -1; } return (int)res; } int munmap(void *addr, size_t length) { intptr_t res = __syscall2(SYS_munmap, (uintptr_t)addr, (uintptr_t)length); if (res < 0) { return -1; } return (int)res; } int brk(void *addr) { intptr_t res = __syscall1(SYS_brk, (uintptr_t)addr); if (res < 0) { return -1; } __curr_brk = (void *)res; return (int)res; } int ioctl(int fd, unsigned long req, void *arg) { intptr_t res = __syscall3(SYS_ioctl, (uintptr_t)fd, (uintptr_t)req, (uintptr_t)arg); if (res < 0) { return -1; } return (int)res; } void _exit(int code) { __syscall1(SYS__exit, code); }