exit() now calls registered handlers before calling _exit().
And initialize_libc() can now register a handler to close stdout and stderr on program termination!! :)
The new one is write(fd, buf, count).
The old one was write(buf, count).
So the old one tries to pass buf as a file descriptor, and write() complains that 4000000 is too large of a file descriptor and throws EBADF.
We now use the new syscall, through the wrapper that fwrite() provides us.
read() and close() are in unistd.h, but open() in fnctl.h.
I thought only the definitions for O_SOMETHING were in fnctl.h, but it is as it is.
Don't know why, but let's not anger the Unix gods.
The FILE* C API is pending as well.
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.
%m as a format specifier is a nonstandard glibc extension, but I like it so I'm implementing it.
What it does is print the value of strerror(errno), without consuming any arguments to printf().
The exit() libc function already accepted an integer, but didn't pass it on to the kernel since we had no mechanism for it to do that.
Now, the kernel stores a task's exit status to display it later (and in the future, return it to userspace via wait()/waitpid())
We don't have files :) (yet)
But if someone wants to fprintf(stderr), then fine. Do it. Except it won't be any different from fprintf(stdout) or printf().