kernel+libc: Add the write() system call, and fwrite()

This commit is contained in:
apio 2023-03-12 11:37:41 +01:00
parent 92dbe58729
commit 292433dc39
Signed by: apio
GPG Key ID: B8A7D06E42258954
6 changed files with 44 additions and 2 deletions

View File

@ -26,3 +26,26 @@ Result<u64> sys_read(Registers*, SyscallArgs args)
return nread; return nread;
} }
Result<u64> sys_write(Registers*, SyscallArgs args)
{
int fd = (int)args[0];
const u8* buf = (const u8*)args[1];
usize size = (usize)args[2];
if (!MemoryManager::validate_user_read(buf, size)) return err(EFAULT);
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
Thread* current = Scheduler::current();
Option<FileDescriptor>& descriptor = current->fd_table->fds[fd];
if (!descriptor.has_value()) return err(EBADF);
usize nwritten = TRY(descriptor->inode->write(buf, descriptor->offset, size));
descriptor->offset += nwritten;
return nwritten;
}

View File

@ -35,7 +35,8 @@ extern "C"
/* Read arbitrarily sized items from a stream. */ /* Read arbitrarily sized items from a stream. */
size_t fread(void* buf, size_t size, size_t nmemb, FILE* stream); size_t fread(void* buf, size_t size, size_t nmemb, FILE* stream);
size_t fwrite(const void*, size_t, size_t, FILE*); /* Write arbitrarily sized items to a stream. */
size_t fwrite(const void* buf, size_t size, size_t nmemb, FILE* stream);
int fseek(FILE*, long, int); int fseek(FILE*, long, int);
long ftell(FILE*); long ftell(FILE*);

View File

@ -38,6 +38,9 @@ extern "C"
/* Read bytes from a file descriptor. */ /* Read bytes from a file descriptor. */
ssize_t read(int fd, void* buf, size_t size); ssize_t read(int fd, void* buf, size_t size);
/* Write bytes to a file descriptor. */
ssize_t write(int fd, const void* buf, size_t size);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -66,6 +66,15 @@ extern "C"
return (size_t)nread / size; return (size_t)nread / size;
} }
size_t fwrite(const void* buf, size_t size, size_t nmemb, FILE* stream)
{
if (size * nmemb == 0) return 0;
ssize_t nwrite = write(stream->_fd, buf, size * nmemb);
return (size_t)nwrite / size;
}
int ferror(FILE* stream) int ferror(FILE* stream)
{ {
return stream->_err; return stream->_err;

View File

@ -54,4 +54,10 @@ extern "C"
long rc = syscall(SYS_read, fd, buf, size); long rc = syscall(SYS_read, fd, buf, size);
__errno_return(rc, ssize_t); __errno_return(rc, ssize_t);
} }
ssize_t write(int fd, const void* buf, size_t size)
{
long rc = syscall(SYS_write, fd, buf, size);
__errno_return(rc, ssize_t);
}
} }

View File

@ -2,7 +2,7 @@
#define enumerate_syscalls(_e) \ #define enumerate_syscalls(_e) \
_e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep) _e(open) \ _e(exit) _e(console_write) _e(clock_gettime) _e(allocate_memory) _e(deallocate_memory) _e(usleep) _e(open) \
_e(close) _e(read) _e(getpid) _e(close) _e(read) _e(getpid) _e(write)
enum Syscalls enum Syscalls
{ {