Luna/apps/src/init.c
apio da2ede3450 Kernel, libc, userspace: Implement file descriptors
Kernel: Implement a descriptor struct which stores the opened node and read offset, and give each task 8 of those.
Implement three syscalls: sys_read, sys_open and sys_close (sys_write still writes to the console instead of using a fd, for now)
Implement three new errors: ENOENT, EBADF and EMFILE.

libc: Implement the new errors, and the new syscalls in syscall().
Also fix _RETURN_WITH_ERRNO() to set errno correctly, which was making strerror() return null, thus crashing perror().

userspace: make init demonstrate the new file API.
2022-10-10 20:21:39 +02:00

69 lines
1.2 KiB
C

#include <luna.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
typedef long ssize_t;
int main()
{
if (gettid() == 0) // why are we the idle task?
{
__luna_abort("SHENANIGANS! init is tid 0 (which is reserved for the idle task)\n");
}
printf("Welcome to Luna!\n");
printf("Running as tid %ld\n\n", gettid());
sleep(1);
char version[40];
syscall(SYS_getversion, version, sizeof(version));
printf("Your kernel version is %s\n\n", version);
sleep(2);
const char* filename = "/sys/config";
printf("Opening %s for reading...\n", filename);
int fd = syscall(SYS_open, filename, 1);
if (fd < 0)
{
perror("open");
// return 1;
}
else { printf("Got fd %d\n", fd); }
char buf[4096];
ssize_t nread = syscall(SYS_read, fd, sizeof(buf), buf);
if (nread < 0)
{
perror("read");
// return 1;
}
else
{
buf[nread] = 0;
printf("Read %zd bytes\n\n", nread);
printf("%s", buf);
}
if (syscall(SYS_close, fd) < 0)
{
perror("close");
// return 1;
}
printf("\n\nPress any key to restart.\n");
return 0;
}