Luna/libs/libc/src/unistd.cpp

112 lines
2.8 KiB
C++

#include <bits/error.h>
#include <fcntl.h>
#include <luna.h>
#include <luna/syscall.h>
#include <stdarg.h>
#include <unistd.h>
extern "C"
{
int execv(const char* program, char* const[])
{
return (int)syscall(SYS_exec, program);
}
int execve(const char*, char* const[], char* const[])
{
NOT_IMPLEMENTED("execve");
}
int execvp(const char*, char* const[])
{
NOT_IMPLEMENTED("execvp");
}
pid_t fork(void)
{
NOT_IMPLEMENTED("fork");
}
long syscall(long number, ...)
{
typedef unsigned long int arg;
long result;
va_list ap;
va_start(ap, number);
switch (number)
{
case SYS_clock:
case SYS_yield:
case SYS_gettid: result = __luna_syscall0(number); break;
case SYS_exit:
case SYS_close:
case SYS_exec:
case SYS_sleep: result = __luna_syscall1(number, va_arg(ap, arg)); break;
case SYS_munmap:
case SYS_open: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
result = __luna_syscall2(number, arg0, arg1);
break;
}
case SYS_fcntl:
case SYS_seek:
case SYS_write:
case SYS_read:
case SYS_mprotect:
case SYS_mmap: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
arg arg2 = va_arg(ap, arg);
result = __luna_syscall3(number, arg0, arg1, arg2);
break;
}
case SYS_paint: {
arg arg0 = va_arg(ap, arg);
arg arg1 = va_arg(ap, arg);
arg arg2 = va_arg(ap, arg);
arg arg3 = va_arg(ap, arg);
arg arg4 = va_arg(ap, arg);
result = __luna_syscall5(number, arg0, arg1, arg2, arg3, arg4);
break;
}
default: result = -ENOSYS; break;
}
va_end(ap);
if (number == SYS_mmap) { _RETURN_WITH_MEMORY_ERRNO(result, long int); }
else { _RETURN_WITH_ERRNO(result, long); }
}
unsigned int sleep(unsigned int seconds)
{
return msleep(seconds * 1000);
}
ssize_t read(int fd, void* buf, size_t count)
{
return syscall(SYS_read, fd, count, buf); // yes, our read() syscall is in the wrong order.
}
ssize_t write(int fd, const void* buf, size_t count)
{
return syscall(SYS_write, fd, count, buf); // yes, our write() syscall is in the wrong order.
}
int close(int fd)
{
return (int)syscall(SYS_close, fd);
}
off_t lseek(int fd, off_t offset, int whence)
{
return syscall(SYS_seek, fd, offset, whence);
}
__lc_noreturn void _exit(int status)
{
syscall(SYS_exit, status);
__lc_unreachable();
}
int dup(int fd)
{
return fcntl(fd, F_DUPFD, 0);
}
}